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
impl UnvalidatedChar
Sourcepub const fn from_char(c: char) -> Self
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');Sourcepub fn try_to_char(self) -> Result<char, CharTryFromError>
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(_)));Sourcepub fn to_char_lossy(self) -> char
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);Sourcepub unsafe fn to_char_unchecked(self) -> char
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
impl AsULE for UnvalidatedChar
Source§type ULE = RawBytesULE<3>
type ULE = RawBytesULE<3>
Self. Read moreSource§fn to_unaligned(self) -> Self::ULE
fn to_unaligned(self) -> Self::ULE
Source§fn from_unaligned(unaligned: Self::ULE) -> Self
fn from_unaligned(unaligned: Self::ULE) -> Self
Source§impl Clone for UnvalidatedChar
impl Clone for UnvalidatedChar
Source§fn clone(&self) -> UnvalidatedChar
fn clone(&self) -> UnvalidatedChar
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for UnvalidatedChar
impl Debug for UnvalidatedChar
Source§impl From<char> for UnvalidatedChar
impl From<char> for UnvalidatedChar
Source§impl Hash for UnvalidatedChar
impl Hash for UnvalidatedChar
Source§impl Ord for UnvalidatedChar
impl Ord for UnvalidatedChar
Source§impl PartialEq for UnvalidatedChar
impl PartialEq for UnvalidatedChar
Source§impl PartialOrd for UnvalidatedChar
impl PartialOrd for UnvalidatedChar
Source§impl TryFrom<UnvalidatedChar> for char
impl TryFrom<UnvalidatedChar> for char
impl Copy for UnvalidatedChar
impl Eq for UnvalidatedChar
impl EqULE for UnvalidatedChar
impl StructuralPartialEq for UnvalidatedChar
Auto Trait Implementations§
impl Freeze for UnvalidatedChar
impl RefUnwindSafe for UnvalidatedChar
impl Send for UnvalidatedChar
impl Sync for UnvalidatedChar
impl Unpin for UnvalidatedChar
impl UnwindSafe for UnvalidatedChar
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> SliceAsULE for Twhere
T: EqULE,
impl<T> SliceAsULE for Twhere
T: EqULE,
impl<T> ErasedDestructor for Twhere
T: 'static,
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 3 bytes