Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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