ZString

Struct ZString 

Source
pub struct ZString { /* private fields */ }
Available on crate feature alloc only.
Expand description

Owning and non-null pointer to zero-terminated textual 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 a NonNull<u8>.
  • The wrapped pointer points at a sequence of valid-to-read non-zero byte values followed by at least one zero byte.
  • The ZString owns the data, and will free it on drop.

Implementations§

Source§

impl ZString

Source

pub unsafe fn new_unchecked(b: Box<str>) -> Self

Converts a Box<str> into a ZString without any additional checking.

§Safety
  • The data must have exactly one null byte at the end.
  • The data must not contain interior null bytes.

Breaking either of the above rules will cause the wrong amount to be freed when the ZString drops.

Source

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

Borrows this ZString as a ZStr.

Source

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

Gets the raw pointer to this data.

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.

Trait Implementations§

Source§

impl Clone for ZString

Source§

fn clone(&self) -> Self

Clones the value

//
let zstring1 = ZString::try_from("abc").unwrap();
let zstring2 = zstring1.clone();
assert!(zstring1.chars().eq(zstring2.chars()));
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ZString

Source§

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

Debug formats with outer " around the string.

let zstring = ZString::try_from("foo").unwrap();
let s = format!("{zstring:?}");
assert_eq!("\"foo\"", s);
Source§

impl Display for ZString

Source§

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

Display formats the string (without outer ").

let zstring = ZString::try_from("foo").unwrap();
let s = format!("{zstring}");
assert_eq!("foo", s);
Source§

impl Drop for ZString

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<ZStr<'_>> for ZString

Source§

fn from(value: ZStr<'_>) -> Self

This is like a “to owned’ style operation.

const FOO: ZStr<'static> = ZStr::from_lit("foo\0");
let zstring = ZString::from(FOO);
assert_eq!(zstring, FOO);
Source§

impl FromIterator<char> for ZString

Source§

fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl Hash for ZString

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 PartialEq<&str> for ZString

Source§

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

A ZStr equals a &str if the bytes match.

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 PartialEq<ZStr<'_>> for ZString

Source§

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

A ZString equals a ZStr by calling ZString::as_zstr

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>

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 PartialEq<ZString> for ZStr<'_>

Source§

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

A ZStr equals a ZString by calling ZString::as_zstr

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 PartialEq for ZString

Source§

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

Two ZString 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).

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 PartialOrd<&str> for ZString

Source§

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

Compares based on the byte sequence pointed to.

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 PartialOrd<ZStr<'_>> for ZString

Source§

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

Compares based on the byte sequence pointed to.

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>

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 PartialOrd<ZString> for ZStr<'_>

Source§

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

Compares based on the byte sequence pointed to.

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 PartialOrd for ZString

Source§

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

Compares based on the byte sequence pointed to.

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 Pointer for ZString

Source§

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

Formats the wrapped pointer value.

Source§

impl TryFrom<&str> for ZString

Source§

fn try_from(value: &str) -> Result<Self, Self::Error>

Trims any trailing nulls and then makes a ZString from what’s left.

let zstring1 = ZString::try_from("abc").unwrap();
assert!("abc".chars().eq(zstring1.chars()));

let zstring2 = ZString::try_from("foo\0\0\0\0").unwrap();
assert!("foo".chars().eq(zstring2.chars()));
§Failure
  • If there are any interior nulls.
assert!(ZString::try_from("ab\0c").is_err());
Source§

type Error = ZStringError

The type returned in the event of a conversion error.

Auto Trait Implementations§

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.