Skip to content

Commit 3b83f5d

Browse files
tamirdojeda
authored andcommitted
rust: replace CStr with core::ffi::CStr
`kernel::ffi::CStr` was introduced in commit d126d23 ("rust: str: add `CStr` type") in November 2022 as an upstreaming of earlier work that was done in May 2021[0]. That earlier work, having predated the inclusion of `CStr` in `core`, largely duplicated the implementation of `std::ffi::CStr`. `std::ffi::CStr` was moved to `core::ffi::CStr` in Rust 1.64 in September 2022. Hence replace `kernel::str::CStr` with `core::ffi::CStr` to reduce our custom code footprint, and retain needed custom functionality through an extension trait. Add `CStr` to `ffi` and the kernel prelude. Link: faa3cbc [0] Acked-by: Greg Kroah-Hartman <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Acked-by: Danilo Krummrich <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Signed-off-by: Tamir Duberstein <[email protected]> Link: https://patch.msgid.link/[email protected] [ Removed assert that would now depend on the Rust version. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent c5cf01b commit 3b83f5d

File tree

13 files changed

+113
-316
lines changed

13 files changed

+113
-316
lines changed

drivers/android/binder/stats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl BinderStats {
6161

6262
mod strings {
6363
use core::str::from_utf8_unchecked;
64-
use kernel::str::CStr;
64+
use kernel::str::{CStr, CStrExt as _};
6565

6666
extern "C" {
6767
static binder_command_strings: [*const u8; super::BC_COUNT];

rust/ffi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ alias! {
4646
}
4747

4848
pub use core::ffi::c_void;
49+
50+
pub use core::ffi::CStr;

rust/kernel/debugfs/entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use crate::debugfs::file_ops::FileOps;
55
use crate::ffi::c_void;
6-
use crate::str::CStr;
6+
use crate::str::{CStr, CStrExt as _};
77
use crate::sync::Arc;
88
use core::marker::PhantomData;
99

rust/kernel/device.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use core::{marker::PhantomData, ptr};
1313

1414
#[cfg(CONFIG_PRINTK)]
1515
use crate::c_str;
16+
use crate::str::CStrExt as _;
1617

1718
pub mod property;
1819

rust/kernel/drm/ioctl.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ macro_rules! declare_drm_ioctls {
156156
Some($cmd)
157157
},
158158
flags: $flags,
159-
name: $crate::c_str!(::core::stringify!($cmd)).as_char_ptr(),
159+
name: $crate::str::as_char_ptr_in_const_context(
160+
$crate::c_str!(::core::stringify!($cmd)),
161+
),
160162
}
161163
),*];
162164
ioctls

rust/kernel/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ impl Error {
182182
if ptr.is_null() {
183183
None
184184
} else {
185+
use crate::str::CStrExt as _;
186+
185187
// SAFETY: The string returned by `errname` is static and `NUL`-terminated.
186188
Some(unsafe { CStr::from_char_ptr(ptr) })
187189
}

rust/kernel/firmware.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
//!
55
//! C header: [`include/linux/firmware.h`](srctree/include/linux/firmware.h)
66
7-
use crate::{bindings, device::Device, error::Error, error::Result, ffi, str::CStr};
7+
use crate::{
8+
bindings,
9+
device::Device,
10+
error::Error,
11+
error::Result,
12+
ffi,
13+
str::{CStr, CStrExt as _},
14+
};
815
use core::ptr::NonNull;
916

1017
/// # Invariants

rust/kernel/prelude.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub use core::{
1919

2020
pub use ::ffi::{
2121
c_char, c_int, c_long, c_longlong, c_schar, c_short, c_uchar, c_uint, c_ulong, c_ulonglong,
22-
c_ushort, c_void,
22+
c_ushort, c_void, CStr,
2323
};
2424

2525
pub use crate::alloc::{flags::*, Box, KBox, KVBox, KVVec, KVec, VBox, VVec, Vec};
@@ -43,7 +43,7 @@ pub use super::static_assert;
4343

4444
pub use super::error::{code::*, Error, Result};
4545

46-
pub use super::{str::CStr, ThisModule};
46+
pub use super::{str::CStrExt as _, ThisModule};
4747

4848
pub use super::init::InPlaceInit;
4949

rust/kernel/seq_file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//!
55
//! C header: [`include/linux/seq_file.h`](srctree/include/linux/seq_file.h)
66
7-
use crate::{bindings, c_str, fmt, types::NotThreadSafe, types::Opaque};
7+
use crate::{bindings, c_str, fmt, str::CStrExt as _, types::NotThreadSafe, types::Opaque};
88

99
/// A utility for generating the contents of a seq file.
1010
#[repr(transparent)]

0 commit comments

Comments
 (0)