Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: str: add `b_str!` macro

Add the `b_str!` macro, which creates a new `BStr` from
a string literal.

It is usable in const contexts, for instance:

const X: &BStr = b_str!("Example");

Signed-off-by: Gary Guo <gary@garyguo.net>
[Reworded, adapted for upstream and applied latest changes]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

authored by

Gary Guo and committed by
Miguel Ojeda
650ec515 7c597746

+21
+21
rust/kernel/str.rs
··· 9 9 /// `BStr` is simply an alias to `[u8]`, but has a more evident semantical meaning. 10 10 pub type BStr = [u8]; 11 11 12 + /// Creates a new [`BStr`] from a string literal. 13 + /// 14 + /// `b_str!` converts the supplied string literal to byte string, so non-ASCII 15 + /// characters can be included. 16 + /// 17 + /// # Examples 18 + /// 19 + /// ``` 20 + /// # use kernel::b_str; 21 + /// # use kernel::str::BStr; 22 + /// const MY_BSTR: &BStr = b_str!("My awesome BStr!"); 23 + /// ``` 24 + #[macro_export] 25 + macro_rules! b_str { 26 + ($str:literal) => {{ 27 + const S: &'static str = $str; 28 + const C: &'static $crate::str::BStr = S.as_bytes(); 29 + C 30 + }}; 31 + } 32 + 12 33 /// Allows formatting of [`fmt::Arguments`] into a raw buffer. 13 34 /// 14 35 /// It does not fail if callers write past the end of the buffer so that they can calculate the