zerovec::ule

Struct UnvalidatedChar

Source
pub struct UnvalidatedChar(/* private fields */);
Expand description

A u8 array of little-endian data that is expected to be a Unicode scalar value, but is not validated as such.

Use this type instead of char when you want to deal with data that is expected to be valid Unicode scalar values, but you want control over when or if you validate that assumption.

ยงExamples

use zerovec::ule::UnvalidatedChar;
use zerovec::{ZeroSlice, ZeroVec};

// data known to be little-endian three-byte chunks of valid Unicode scalar values
let data = [0x68, 0x00, 0x00, 0x69, 0x00, 0x00, 0x4B, 0xF4, 0x01];
// ground truth expectation
let real = ['h', 'i', '๐Ÿ‘‹'];

let chars: &ZeroSlice<UnvalidatedChar> = ZeroSlice::parse_byte_slice(&data).expect("invalid data length");
let parsed: Vec<_> = chars.iter().map(|c| unsafe { c.to_char_unchecked() }).collect();
assert_eq!(&parsed, &real);

let real_chars: ZeroVec<_> = real.iter().copied().map(UnvalidatedChar::from_char).collect();
let serialized_data = chars.as_bytes();
assert_eq!(serialized_data, &data);

Implementationsยง

Sourceยง

impl UnvalidatedChar

Source

pub const fn from_char(c: char) -> Self

Create a UnvalidatedChar from a char.

ยงExamples
use zerovec::ule::UnvalidatedChar;

let a = UnvalidatedChar::from_char('a');
assert_eq!(a.try_to_char().unwrap(), 'a');
Source

pub fn try_to_char(self) -> Result<char, CharTryFromError>

Attempt to convert a UnvalidatedChar to a char.

ยงExamples
use zerovec::ule::{AsULE, UnvalidatedChar};

let a = UnvalidatedChar::from_char('a');
assert_eq!(a.try_to_char(), Ok('a'));

let b = UnvalidatedChar::from_unaligned([0xFF, 0xFF, 0xFF].into());
assert!(matches!(b.try_to_char(), Err(_)));
Source

pub fn to_char_lossy(self) -> char

Convert a UnvalidatedChar to a char', returning [char::REPLACEMENT_CHARACTER] if the UnvalidatedChar` does not represent a valid Unicode scalar value.

ยงExamples
use zerovec::ule::{AsULE, UnvalidatedChar};

let a = UnvalidatedChar::from_unaligned([0xFF, 0xFF, 0xFF].into());
assert_eq!(a.to_char_lossy(), char::REPLACEMENT_CHARACTER);
Source

pub unsafe fn to_char_unchecked(self) -> char

Convert a UnvalidatedChar to a char without checking that it is a valid Unicode scalar value.

ยงSafety

The UnvalidatedChar must be a valid Unicode scalar value in little-endian order.

ยงExamples
use zerovec::ule::UnvalidatedChar;

let a = UnvalidatedChar::from_char('a');
assert_eq!(unsafe { a.to_char_unchecked() }, 'a');

Trait Implementationsยง

Sourceยง

impl AsULE for UnvalidatedChar

Sourceยง

type ULE = RawBytesULE<3>

The ULE type corresponding to Self. Read more
Sourceยง

fn to_unaligned(self) -> Self::ULE

Converts from Self to Self::ULE. Read more
Sourceยง

fn from_unaligned(unaligned: Self::ULE) -> Self

Converts from Self::ULE to Self. Read more
Sourceยง

impl Clone for UnvalidatedChar

Sourceยง

fn clone(&self) -> UnvalidatedChar

Returns a copy 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 Debug for UnvalidatedChar

Sourceยง

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

Formats the value using the given formatter. Read more
Sourceยง

impl From<char> for UnvalidatedChar

Sourceยง

fn from(value: char) -> Self

Converts to this type from the input type.
Sourceยง

impl Hash for UnvalidatedChar

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 Ord for UnvalidatedChar

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

Sourceยง

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

Tests for self and other values to be equal, and is used by ==.
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 for UnvalidatedChar

Sourceยง

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

This method returns an ordering between self and other values if one exists. Read more
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 TryFrom<UnvalidatedChar> for char

Sourceยง

type Error = CharTryFromError

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

fn try_from(value: UnvalidatedChar) -> Result<char, Self::Error>

Performs the conversion.
Sourceยง

impl Copy for UnvalidatedChar

Sourceยง

impl Eq for UnvalidatedChar

Sourceยง

impl EqULE for UnvalidatedChar

Sourceยง

impl StructuralPartialEq for UnvalidatedChar

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, dst: *mut T)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> SliceAsULE for T
where T: EqULE,

Sourceยง

fn slice_to_unaligned(slice: &[T]) -> Option<&[<T as AsULE>::ULE]>

Converts from &[Self] to &[Self::ULE] if possible. Read more
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, 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.
Sourceยง

impl<T> ErasedDestructor for T
where T: 'static,