Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport CStr improvement to remove need for a nightly feature #6

Merged
merged 1 commit into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rust/kernel/src/chrdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::error::{Error, KernelResult};
use crate::file_operations;
use crate::types::CStr;

pub fn builder(name: &'static CStr, minors: Range<u16>) -> KernelResult<Builder> {
pub fn builder(name: CStr<'static>, minors: Range<u16>) -> KernelResult<Builder> {
Ok(Builder {
name,
minors,
Expand All @@ -23,7 +23,7 @@ pub fn builder(name: &'static CStr, minors: Range<u16>) -> KernelResult<Builder>
}

pub struct Builder {
name: &'static CStr,
name: CStr<'static>,
minors: Range<u16>,
file_ops: Vec<&'static bindings::file_operations>,
}
Expand Down
2 changes: 1 addition & 1 deletion rust/kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! The `kernel` crate

#![no_std]
#![feature(allocator_api, alloc_error_handler, const_raw_ptr_deref)]
#![feature(allocator_api, alloc_error_handler)]

extern crate alloc;

Expand Down
4 changes: 2 additions & 2 deletions rust/kernel/src/sysctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ unsafe extern "C" fn proc_handler<T: SysctlStorage>(

impl<T: SysctlStorage> Sysctl<T> {
pub fn register(
path: &'static types::CStr,
name: &'static types::CStr,
path: types::CStr<'static>,
name: types::CStr<'static>,
storage: T,
mode: types::Mode,
) -> error::KernelResult<Sysctl<T>> {
Expand Down
14 changes: 7 additions & 7 deletions rust/kernel/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@ impl Mode {
/// A string that is guaranteed to have exactly one NUL byte, which is at the
/// end. Used for interoperability with kernel APIs that take C strings.
#[repr(transparent)]
pub struct CStr(str);
pub struct CStr<'a>(&'a str);

impl CStr {
impl CStr<'_> {
/// Creates a new CStr from a str without performing any additional checks.
/// # Safety
///
/// `data` _must_ end with a NUL byte, and should only have only a single
/// NUL byte, or the string will be truncated.
pub const unsafe fn new_unchecked(data: &str) -> &CStr {
&*(data as *const str as *const CStr)
pub const unsafe fn new_unchecked(data: &str) -> CStr {
CStr(data)
}
}

impl Deref for CStr {
impl Deref for CStr<'_> {
type Target = str;

fn deref(&self) -> &str {
&self.0
self.0
}
}

/// Creates a new `CStr` from a string literal. The string literal should not contain any NUL
/// bytes. Example usage:
/// ```
/// const MY_CSTR: &CStr = cstr!("My awesome CStr!");
/// const MY_CSTR: CStr<'static> = cstr!("My awesome CStr!");
/// ```
#[macro_export]
macro_rules! cstr {
Expand Down