Macro qed::const_cstr_from_str

source ·
macro_rules! const_cstr_from_str {
    ($str:expr $(,)?) => { ... };
}
Expand description

Construct a const CStr from the given str at compile time and assert that the given str bytes are a valid CStr (NUL terminated with no interior NUL bytes).

This macro emits a compile error if the given str contains any interior NUL bytes or does not have a NUL terminator.

§Examples

use core::ffi::CStr;

const ARRAY_CLASS_CSTR: &CStr = qed::const_cstr_from_str!("Array\0");

The following fails to compile because the str slice contains an interior NUL byte:

use core::ffi::CStr;

const CSTR: &CStr = qed::const_cstr_from_str!("abc\0xyz");

The following fails to compile because the str slice does not contain a NUL terminator:

use core::ffi::CStr;

const CSTR: &CStr = qed::const_cstr_from_str!("Q.E.D.");

The following fails to compile because the empty string is not a valid CStr:

use core::ffi::CStr;

const CSTR: &CStr = qed::const_cstr_from_str!("");