Skip to content

Commit

Permalink
Merge pull request #6 from Rust-for-Linux/cstr-improvement
Browse files Browse the repository at this point in the history
Backport CStr improvement to remove need for a nightly feature
  • Loading branch information
ojeda authored Sep 18, 2020
2 parents 252a474 + 59ac2fe commit 7f60ab1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions rust/kernel/src/chrdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16>) -> KernelResult<Builder> {
pub fn builder(name: CStr<'static>, minors: Range<u16>) -> KernelResult<Builder> {
Ok(Builder {
name,
minors,
Expand All @@ -23,7 +23,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 rust/kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions rust/kernel/src/sysctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,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 rust/kernel/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7f60ab1

Please sign in to comment.