On Fri, May 30, 2025 at 08:27:45AM -0400, Tamir Duberstein wrote: > C-String literals were added in Rust 1.77. Replace instances of > `kernel::c_str!` with C-String literals where possible and rename > `kernel::c_str!` to `str_to_cstr!` to clarify its intended use. > > Closes: https://github.com/Rust-for-Linux/linux/issues/1075 > Signed-off-by: Tamir Duberstein <tam...@gmail.com>
> -/// Creates a new [`CStr`] from a string literal. > +/// Creates a static C string wrapper at compile time. A C string *wrapper*? What do you mean? I would drop the word "wrapper" here. > -/// The string literal should not contain any `NUL` bytes. > +/// Rust supports C string literals since Rust 1.77, and they should be used > instead of this macro > +/// where possible. This macro exists to allow static *non-literal* C > strings to be created at > +/// compile time. This is most often used in other macros. > +/// > +/// # Panics > +/// > +/// This macro panics if the operand contains an interior `NUL` byte. > /// > /// # Examples > /// > /// ``` > -/// # use kernel::c_str; > +/// # use kernel::str_to_cstr; > /// # use kernel::str::CStr; > -/// const MY_CSTR: &CStr = c_str!("My awesome CStr!"); > +/// const MY_CSTR: &CStr = str_to_cstr!(concat!(file!(), ":", line!(), ": My > CStr!")); > /// ``` > #[macro_export] > -macro_rules! c_str { > +macro_rules! str_to_cstr { > + // NB: we could write `($str:lit) => compile_error!("use a C string > literal instead");` here but > + // that would trigger when the literal is at the top of several macro > expansions. That would be > + // too limiting to macro authors, so we rely on the name as a hint > instead. > ($str:expr) => {{ > const S: &str = concat!($str, "\0"); > const C: &$crate::str::CStr = match > $crate::str::CStr::from_bytes_with_nul(S.as_bytes()) { Alice