Skip to content
This repository has been archived by the owner on Mar 7, 2021. It is now read-only.

Commit

Permalink
Refactor the CStr API to reduce the need for nightly features (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex authored Sep 6, 2020
1 parent 079f100 commit abc9791
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/chrdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16>) -> KernelResult<Builder> {
pub fn builder(name: CStr<'static>, minors: Range<u16>) -> KernelResult<Builder> {
Ok(Builder {
name,
minors,
Expand All @@ -21,7 +21,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 src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<T: FileSystem> Drop for Registration<T> {
}

pub trait FileSystem: Sync {
const NAME: &'static CStr;
const NAME: CStr<'static>;
const FLAGS: FileSystemFlags;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/sysctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,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 src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/filesystem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
Expand Down

0 comments on commit abc9791

Please sign in to comment.