Struct zerovec::ule::UnvalidatedChar
source · #[repr(transparent)]pub struct UnvalidatedChar([u8; 3]);
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);
Tuple Fields§
§0: [u8; 3]
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>
The ULE type corresponding to
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
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
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
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)