ArrayZString

Struct ArrayZString 

Source
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_zstr method 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 size N, but only N-1 of 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>

Source

pub const fn const_default() -> Self

Gives a zeroed array.

This is the same as default, but const fn.

Source

pub const fn as_zstr(&self) -> ZStr<'_>

Gets a ZStr to this data.

§Panics
  • If the length N is zero, this will panic.
Source

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.
Source

pub fn bytes(&self) -> impl Iterator<Item = u8> + '_

An iterator over the bytes of this ZStr.

  • This iterator excludes the terminating 0 byte.
Source

pub fn chars(&self) -> impl Iterator<Item = char> + '_

An iterator over the decoded char values of this ZStr.

Source

pub const fn as_ptr(self) -> *const u8

Gets the raw pointer to this data.

Trait Implementations§

Source§

impl<const N: usize> Clone for ArrayZString<N>

Source§

fn clone(&self) -> ArrayZString<N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize> Debug for ArrayZString<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Debug formats with outer " around the string.

let arr_str: ArrayZString<16> = ArrayZString::try_from("foo").unwrap();
let s = format!("{arr_str:?}");
assert_eq!("\"foo\"", s);
Source§

impl<const N: usize> Default for ArrayZString<N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const N: usize> Display for ArrayZString<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Display formats the string (without outer ").

let arr_str: ArrayZString<16> = ArrayZString::try_from("foo").unwrap();
let s = format!("{arr_str}");
assert_eq!("foo", s);
Source§

impl<const N: usize> Hash for ArrayZString<N>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<const N: usize> Ord for ArrayZString<N>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<const N: usize, const X: usize> PartialEq<ArrayZString<X>> for ArrayZString<N>

Source§

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(),
);
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize> PartialEq<ZStr<'_>> for ArrayZString<N>

Source§

fn eq(&self, other: &ZStr<'_>) -> bool

An ArrayZString<N> equals a ZStr by bytes.

1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize> PartialEq<ZString> for ArrayZString<N>

Available on crate feature alloc only.
Source§

fn eq(&self, other: &ZString) -> bool

An ArrayZString<N> equals a ZString when they contain the same bytes.

1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize, const X: usize> PartialOrd<ArrayZString<X>> for ArrayZString<N>

Source§

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));
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<const N: usize> PartialOrd<ZStr<'_>> for ArrayZString<N>

Source§

fn partial_cmp(&self, other: &ZStr<'_>) -> Option<Ordering>

An ArrayZString<N> compares to a ZStr by bytes.

1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<const N: usize> PartialOrd<ZString> for ArrayZString<N>

Available on crate feature alloc only.
Source§

fn partial_cmp(&self, other: &ZString) -> Option<Ordering>

An ArrayZString<N> compares to a ZString by bytes.

1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<const N: usize> TryFrom<&str> for ArrayZString<N>

Source§

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());
Source§

type Error = Option<ZStringError>

The type returned in the event of a conversion error.
Source§

impl<const N: usize> Copy for ArrayZString<N>

Source§

impl<const N: usize> Eq for ArrayZString<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for ArrayZString<N>

§

impl<const N: usize> RefUnwindSafe for ArrayZString<N>

§

impl<const N: usize> Send for ArrayZString<N>

§

impl<const N: usize> Sync for ArrayZString<N>

§

impl<const N: usize> Unpin for ArrayZString<N>

§

impl<const N: usize> UnwindSafe for ArrayZString<N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.