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!(b.try_to_char().is_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');Trait Implementationsยง
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 more