Struct PotentialCodePoint

struct PotentialCodePoint(_)

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

impl PotentialCodePoint

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');
const fn from_u24(c: u32) -> Self

Create PotentialCodePoint from a u32 value, ignoring the most significant 8 bits.

fn try_to_char(self: 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(_)));
fn to_char_lossy(self: Self) -> char

Convert a PotentialCodePoint to a char', returning [char::REPLACEMENT_CHARACTER](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);
unsafe fn to_char_unchecked(self: 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');
const fn to_unaligned(self: Self) -> RawBytesULE<3>

For converting to the ULE type in a const context

Can be removed once const traits are a thing

impl AsULE for PotentialCodePoint

fn to_unaligned(self: Self) -> <Self as >::ULE
fn from_unaligned(unaligned: <Self as >::ULE) -> Self

impl Clone for PotentialCodePoint

fn clone(self: &Self) -> PotentialCodePoint

impl Copy for PotentialCodePoint

impl Debug for PotentialCodePoint

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl Eq for PotentialCodePoint

impl EqULE for PotentialCodePoint

impl Freeze for PotentialCodePoint

impl From for PotentialCodePoint

fn from(value: char) -> Self

impl Hash for PotentialCodePoint

fn hash<__H: $crate::hash::Hasher>(self: &Self, state: &mut __H)

impl Ord for PotentialCodePoint

fn cmp(self: &Self, other: &Self) -> Ordering

impl PartialEq for PotentialCodePoint

fn eq(self: &Self, other: &char) -> bool

impl PartialEq for PotentialCodePoint

fn eq(self: &Self, other: &PotentialCodePoint) -> bool

impl PartialOrd for PotentialCodePoint

fn partial_cmp(self: &Self, other: &Self) -> Option<Ordering>

impl PartialOrd for PotentialCodePoint

fn partial_cmp(self: &Self, other: &char) -> Option<Ordering>

impl RefUnwindSafe for PotentialCodePoint

impl Send for PotentialCodePoint

impl StructuralPartialEq for PotentialCodePoint

impl Sync for PotentialCodePoint

impl TryFrom for PotentialCodePoint

fn try_from(x: u32) -> Result<Self, ()>

impl Unpin for PotentialCodePoint

impl UnsafeUnpin for PotentialCodePoint

impl UnwindSafe for PotentialCodePoint

impl<T> Any for PotentialCodePoint

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for PotentialCodePoint

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for PotentialCodePoint

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for PotentialCodePoint

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> ErasedDestructor for PotentialCodePoint

impl<T> From for PotentialCodePoint

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SliceAsULE for PotentialCodePoint

fn slice_to_unaligned(slice: &[T]) -> Option<&[<T as AsULE>::ULE]>

impl<T> ToOwned for PotentialCodePoint

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for PotentialCodePoint

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for PotentialCodePoint

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for PotentialCodePoint

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>