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

Restrict a value to a certain interval. Read more
Sourceยง

impl PartialEq<PotentialCodePoint> for char

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 PartialEq<char> for PotentialCodePoint

Sourceยง

fn eq(&self, other: &char) -> 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 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<PotentialCodePoint> for char

Sourceยง

fn partial_cmp(&self, other: &PotentialCodePoint) -> 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 PartialOrd<char> for PotentialCodePoint

Sourceยง

fn partial_cmp(&self, other: &char) -> 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 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, dest: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> SliceAsULE for T
where T: EqULE,

Sourceยง

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

Sourceยง

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