Skip to main content

ttf_view/tables/cmap/
codepoint.rs

1use crate::util::impl_fmt_with;
2
3#[derive(Copy, Hash)]
4#[derive_const(Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
5pub struct Codepoint(u32);
6
7impl Codepoint {
8    pub const fn new(value: u32) -> Self {
9        Self(value)
10    }
11    pub const fn get(&self) -> u32 {
12        self.0
13    }
14}
15
16impl_fmt_with! {
17    Debug, Display, Binary, Octal, LowerHex, UpperHex, LowerExp, UpperExp:
18    |this: &Codepoint| this.get()
19}
20
21// Conversions from std integer types to Codepoint
22const impl From<u8> for Codepoint {
23    fn from(value: u8) -> Self {
24        Self::new(value as u32)
25    }
26}
27const impl From<u16> for Codepoint {
28    fn from(value: u16) -> Self {
29        Self::new(value as u32)
30    }
31}
32const impl From<u32> for Codepoint {
33    fn from(value: u32) -> Self {
34        Self::new(value)
35    }
36}
37const impl TryFrom<usize> for Codepoint {
38    type Error = ();
39    fn try_from(value: usize) -> Result<Self, Self::Error> {
40        value.try_into().or(Err(())).map(Self::new)
41    }
42}
43
44// Conversions from Codepoint to std integer types
45const impl TryFrom<Codepoint> for u8 {
46    type Error = ();
47    fn try_from(value: Codepoint) -> Result<Self, Self::Error> {
48        value.get().try_into().or(Err(()))
49    }
50}
51const impl TryFrom<Codepoint> for u16 {
52    type Error = ();
53    fn try_from(value: Codepoint) -> Result<Self, Self::Error> {
54        value.get().try_into().or(Err(()))
55    }
56}
57const impl From<Codepoint> for u32 {
58    fn from(value: Codepoint) -> Self {
59        value.get()
60    }
61}
62const impl From<Codepoint> for usize {
63    fn from(value: Codepoint) -> Self {
64        value.get() as usize
65    }
66}