From abc979140d6a070867c9e78e251c2c689782274f Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sun, 6 Sep 2020 18:39:57 -0400 Subject: [PATCH] Refactor the CStr API to reduce the need for nightly features (#271) --- src/chrdev.rs | 4 ++-- src/filesystem.rs | 2 +- src/lib.rs | 2 +- src/sysctl.rs | 4 ++-- src/types.rs | 14 +++++++------- tests/filesystem/src/lib.rs | 2 +- tests/utils/src/lib.rs | 2 +- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/chrdev.rs b/src/chrdev.rs index d6d10fbb..0fb4402c 100644 --- a/src/chrdev.rs +++ b/src/chrdev.rs @@ -12,7 +12,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, @@ -21,7 +21,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/src/filesystem.rs b/src/filesystem.rs index c0f475dd..e589728c 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -22,7 +22,7 @@ impl Drop for Registration { } pub trait FileSystem: Sync { - const NAME: &'static CStr; + const NAME: CStr<'static>; const FLAGS: FileSystemFlags; } diff --git a/src/lib.rs b/src/lib.rs index f4fa3581..ea096a79 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ #![no_std] -#![feature(allocator_api, alloc_error_handler, const_raw_ptr_deref)] +#![feature(allocator_api, alloc_error_handler)] extern crate alloc; diff --git a/src/sysctl.rs b/src/sysctl.rs index 71b054a1..2545e60d 100644 --- a/src/sysctl.rs +++ b/src/sysctl.rs @@ -118,8 +118,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/src/types.rs b/src/types.rs index 0c83cb2e..9c7653bd 100644 --- a/src/types.rs +++ b/src/types.rs @@ -17,31 +17,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 { diff --git a/tests/filesystem/src/lib.rs b/tests/filesystem/src/lib.rs index 0f939221..e1557e48 100644 --- a/tests/filesystem/src/lib.rs +++ b/tests/filesystem/src/lib.rs @@ -12,7 +12,7 @@ struct TestFSModule { struct TestFS {} impl FileSystem for TestFS { - const NAME: &'static CStr = cstr!("testfs"); + const NAME: CStr<'static> = cstr!("testfs"); const FLAGS: FileSystemFlags = FileSystemFlags::empty(); } diff --git a/tests/utils/src/lib.rs b/tests/utils/src/lib.rs index 1ba9fcfd..0b34dd80 100644 --- a/tests/utils/src/lib.rs +++ b/tests/utils/src/lib.rs @@ -3,7 +3,7 @@ struct UtilsTestModule; #[allow(dead_code)] -const TEST_CSTR: &linux_kernel_module::CStr = linux_kernel_module::cstr!("abc"); +const TEST_CSTR: linux_kernel_module::CStr<'static> = linux_kernel_module::cstr!("abc"); impl linux_kernel_module::KernelModule for UtilsTestModule { fn init() -> linux_kernel_module::KernelResult {