Struct potential_utf::PotentialCodePoint
source · pub struct PotentialCodePoint(/* private fields */);
Expand description
A 24-bit numeric data type 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 potential_utf::PotentialCodePoint;
assert_eq!(PotentialCodePoint::from_u24(0x68).try_to_char(), Ok('h'));
assert_eq!(PotentialCodePoint::from_char('i').try_to_char(), Ok('i'));
assert_eq!(PotentialCodePoint::from_u24(0x1F44B).try_to_char(), Ok('👋'));
assert!(PotentialCodePoint::from_u24(0xDE01).try_to_char().is_err());
assert_eq!(PotentialCodePoint::from_u24(0xDE01).to_char_lossy(), char::REPLACEMENT_CHARACTER);
Implementations§
source§impl PotentialCodePoint
impl PotentialCodePoint
sourcepub const fn from_char(c: char) -> Self
pub const fn from_char(c: char) -> Self
Create a PotentialCodePoint
from a char
.
§Examples
use potential_utf::PotentialCodePoint;
let a = PotentialCodePoint::from_char('a');
assert_eq!(a.try_to_char().unwrap(), 'a');
sourcepub const fn from_u24(c: u32) -> Self
pub const fn from_u24(c: u32) -> Self
Create PotentialCodePoint
from a u32 value, ignoring the most significant 8 bits.
sourcepub fn try_to_char(self) -> Result<char, CharTryFromError>
pub fn try_to_char(self) -> Result<char, CharTryFromError>
Attempt to convert a PotentialCodePoint
to a char
.
§Examples
use potential_utf::PotentialCodePoint;
use zerovec::ule::AsULE;
let a = PotentialCodePoint::from_char('a');
assert_eq!(a.try_to_char(), Ok('a'));
let b = PotentialCodePoint::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 PotentialCodePoint
to a char', returning [
char::REPLACEMENT_CHARACTER] if the
PotentialCodePoint` does not represent a valid Unicode scalar value.
§Examples
use potential_utf::PotentialCodePoint;
use zerovec::ule::AsULE;
let a = PotentialCodePoint::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 PotentialCodePoint
to a char
without checking that it is
a valid Unicode scalar value.
§Safety
The PotentialCodePoint
must be a valid Unicode scalar value in little-endian order.
§Examples
use potential_utf::PotentialCodePoint;
let a = PotentialCodePoint::from_char('a');
assert_eq!(unsafe { a.to_char_unchecked() }, 'a');
sourcepub const fn to_unaligned(self) -> RawBytesULE<3>
pub const fn to_unaligned(self) -> RawBytesULE<3>
For converting to the ULE type in a const context
Can be removed once const traits are a thing
Trait Implementations§
source§impl AsULE for PotentialCodePoint
impl AsULE for PotentialCodePoint
This impl requires enabling the optional zerovec
Cargo feature
source§impl Bake for PotentialCodePoint
impl Bake for PotentialCodePoint
This impl requires enabling the optional databake
Cargo feature
source§fn bake(&self, env: &CrateEnv) -> TokenStream
fn bake(&self, env: &CrateEnv) -> TokenStream
source§impl Clone for PotentialCodePoint
impl Clone for PotentialCodePoint
source§fn clone(&self) -> PotentialCodePoint
fn clone(&self) -> PotentialCodePoint
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for PotentialCodePoint
impl Debug for PotentialCodePoint
source§impl<'de> Deserialize<'de> for PotentialCodePoint
impl<'de> Deserialize<'de> for PotentialCodePoint
This impl requires enabling the optional serde
Cargo feature
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<PotentialCodePoint> for u32
impl From<PotentialCodePoint> for u32
source§fn from(x: PotentialCodePoint) -> Self
fn from(x: PotentialCodePoint) -> Self
source§impl From<char> for PotentialCodePoint
impl From<char> for PotentialCodePoint
source§impl Hash for PotentialCodePoint
impl Hash for PotentialCodePoint
source§impl Ord for PotentialCodePoint
impl Ord for PotentialCodePoint
source§impl PartialEq for PotentialCodePoint
impl PartialEq for PotentialCodePoint
source§impl PartialOrd for PotentialCodePoint
impl PartialOrd for PotentialCodePoint
source§impl Serialize for PotentialCodePoint
impl Serialize for PotentialCodePoint
This impl requires enabling the optional serde
Cargo feature
source§impl TryFrom<PotentialCodePoint> for char
impl TryFrom<PotentialCodePoint> for char
source§impl TryFrom<u32> for PotentialCodePoint
impl TryFrom<u32> for PotentialCodePoint
impl Copy for PotentialCodePoint
impl Eq for PotentialCodePoint
impl EqULE for PotentialCodePoint
This impl requires enabling the optional zerovec
Cargo feature
impl StructuralPartialEq for PotentialCodePoint
Auto Trait Implementations§
impl Freeze for PotentialCodePoint
impl RefUnwindSafe for PotentialCodePoint
impl Send for PotentialCodePoint
impl Sync for PotentialCodePoint
impl Unpin for PotentialCodePoint
impl UnwindSafe for PotentialCodePoint
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more