mirror of
https://github.com/koverstreet/bcachefs-tools.git
synced 2025-02-23 00:00:02 +03:00
refactor: make c_str!
simpler, add test
Signed-off-by: Thomas Mühlbacher <tmuehlbacher@posteo.net>
This commit is contained in:
parent
0ca3233518
commit
15e3c90584
@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user