diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2020-09-12 11:05:05 -0400 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2020-09-12 12:05:24 -0400 |
commit | 59ac2fe62c6d72de5cdad3feb34baa261f366b43 (patch) | |
tree | 6c9e5963b86ba1ed0fe30d2ba4349ab3c8a21600 | |
parent | 252a474bd9073361299ae6b0ac702e00868cf7b7 (diff) |
Backport CStr improvement to remove need for a nightly feature
-rw-r--r-- | rust/kernel/src/chrdev.rs | 4 | ||||
-rw-r--r-- | rust/kernel/src/lib.rs | 2 | ||||
-rw-r--r-- | rust/kernel/src/sysctl.rs | 4 | ||||
-rw-r--r-- | rust/kernel/src/types.rs | 14 |
4 files changed, 12 insertions, 12 deletions
diff --git a/rust/kernel/src/chrdev.rs b/rust/kernel/src/chrdev.rs index 4606a7bd5a73..d32b9a0d584a 100644 --- a/rust/kernel/src/chrdev.rs +++ b/rust/kernel/src/chrdev.rs @@ -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, @@ -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>, } diff --git a/rust/kernel/src/lib.rs b/rust/kernel/src/lib.rs index 18d553c094ff..b545e5284972 100644 --- a/rust/kernel/src/lib.rs +++ b/rust/kernel/src/lib.rs @@ -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; diff --git a/rust/kernel/src/sysctl.rs b/rust/kernel/src/sysctl.rs index 654b7122ba3b..b94cdbc939cc 100644 --- a/rust/kernel/src/sysctl.rs +++ b/rust/kernel/src/sysctl.rs @@ -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>> { diff --git a/rust/kernel/src/types.rs b/rust/kernel/src/types.rs index ea5f8d7c2ebb..e9b4d13d4f8c 100644 --- a/rust/kernel/src/types.rs +++ b/rust/kernel/src/types.rs @@ -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 { |