Struct zerovec::ule::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 + PartialOrd,

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,