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

source

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');
source

pub const fn from_u24(c: u32) -> Self

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

source

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(_)));
source

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

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');
source

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

This impl requires enabling the optional zerovec Cargo feature

source§

type ULE = RawBytesULE<3>

The ULE type corresponding to Self. Read more
source§

fn to_unaligned(self) -> Self::ULE

Converts from Self to Self::ULE. Read more
source§

fn from_unaligned(unaligned: Self::ULE) -> Self

Converts from Self::ULE to Self. Read more
source§

impl Bake for PotentialCodePoint

This impl requires enabling the optional databake Cargo feature

source§

fn bake(&self, env: &CrateEnv) -> TokenStream

Returns a TokenStream that would evaluate to self. Read more
source§

impl Clone for PotentialCodePoint

source§

fn clone(&self) -> PotentialCodePoint

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for PotentialCodePoint

source§

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

Formats the value using the given formatter. Read more
source§

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

Deserialize this value from the given Serde deserializer. Read more
source§

impl From<PotentialCodePoint> for u32

source§

fn from(x: PotentialCodePoint) -> Self

Converts to this type from the input type.
source§

impl From<char> for PotentialCodePoint

source§

fn from(value: char) -> Self

Converts to this type from the input type.
source§

impl Hash for PotentialCodePoint

source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for PotentialCodePoint

source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for PotentialCodePoint

source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for PotentialCodePoint

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for PotentialCodePoint

This impl requires enabling the optional serde Cargo feature

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl TryFrom<PotentialCodePoint> for char

source§

type Error = CharTryFromError

The type returned in the event of a conversion error.
source§

fn try_from(value: PotentialCodePoint) -> Result<char, Self::Error>

Performs the conversion.
source§

impl TryFrom<u32> for PotentialCodePoint

source§

type Error = ()

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl Copy for PotentialCodePoint

source§

impl Eq for PotentialCodePoint

source§

impl EqULE for PotentialCodePoint

This impl requires enabling the optional zerovec Cargo feature

source§

impl StructuralPartialEq for PotentialCodePoint

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> SliceAsULE for T
where T: EqULE,

§

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

Converts from &[Self] to &[Self::ULE] if possible. Read more
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

impl<T> ErasedDestructor for T
where T: 'static,