Re: [PATCH v14 3/3] rust: replace `CStr` with `core::ffi::CStr`
From: Tamir Duberstein
Date: Wed Aug 13 2025 - 10:11:43 EST
On Tue, Aug 12, 2025 at 3:50 AM Benno Lossin <lossin@xxxxxxxxxx> wrote:
>
> On Thu Jul 10, 2025 at 4:53 PM CEST, Tamir Duberstein wrote:
> > `kernel::ffi::CStr` was introduced in commit d126d2380131 ("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: https://github.com/Rust-for-Linux/linux/commit/faa3cbcca03d0dec8f8e43f1d8d5c0860d98a23f [0]
> > Acked-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
> > Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> > Signed-off-by: Tamir Duberstein <tamird@xxxxxxxxx>
>
> Reviewed-by: Benno Lossin <lossin@xxxxxxxxxx>
Thanks!
> > ---
> > rust/ffi.rs | 2 +
> > rust/kernel/device.rs | 1 +
> > rust/kernel/error.rs | 2 +
> > rust/kernel/firmware.rs | 9 +-
> > rust/kernel/prelude.rs | 4 +-
> > rust/kernel/seq_file.rs | 2 +-
> > rust/kernel/str.rs | 395 +++++++++-------------------------------
> > rust/kernel/sync/condvar.rs | 2 +-
> > rust/kernel/sync/lock.rs | 2 +-
> > rust/kernel/sync/lock/global.rs | 2 +-
> > 10 files changed, 106 insertions(+), 315 deletions(-)
>
> > +/// Extensions to [`CStr`].
> > +pub trait CStrExt {
>
> Should we make this trait sealed?
We can -- but is it harmful for someone to implement? I think probably not.
>
> ---
> Cheers,
> Benno
>
> > /// Wraps a raw C string pointer.
> > ///
> > /// # Safety