Macro qed::const_cstr_from_bytes

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

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

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

§Examples

use core::ffi::CStr;

const ARRAY_CLASS: &[u8] = b"Array\0";
const ARRAY_CLASS_CSTR: &CStr = qed::const_cstr_from_bytes!(ARRAY_CLASS);

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

use core::ffi::CStr;

const BYTES: &[u8] = b"abc\0xyz";
const BYTES_CSTR: &CStr = qed::const_cstr_from_bytes!(BYTES);

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

use core::ffi::CStr;

const BYTES: &[u8] = b"Q.E.D.";
const BYTES_CSTR: &CStr = qed::const_cstr_from_bytes!(BYTES);

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

use core::ffi::CStr;

const EMPTY: &[u8] = b"";
const CSTR: &CStr = qed::const_cstr_from_bytes!(BYTES);