pub struct ArrayZString<const N: usize>(/* private fields */);Expand description
An array holding textual data that’s zero termianted.
This is a newtype over a byte array, with a const generic length N.
The bytes contained 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
- The
as_zstrmethod assumes that there’s a null somewhere before the end of the array. Safe code cannot break this rule, but unsafe code must be sure to maintain this invaraint. The array has sizeN, but onlyN-1of the bytes are usable, because there has to be at least one'\0'before the end of the array.
Implementations§
Source§impl<const N: usize> ArrayZString<N>
impl<const N: usize> ArrayZString<N>
Sourcepub const fn const_default() -> Self
pub const fn const_default() -> Self
Gives a zeroed array.
This is the same as default, but const fn.
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
View the data as a rust &str.
§Panics
- If somehow the bytes in the array aren’t utf-8 this will panic. Safe code cannot cause this to happen.
Sourcepub fn bytes(&self) -> impl Iterator<Item = u8> + '_
pub fn bytes(&self) -> impl Iterator<Item = u8> + '_
An iterator over the bytes of this ZStr.
- This iterator excludes the terminating 0 byte.
Trait Implementations§
Source§impl<const N: usize> Clone for ArrayZString<N>
impl<const N: usize> Clone for ArrayZString<N>
Source§fn clone(&self) -> ArrayZString<N>
fn clone(&self) -> ArrayZString<N>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<const N: usize> Debug for ArrayZString<N>
impl<const N: usize> Debug for ArrayZString<N>
Source§impl<const N: usize> Default for ArrayZString<N>
impl<const N: usize> Default for ArrayZString<N>
Source§impl<const N: usize> Display for ArrayZString<N>
impl<const N: usize> Display for ArrayZString<N>
Source§impl<const N: usize> Hash for ArrayZString<N>
impl<const N: usize> Hash for ArrayZString<N>
Source§impl<const N: usize> Ord for ArrayZString<N>
impl<const N: usize> Ord for ArrayZString<N>
Source§impl<const N: usize, const X: usize> PartialEq<ArrayZString<X>> for ArrayZString<N>
impl<const N: usize, const X: usize> PartialEq<ArrayZString<X>> for ArrayZString<N>
Source§fn eq(&self, other: &ArrayZString<X>) -> bool
fn eq(&self, other: &ArrayZString<X>) -> bool
Two ArrayZString are equal when the bytes “in” their strings are the
same, regardless of capacity differences.
assert_eq!(
ArrayZString::<6>::try_from("hello").unwrap(),
ArrayZString::<10>::try_from("hello").unwrap(),
);Source§impl<const N: usize> PartialEq<ZString> for ArrayZString<N>
Available on crate feature alloc only.
impl<const N: usize> PartialEq<ZString> for ArrayZString<N>
alloc only.Source§impl<const N: usize, const X: usize> PartialOrd<ArrayZString<X>> for ArrayZString<N>
impl<const N: usize, const X: usize> PartialOrd<ArrayZString<X>> for ArrayZString<N>
Source§fn partial_cmp(&self, other: &ArrayZString<X>) -> Option<Ordering>
fn partial_cmp(&self, other: &ArrayZString<X>) -> Option<Ordering>
Two ArrayZString are compared by the bytes “in” their strings,
regardless of capacity differences.
let abc = ArrayZString::<6>::try_from("abc").unwrap();
let def = ArrayZString::<10>::try_from("def").unwrap();
assert_eq!(abc.partial_cmp(&def), Some(Ordering::Less));Source§impl<const N: usize> PartialOrd<ZStr<'_>> for ArrayZString<N>
impl<const N: usize> PartialOrd<ZStr<'_>> for ArrayZString<N>
Source§impl<const N: usize> PartialOrd<ZString> for ArrayZString<N>
Available on crate feature alloc only.
impl<const N: usize> PartialOrd<ZString> for ArrayZString<N>
alloc only.Source§impl<const N: usize> TryFrom<&str> for ArrayZString<N>
impl<const N: usize> TryFrom<&str> for ArrayZString<N>
Source§fn try_from(value: &str) -> Result<Self, Self::Error>
fn try_from(value: &str) -> Result<Self, Self::Error>
Attempts to make an ArrayZString from a &str
let arr_str: ArrayZString<16> = ArrayZString::try_from("hello").unwrap();
assert_eq!(arr_str.as_str(), "hello");§Failure
The error type is unfortunately awkward here because 0.2 released with an
exhaustive error type. So instead we get an Option<ZStringError>, where
Some is an actual ZStringError and None indicates that there was
no zstring related issue, just a lack of capacity.
- Any number of trailing nulls are allowed, and will be trimmed.
- Interior nulls are not allowed (err:
Some(ZStringError::InteriorNulls)). - The trimmed byte length must be less than or equal to
N-1(err:None).
let interior_null_err: Option<ZStringError> =
ArrayZString::<16>::try_from("hel\0lo").unwrap_err();
assert_eq!(interior_null_err, Some(ZStringError::InteriorNulls));
// strings equal to or greater than the array size won't fit.
let capacity_err: Option<ZStringError> =
ArrayZString::<5>::try_from("hello").unwrap_err();
assert_eq!(capacity_err, None);
// if the array size exceeds the string size it will fit.
assert!(ArrayZString::<6>::try_from("hello").is_ok());