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::{RawBytesULE, UnvalidatedChar, ULE};
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
§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 Bake for UnvalidatedChar
impl Bake for UnvalidatedChar
source§fn bake(&self, env: &CrateEnv) -> TokenStream
fn bake(&self, env: &CrateEnv) -> TokenStream
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<'de> Deserialize<'de> for UnvalidatedChar
impl<'de> Deserialize<'de> for UnvalidatedChar
This impl requires enabling the optional serde
Cargo feature of the zerovec
crate
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,
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§fn eq(&self, other: &UnvalidatedChar) -> bool
fn eq(&self, other: &UnvalidatedChar) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd for UnvalidatedChar
impl PartialOrd for UnvalidatedChar
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl Serialize for UnvalidatedChar
impl Serialize for UnvalidatedChar
This impl requires enabling the optional serde
Cargo feature of the zerovec
crate