diff options
author | Gary Guo <[email protected]> | 2022-11-10 17:41:31 +0100 |
---|---|---|
committer | Miguel Ojeda <[email protected]> | 2022-12-04 01:59:15 +0100 |
commit | b18cb00e5a8a1182ef491b770ea1a3dab081dc5b (patch) | |
tree | f5ba092679cebe1498a6f359260dd6cc7b0a3632 | |
parent | 985f1f09bf5bf8cbcb43e58a2320de2a5054be4c (diff) |
rust: str: add `c_str!` macro
Add `c_str!`, which is a convenience macro that creates a new `CStr`
from a string literal.
It is designed to be similar to a `str` in usage, and it is usable
in const contexts, for instance:
const X: &CStr = c_str!("Example");
Co-developed-by: Alex Gaynor <[email protected]>
Signed-off-by: Alex Gaynor <[email protected]>
Signed-off-by: Gary Guo <[email protected]>
[Reworded, adapted for upstream and applied latest changes]
Signed-off-by: Miguel Ojeda <[email protected]>
-rw-r--r-- | rust/kernel/str.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 3ed685cb5a3c..a995db36486f 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -321,6 +321,29 @@ where } } +/// Creates a new [`CStr`] from a string literal. +/// +/// The string literal should not contain any `NUL` bytes. +/// +/// # Examples +/// +/// ``` +/// # use kernel::c_str; +/// # use kernel::str::CStr; +/// const MY_CSTR: &CStr = c_str!("My awesome CStr!"); +/// ``` +#[macro_export] +macro_rules! c_str { + ($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"), + }; + C + }}; +} + #[cfg(test)] mod tests { use super::*; |