pub struct ZStr<'a> { /* private fields */ }Expand description
Borrowed and non-null pointer to zero-terminated text data.
Because this is a thin pointer it’s suitable for direct FFI usage.
The bytes pointed to should be utf-8 encoded, but the CharDecoder used
to convert the bytes to char values is safe to use even when the bytes are
not proper utf-8.
§Safety
- This is
repr(transparent)over aNonNull<u8>. - The wrapped pointer points at a sequence of valid-to-read non-zero byte values followed by at least one zero byte.
- When you create a
ZStr<'a>value the pointer must be valid for at least as long as the lifetime'a.
Implementations§
Source§impl<'a> ZStr<'a>
impl<'a> ZStr<'a>
Sourcepub const fn from_lit(s: &'static str) -> ZStr<'static>
pub const fn from_lit(s: &'static str) -> ZStr<'static>
Makes a ZStr<'static> from a &'static str
This is intended for use with string litearls, but if you leak a runtime string into a static string I guess that works too.
const FOO: ZStr<'static> = ZStr::from_lit("foo\0");§Panics
- If
try_fromwould return an error, this will panic instead. Because this is intended for compile time constants, the panic will “just” trigger a build error.
Sourcepub fn bytes(self) -> impl Iterator<Item = u8> + 'a
pub fn bytes(self) -> impl Iterator<Item = u8> + 'a
An iterator over the bytes of this ZStr.
- This iterator excludes the terminating 0 byte.
Trait Implementations§
Source§impl PartialEq<ZStr<'_>> for ZStr<'_>
impl PartialEq<ZStr<'_>> for ZStr<'_>
Source§fn eq(&self, other: &ZStr<'_>) -> bool
fn eq(&self, other: &ZStr<'_>) -> bool
Two ZStr are considered equal if they point at the exact same byte
sequence.
This is much faster to compute when the bytes are valid UTF-8, though it is stricter if the bytes are not valid UTF-8 (the character replacement process during decoding could make two different byte sequences have the same character sequence).
const FOO1: ZStr<'static> = ZStr::from_lit("foo\0");
const FOO2: ZStr<'static> = ZStr::from_lit("foo\0");
assert_eq!(FOO1, FOO2);Source§impl PartialOrd<&str> for ZStr<'_>
impl PartialOrd<&str> for ZStr<'_>
Source§impl<const N: usize> PartialOrd<ZStr<'_>> for ArrayZString<N>
impl<const N: usize> PartialOrd<ZStr<'_>> for ArrayZString<N>
Source§impl PartialOrd<ZStr<'_>> for ZStr<'_>
impl PartialOrd<ZStr<'_>> for ZStr<'_>
Source§fn partial_cmp(&self, other: &ZStr<'_>) -> Option<Ordering>
fn partial_cmp(&self, other: &ZStr<'_>) -> Option<Ordering>
Compares based on the byte sequence pointed to.
const ABC: ZStr<'static> = ZStr::from_lit("abc\0");
const DEF: ZStr<'static> = ZStr::from_lit("def\0");
const GHI: ZStr<'static> = ZStr::from_lit("ghi\0");
assert_eq!(ABC.partial_cmp(&DEF), Some(Ordering::Less));
assert_eq!(DEF.partial_cmp(&GHI), Some(Ordering::Less));
assert_eq!(GHI.partial_cmp(&ABC), Some(Ordering::Greater));Source§impl PartialOrd<ZStr<'_>> for ZString
Available on crate feature alloc only.
impl PartialOrd<ZStr<'_>> for ZString
alloc only.Source§impl PartialOrd<ZString> for ZStr<'_>
Available on crate feature alloc only.
impl PartialOrd<ZString> for ZStr<'_>
alloc only.Source§impl<'a> TryFrom<&'a str> for ZStr<'a>
impl<'a> TryFrom<&'a str> for ZStr<'a>
Source§fn try_from(value: &'a str) -> Result<Self, Self::Error>
fn try_from(value: &'a str) -> Result<Self, Self::Error>
Converts the value in place.
The trailing nulls of the source &str will not “be in” the output
sequence of the returned ZStr.
let z1 = ZStr::try_from("abcd\0").unwrap();
assert!(z1.chars().eq("abcd".chars()));
let z2 = ZStr::try_from("abcd\0\0\0").unwrap();
assert!(z2.chars().eq("abcd".chars()));§Failure
- There must be at least one trailing null in the input
&str. - There must be no nulls followed by a non-null (“interior nulls”). This
second condition is not a strict requirement of the type, more of a
correctness lint. If interior nulls were allowed then
"ab\0cd\0"converted to aZStrwould only be read as"ab", and the second half of the string would effectively be erased.