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

Commit abc9791

Browse files
authored
Refactor the CStr API to reduce the need for nightly features (#271)
1 parent 079f100 commit abc9791

File tree

7 files changed

+15
-15
lines changed

7 files changed

+15
-15
lines changed

Diff for: src/chrdev.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::error::{Error, KernelResult};
1212
use crate::file_operations;
1313
use crate::types::CStr;
1414

15-
pub fn builder(name: &'static CStr, minors: Range<u16>) -> KernelResult<Builder> {
15+
pub fn builder(name: CStr<'static>, minors: Range<u16>) -> KernelResult<Builder> {
1616
Ok(Builder {
1717
name,
1818
minors,
@@ -21,7 +21,7 @@ pub fn builder(name: &'static CStr, minors: Range<u16>) -> KernelResult<Builder>
2121
}
2222

2323
pub struct Builder {
24-
name: &'static CStr,
24+
name: CStr<'static>,
2525
minors: Range<u16>,
2626
file_ops: Vec<&'static bindings::file_operations>,
2727
}

Diff for: src/filesystem.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<T: FileSystem> Drop for Registration<T> {
2222
}
2323

2424
pub trait FileSystem: Sync {
25-
const NAME: &'static CStr;
25+
const NAME: CStr<'static>;
2626
const FLAGS: FileSystemFlags;
2727
}
2828

Diff for: src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![no_std]
2-
#![feature(allocator_api, alloc_error_handler, const_raw_ptr_deref)]
2+
#![feature(allocator_api, alloc_error_handler)]
33

44
extern crate alloc;
55

Diff for: src/sysctl.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ unsafe extern "C" fn proc_handler<T: SysctlStorage>(
118118

119119
impl<T: SysctlStorage> Sysctl<T> {
120120
pub fn register(
121-
path: &'static types::CStr,
122-
name: &'static types::CStr,
121+
path: types::CStr<'static>,
122+
name: types::CStr<'static>,
123123
storage: T,
124124
mode: types::Mode,
125125
) -> error::KernelResult<Sysctl<T>> {

Diff for: src/types.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,31 @@ impl Mode {
1717
/// A string that is guaranteed to have exactly one NUL byte, which is at the
1818
/// end. Used for interoperability with kernel APIs that take C strings.
1919
#[repr(transparent)]
20-
pub struct CStr(str);
20+
pub struct CStr<'a>(&'a str);
2121

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

33-
impl Deref for CStr {
33+
impl Deref for CStr<'_> {
3434
type Target = str;
3535

3636
fn deref(&self) -> &str {
37-
&self.0
37+
self.0
3838
}
3939
}
4040

4141
/// Creates a new `CStr` from a string literal. The string literal should not contain any NUL
4242
/// bytes. Example usage:
4343
/// ```
44-
/// const MY_CSTR: &CStr = cstr!("My awesome CStr!");
44+
/// const MY_CSTR: CStr<'static> = cstr!("My awesome CStr!");
4545
/// ```
4646
#[macro_export]
4747
macro_rules! cstr {

Diff for: tests/filesystem/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct TestFSModule {
1212
struct TestFS {}
1313

1414
impl FileSystem for TestFS {
15-
const NAME: &'static CStr = cstr!("testfs");
15+
const NAME: CStr<'static> = cstr!("testfs");
1616
const FLAGS: FileSystemFlags = FileSystemFlags::empty();
1717
}
1818

Diff for: tests/utils/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
struct UtilsTestModule;
44

55
#[allow(dead_code)]
6-
const TEST_CSTR: &linux_kernel_module::CStr = linux_kernel_module::cstr!("abc");
6+
const TEST_CSTR: linux_kernel_module::CStr<'static> = linux_kernel_module::cstr!("abc");
77

88
impl linux_kernel_module::KernelModule for UtilsTestModule {
99
fn init() -> linux_kernel_module::KernelResult<Self> {

0 commit comments

Comments
 (0)