1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Version16Dot16(u32);
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct MajorMinor {
pub major: u16,
pub minor: u16,
}
pub trait Compatible<Rhs = Self>: Sized {
fn compatible(&self, other: Rhs) -> bool;
}
impl Version16Dot16 {
pub const VERSION_0_5: Version16Dot16 = Version16Dot16::new(0, 5);
pub const VERSION_1_0: Version16Dot16 = Version16Dot16::new(1, 0);
pub const VERSION_2_0: Version16Dot16 = Version16Dot16::new(2, 0);
pub const VERSION_2_5: Version16Dot16 = Version16Dot16::new(2, 5);
pub const VERSION_3_0: Version16Dot16 = Version16Dot16::new(3, 0);
pub const fn new(major: u16, minor: u16) -> Self {
assert!(minor < 10, "minor version must be in the range [0, 9)");
let version = (major as u32) << 16 | (minor as u32) << 12;
Version16Dot16(version)
}
pub const fn to_major_minor(self) -> (u16, u16) {
let major = (self.0 >> 16) as u16;
let minor = ((self.0 & 0xFFFF) >> 12) as u16;
(major, minor)
}
#[inline]
pub const fn to_be_bytes(self) -> [u8; 4] {
self.0.to_be_bytes()
}
}
crate::newtype_scalar!(Version16Dot16, [u8; 4]);
impl MajorMinor {
pub const VERSION_1_0: MajorMinor = MajorMinor::new(1, 0);
pub const VERSION_1_1: MajorMinor = MajorMinor::new(1, 1);
pub const VERSION_1_2: MajorMinor = MajorMinor::new(1, 2);
pub const VERSION_1_3: MajorMinor = MajorMinor::new(1, 3);
pub const VERSION_2_0: MajorMinor = MajorMinor::new(2, 0);
#[inline]
pub const fn new(major: u16, minor: u16) -> Self {
MajorMinor { major, minor }
}
#[inline]
pub const fn to_be_bytes(self) -> [u8; 4] {
let [a, b] = self.major.to_be_bytes();
let [c, d] = self.minor.to_be_bytes();
[a, b, c, d]
}
}
impl crate::Scalar for MajorMinor {
type Raw = [u8; 4];
fn from_raw(raw: Self::Raw) -> Self {
let major = u16::from_be_bytes([raw[0], raw[1]]);
let minor = u16::from_be_bytes([raw[2], raw[3]]);
Self { major, minor }
}
fn to_raw(self) -> Self::Raw {
self.to_be_bytes()
}
}
impl Compatible for Version16Dot16 {
#[inline]
fn compatible(&self, other: Self) -> bool {
let (self_major, self_minor) = self.to_major_minor();
let (other_major, other_minor) = other.to_major_minor();
self_major == other_major && self_minor >= other_minor
}
}
impl Compatible for MajorMinor {
#[inline]
fn compatible(&self, other: Self) -> bool {
self.major == other.major && self.minor >= other.minor
}
}
impl Compatible for u16 {
#[inline]
fn compatible(&self, other: Self) -> bool {
*self >= other
}
}
impl std::fmt::Debug for Version16Dot16 {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Version16Dot16({:08x})", self.0)
}
}
impl std::fmt::Display for Version16Dot16 {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let (major, minor) = self.to_major_minor();
write!(f, "{major}.{minor}")
}
}
impl std::fmt::Display for MajorMinor {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let MajorMinor { major, minor } = self;
write!(f, "{major}.{minor}")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn version_smoke_test() {
assert_eq!(Version16Dot16(0x00005000).to_major_minor(), (0, 5));
assert_eq!(Version16Dot16(0x00011000).to_major_minor(), (1, 1));
assert_eq!(Version16Dot16::new(0, 5).0, 0x00005000);
assert_eq!(Version16Dot16::new(1, 1).0, 0x00011000);
}
}