null_terminated_str/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(docsrs, feature(doc_auto_cfg))]
3
4#[cfg(feature = "serde")]
5mod serde_impl;
6
7mod borrowed;
8pub use borrowed::NullTerminatedStr;
9
10mod owned;
11pub use owned::{NullStringFromUtf8Error, NullTerminatedString};
12
13mod into_null_terminated_string;
14pub use into_null_terminated_string::IntoNullTerminatedString;
15
16/// Create a null-terminated utf-8 str as an rvalue.
17/// Appends a NUL byte to the passed string.
18///
19/// ```rust
20/// use null_terminated_str::{
21///     const_null_terminated_str,
22///     NullTerminatedStr
23/// };
24/// use std::ops::Deref;
25///
26/// const S: &NullTerminatedStr = const_null_terminated_str!("Hello, World!");
27/// assert_eq!(S.deref(), "Hello, World!");
28/// ```
29///
30/// If the `str` contains NULL bytes, then the compilation
31/// would fail.
32///
33/// ```rust,compile_fail
34/// use null_terminated_str::{
35///     const_null_terminated_str,
36///     NullTerminatedStr
37/// };
38/// use std::ops::Deref;
39///
40/// const S: &NullTerminatedStr = const_null_terminated_str!("Hello,\0 World!");
41/// assert_eq!(S.deref(), "Hello, World!");
42/// ```
43#[macro_export]
44macro_rules! const_null_terminated_str {
45    ($strval:expr) => {
46        $crate::NullTerminatedStr::from_const_str(concat!($strval, "\0"))
47    };
48}