[PATCH v10 4/5] rust: replace `kernel::c_str!` with C-Strings
Tamir Duberstein
tamird at gmail.com
Mon May 26 22:29:46 UTC 2025
On Mon, May 26, 2025 at 11:04 AM Benno Lossin <lossin at kernel.org> wrote:
>
> On Sat May 24, 2025 at 10:33 PM CEST, Tamir Duberstein wrote:
> > +macro_rules! c_str_avoid_literals {
>
> I don't like this name, how about `concat_to_c_str` or
> `concat_with_nul`?
>
> This macro also is useful from macros that have a normal string literal,
> but can't turn it into a `c""` one.
Uh, can you give an example? I'm not attached to the name.
>
> > + // 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()) {
> > - Ok(v) => v,
> > - Err(_) => panic!("string contains interior NUL"),
> > - };
> > + const S: &'static str = concat!($str, "\0");
> > + const C: &'static $crate::str::CStr =
> > + match $crate::str::CStr::from_bytes_with_nul(S.as_bytes()) {
>
> Why is this still our CStr?
Good question. I'll just revert all the changes here, I don't need to
touch this.
>
> > + Ok(v) => v,
> > + Err(err) => {
> > + let _: core::ffi::FromBytesWithNulError = err;
>
> Is this really necessary?
No. Reverted in v11.
More information about the dri-devel
mailing list