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>
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
)