From 59ac2fe62c6d72de5cdad3feb34baa261f366b43 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 12 Sep 2020 11:05:05 -0400 Subject: Backport CStr improvement to remove need for a nightly feature --- rust/kernel/src/chrdev.rs | 4 ++-- rust/kernel/src/lib.rs | 2 +- rust/kernel/src/sysctl.rs | 4 ++-- 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) -> KernelResult { +pub fn builder(name: CStr<'static>, minors: Range) -> KernelResult { Ok(Builder { name, minors, @@ -23,7 +23,7 @@ pub fn builder(name: &'static CStr, minors: Range) -> KernelResult } pub struct Builder { - name: &'static CStr, + name: CStr<'static>, minors: Range, 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( impl Sysctl { 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> { 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 { -- cgit v1.2.3