#[allow(unused_macros)]
#[macro_export]
macro_rules! cstr {
($str:literal) => {{
assert!(
!$str.bytes().any(|b| b == b'\0'),
"cstr argument contains embedded NUL bytes",
);
#[allow(unsafe_code, unused_unsafe)]
{
unsafe {
$crate::ffi::CStr::from_bytes_with_nul_unchecked(concat!($str, "\0").as_bytes())
}
}
}};
}
#[test]
fn test_cstr() {
use crate::ffi::CString;
use alloc::borrow::ToOwned;
assert_eq!(cstr!(""), &*CString::new("").unwrap());
assert_eq!(cstr!("").to_owned(), CString::new("").unwrap());
assert_eq!(cstr!("hello"), &*CString::new("hello").unwrap());
assert_eq!(cstr!("hello").to_owned(), CString::new("hello").unwrap());
}
#[test]
#[should_panic]
fn test_invalid_cstr() {
let _ = cstr!("hello\0world");
}
#[test]
#[should_panic]
fn test_invalid_empty_cstr() {
let _ = cstr!("\0");
}