summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiguel Ojeda <ojeda@users.noreply.github.com>2020-09-18 19:56:08 +0200
committerGitHub <noreply@github.com>2020-09-18 19:56:08 +0200
commit7f60ab154ad56cb57812fb002125c62cba338821 (patch)
tree6c9e5963b86ba1ed0fe30d2ba4349ab3c8a21600
parent252a474bd9073361299ae6b0ac702e00868cf7b7 (diff)
parent59ac2fe62c6d72de5cdad3feb34baa261f366b43 (diff)
Merge pull request #6 from Rust-for-Linux/cstr-improvement
Backport CStr improvement to remove need for a nightly feature
-rw-r--r--rust/kernel/src/chrdev.rs4
-rw-r--r--rust/kernel/src/lib.rs2
-rw-r--r--rust/kernel/src/sysctl.rs4
-rw-r--r--rust/kernel/src/types.rs14
4 files changed, 12 insertions, 12 deletions
diff --git a/rust/kernel/src/chrdev.rs b/rust/kernel/src/chrdev.rs
index 4606a7bd5a73..d32b9a0d584a 100644
--- a/rust/kernel/src/chrdev.rs
+++ b/rust/kernel/src/chrdev.rs
@@ -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,
@@ -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>,
}
diff --git a/rust/kernel/src/lib.rs b/rust/kernel/src/lib.rs
index 18d553c094ff..b545e5284972 100644
--- a/rust/kernel/src/lib.rs
+++ b/rust/kernel/src/lib.rs
@@ -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;
diff --git a/rust/kernel/src/sysctl.rs b/rust/kernel/src/sysctl.rs
index 654b7122ba3b..b94cdbc939cc 100644
--- a/rust/kernel/src/sysctl.rs
+++ b/rust/kernel/src/sysctl.rs
@@ -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>> {
diff --git a/rust/kernel/src/types.rs b/rust/kernel/src/types.rs
index ea5f8d7c2ebb..e9b4d13d4f8c 100644
--- a/rust/kernel/src/types.rs
+++ b/rust/kernel/src/types.rs
@@ -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 {