1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/// Constructs a [`NulStr`] from a string literal,
/// truncating the string on internal nul bytes.
///
/// # Correctness
///
/// This truncates the passed in string if it contains nul bytes,
/// which means that silent truncation can happen with arbitrary inputs,
/// rather than compile-time errors.
///
/// # Example
///
/// ```rust
/// use abi_stable::nulstr_trunc;
///
/// assert_eq!(nulstr_trunc!("Huh?").to_str_with_nul(), "Huh?\0");
///
/// assert_eq!(nulstr_trunc!("Hello!").to_str_with_nul(), "Hello!\0");
///
/// assert_eq!(
/// nulstr_trunc!("Hello\0, world!").to_str_with_nul(),
/// "Hello\0"
/// );
///
/// ```
///
/// [`NulStr`]: ./sabi_types/struct.NulStr.html
#[macro_export]
macro_rules! nulstr_trunc {
($str:expr $(,)*) => {{
const __STR_NHPMWYD3NJA: $crate::sabi_types::NulStr<'_> =
$crate::sabi_types::NulStr::from_str($crate::pmr::concat!($str, "\0"));
__STR_NHPMWYD3NJA
}};
}
/// Constructs a [`NulStr`] from a string literal.
///
/// # Error
///
/// This causes a compile-time error if the input string contains nul byte(s).
///
/// # Example
///
/// ```rust
/// use abi_stable::nulstr;
///
/// assert_eq!( nulstr!("Huh?").to_str_with_nul(), "Huh?\0" );
/// assert_eq!( nulstr!("Hello!").to_str_with_nul(), "Hello!\0" );
/// ```
///
/// Nul bytes in the middle of the string cause compilation errors:
/// ```compile_fail
/// use abi_stable::nulstr;
///
/// assert_eq!(nulstr!("Hello\0, world!").to_str_with_nul(), "Hello\0");
/// ```
///
/// [`NulStr`]: ./sabi_types/struct.NulStr.html
#[macro_export]
macro_rules! nulstr {
($str:expr $(,)*) => {{
const __STR_NHPMWYD3NJA: $crate::sabi_types::NulStr<'_> =
$crate::sabi_types::NulStr::__try_from_str_unwrapping($crate::pmr::concat!($str, "\0"));
__STR_NHPMWYD3NJA
}};
}