mirror of
https://gitee.com/bianbu-linux/linux-6.6
synced 2025-04-24 14:07:52 -04:00
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>
This commit is contained in:
parent
7c59774646
commit
650ec51561
1 changed files with 21 additions and 0 deletions
|
@ -9,6 +9,27 @@ use core::fmt;
|
||||||
/// `BStr` is simply an alias to `[u8]`, but has a more evident semantical meaning.
|
/// `BStr` is simply an alias to `[u8]`, but has a more evident semantical meaning.
|
||||||
pub type BStr = [u8];
|
pub type BStr = [u8];
|
||||||
|
|
||||||
|
/// Creates a new [`BStr`] from a string literal.
|
||||||
|
///
|
||||||
|
/// `b_str!` converts the supplied string literal to byte string, so non-ASCII
|
||||||
|
/// characters can be included.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use kernel::b_str;
|
||||||
|
/// # use kernel::str::BStr;
|
||||||
|
/// const MY_BSTR: &BStr = b_str!("My awesome BStr!");
|
||||||
|
/// ```
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! b_str {
|
||||||
|
($str:literal) => {{
|
||||||
|
const S: &'static str = $str;
|
||||||
|
const C: &'static $crate::str::BStr = S.as_bytes();
|
||||||
|
C
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
/// Allows formatting of [`fmt::Arguments`] into a raw buffer.
|
/// Allows formatting of [`fmt::Arguments`] into a raw buffer.
|
||||||
///
|
///
|
||||||
/// It does not fail if callers write past the end of the buffer so that they can calculate the
|
/// It does not fail if callers write past the end of the buffer so that they can calculate the
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue