diff options
author | Thomas Mühlbacher <tmuehlbacher@posteo.net> | 2024-05-31 12:27:56 +0200 |
---|---|---|
committer | Thomas Mühlbacher <tmuehlbacher@posteo.net> | 2024-05-31 12:48:42 +0200 |
commit | 15e3c90584e516dc50b453da20f0298c750a0c6a (patch) | |
tree | e8e630ffd8872803322c8dd0d3b5cc4e3e8646fa /src/commands | |
parent | 0ca323351841bc77d31bdc2bd0d396565c24a508 (diff) |
refactor: make `c_str!` simpler, add test
Signed-off-by: Thomas Mühlbacher <tmuehlbacher@posteo.net>
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/mod.rs | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 07c25966..e873a46d 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -27,13 +27,27 @@ enum Subcommands { Subvolume(subvolume::Cli), } +// FIXME: Can be removed after bumping MSRV >= 1.77 in favor of `c""` literals #[macro_export] macro_rules! c_str { ($lit:expr) => { - unsafe { - std::ffi::CStr::from_ptr(concat!($lit, "\0").as_ptr() as *const std::os::raw::c_char) - .to_bytes_with_nul() - .as_ptr() as *const std::os::raw::c_char - } + ::std::ffi::CStr::from_bytes_with_nul(concat!($lit, "\0").as_bytes()) + .unwrap() + .as_ptr() }; } + +#[cfg(test)] +mod tests { + use std::ffi::CStr; + + #[test] + fn check_cstr_macro() { + let literal = c_str!("hello"); + + assert_eq!( + literal, + CStr::from_bytes_with_nul(b"hello\0").unwrap().as_ptr() + ); + } +} |