Skip to content

Commit 950fc2b

Browse files
committed
Backport CStr improvement to remove need for a nightly feature
1 parent e280b81 commit 950fc2b

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

rust/kernel/src/chrdev.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::error::{Error, KernelResult};
1414
use crate::file_operations;
1515
use crate::types::CStr;
1616

17-
pub fn builder(name: &'static CStr, minors: Range<u16>) -> KernelResult<Builder> {
17+
pub fn builder(name: CStr<'static>, minors: Range<u16>) -> KernelResult<Builder> {
1818
Ok(Builder {
1919
name,
2020
minors,
@@ -23,7 +23,7 @@ pub fn builder(name: &'static CStr, minors: Range<u16>) -> KernelResult<Builder>
2323
}
2424

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

rust/kernel/src/filesystem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl<T: FileSystem> Drop for Registration<T> {
2626
}
2727

2828
pub trait FileSystem: Sync {
29-
const NAME: &'static CStr;
29+
const NAME: CStr<'static>;
3030
const FLAGS: FileSystemFlags;
3131
}
3232

rust/kernel/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! The `kernel` crate
44
55
#![no_std]
6-
#![feature(allocator_api, alloc_error_handler, const_raw_ptr_deref)]
6+
#![feature(allocator_api, alloc_error_handler)]
77

88
extern crate alloc;
99

rust/kernel/src/sysctl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ unsafe extern "C" fn proc_handler<T: SysctlStorage>(
120120

121121
impl<T: SysctlStorage> Sysctl<T> {
122122
pub fn register(
123-
path: &'static types::CStr,
124-
name: &'static types::CStr,
123+
path: types::CStr<'static>,
124+
name: types::CStr<'static>,
125125
storage: T,
126126
mode: types::Mode,
127127
) -> error::KernelResult<Sysctl<T>> {

rust/kernel/src/types.rs

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

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

35-
impl Deref for CStr {
35+
impl Deref for CStr<'_> {
3636
type Target = str;
3737

3838
fn deref(&self) -> &str {
39-
&self.0
39+
self.0
4040
}
4141
}
4242

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

0 commit comments

Comments
 (0)