font_types/
version.rs
1#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))]
11#[repr(transparent)]
12pub struct Version16Dot16(u32);
13
14#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
23#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
24#[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))]
25#[repr(C)]
26pub struct MajorMinor {
27 pub major: u16,
29 pub minor: u16,
31}
32
33pub trait Compatible<Rhs = Self>: Sized {
35 fn compatible(&self, other: Rhs) -> bool;
40}
41
42impl Version16Dot16 {
43 pub const VERSION_0_5: Version16Dot16 = Version16Dot16::new(0, 5);
45 pub const VERSION_1_0: Version16Dot16 = Version16Dot16::new(1, 0);
47 pub const VERSION_1_1: Version16Dot16 = Version16Dot16::new(1, 1);
49 pub const VERSION_2_0: Version16Dot16 = Version16Dot16::new(2, 0);
51 pub const VERSION_2_5: Version16Dot16 = Version16Dot16::new(2, 5);
53 pub const VERSION_3_0: Version16Dot16 = Version16Dot16::new(3, 0);
55
56 pub const fn new(major: u16, minor: u16) -> Self {
64 assert!(minor < 10, "minor version must be in the range [0, 9]");
65 let version = ((major as u32) << 16) | ((minor as u32) << 12);
66 Version16Dot16(version)
67 }
68
69 pub const fn to_major_minor(self) -> (u16, u16) {
71 let major = (self.0 >> 16) as u16;
72 let minor = ((self.0 & 0xFFFF) >> 12) as u16;
73 (major, minor)
74 }
75
76 #[inline]
78 pub const fn to_be_bytes(self) -> [u8; 4] {
79 self.0.to_be_bytes()
80 }
81}
82
83crate::newtype_scalar!(Version16Dot16, [u8; 4]);
84
85impl MajorMinor {
86 pub const VERSION_1_0: MajorMinor = MajorMinor::new(1, 0);
88 pub const VERSION_1_1: MajorMinor = MajorMinor::new(1, 1);
90 pub const VERSION_1_2: MajorMinor = MajorMinor::new(1, 2);
92 pub const VERSION_1_3: MajorMinor = MajorMinor::new(1, 3);
94 pub const VERSION_2_0: MajorMinor = MajorMinor::new(2, 0);
96
97 #[inline]
99 pub const fn new(major: u16, minor: u16) -> Self {
100 MajorMinor { major, minor }
101 }
102
103 #[inline]
105 pub const fn to_be_bytes(self) -> [u8; 4] {
106 let [a, b] = self.major.to_be_bytes();
107 let [c, d] = self.minor.to_be_bytes();
108 [a, b, c, d]
109 }
110}
111
112impl crate::Scalar for MajorMinor {
113 type Raw = [u8; 4];
114
115 fn from_raw(raw: Self::Raw) -> Self {
116 let major = u16::from_be_bytes([raw[0], raw[1]]);
117 let minor = u16::from_be_bytes([raw[2], raw[3]]);
118 Self { major, minor }
119 }
120
121 fn to_raw(self) -> Self::Raw {
122 self.to_be_bytes()
123 }
124}
125
126impl Compatible for Version16Dot16 {
127 #[inline]
128 fn compatible(&self, other: Self) -> bool {
129 let (self_major, self_minor) = self.to_major_minor();
130 let (other_major, other_minor) = other.to_major_minor();
131 self_major == other_major && self_minor >= other_minor
132 }
133}
134
135impl Compatible<(u16, u16)> for Version16Dot16 {
136 fn compatible(&self, other: (u16, u16)) -> bool {
137 self.compatible(Version16Dot16::new(other.0, other.1))
138 }
139}
140
141impl Compatible for MajorMinor {
142 #[inline]
143 fn compatible(&self, other: Self) -> bool {
144 self.major == other.major && self.minor >= other.minor
145 }
146}
147
148impl Compatible<(u16, u16)> for MajorMinor {
149 fn compatible(&self, other: (u16, u16)) -> bool {
150 self.compatible(MajorMinor::new(other.0, other.1))
151 }
152}
153
154impl Compatible for u16 {
155 #[inline]
156 fn compatible(&self, other: Self) -> bool {
157 *self >= other
158 }
159}
160
161impl std::fmt::Debug for Version16Dot16 {
162 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
163 write!(f, "Version16Dot16({:08x})", self.0)
164 }
165}
166
167impl std::fmt::Display for Version16Dot16 {
168 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
169 let (major, minor) = self.to_major_minor();
170 write!(f, "{major}.{minor}")
171 }
172}
173
174impl std::fmt::Display for MajorMinor {
175 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
176 let MajorMinor { major, minor } = self;
177 write!(f, "{major}.{minor}")
178 }
179}
180
181#[cfg(test)]
182mod tests {
183 use super::*;
184 #[test]
185 fn version_smoke_test() {
186 assert_eq!(Version16Dot16(0x00005000).to_major_minor(), (0, 5));
187 assert_eq!(Version16Dot16(0x00011000).to_major_minor(), (1, 1));
188 assert_eq!(Version16Dot16::new(0, 5).0, 0x00005000);
189 assert_eq!(Version16Dot16::new(1, 1).0, 0x00011000);
190 }
191
192 #[test]
193 #[should_panic]
194 fn minor_version_out_of_range_test() {
195 Version16Dot16::new(1, 10);
196 }
197}