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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
pub trait Scalar {
type Raw: Copy + AsRef<[u8]>;
fn from_raw(raw: Self::Raw) -> Self;
fn to_raw(self) -> Self::Raw;
}
pub trait FixedSize: Sized {
const RAW_BYTE_LEN: usize;
}
pub trait ReadScalar: FixedSize {
fn read(bytes: &[u8]) -> Option<Self>;
}
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
pub struct BigEndian<T: Scalar>(pub(crate) T::Raw);
impl<T: Scalar> BigEndian<T> {
pub fn new(raw: T::Raw) -> BigEndian<T> {
BigEndian(raw)
}
pub fn get(&self) -> T {
T::from_raw(self.0)
}
pub fn set(&mut self, value: T) {
self.0 = value.to_raw();
}
pub fn be_bytes(&self) -> &[u8] {
self.0.as_ref()
}
}
impl<T: Scalar> From<T> for BigEndian<T> {
#[inline]
fn from(val: T) -> Self {
BigEndian(val.to_raw())
}
}
impl<T: Scalar + Default> Default for BigEndian<T> {
fn default() -> Self {
Self::from(T::default())
}
}
impl<T: Scalar + Copy + PartialEq> PartialEq<T> for BigEndian<T> {
fn eq(&self, other: &T) -> bool {
self.get() == *other
}
}
impl<const N: usize> FixedSize for [u8; N] {
const RAW_BYTE_LEN: usize = N;
}
impl<const N: usize> ReadScalar for [u8; N] {
#[inline]
fn read(bytes: &[u8]) -> Option<Self> {
bytes.try_into().ok()
}
}
impl<T> ReadScalar for BigEndian<T>
where
T: Scalar + FixedSize,
<T as Scalar>::Raw: ReadScalar,
{
#[inline]
fn read(bytes: &[u8]) -> Option<Self> {
T::Raw::read(bytes).map(BigEndian)
}
}
impl<T> ReadScalar for T
where
T: Scalar + FixedSize,
<T as Scalar>::Raw: ReadScalar,
{
#[inline]
fn read(bytes: &[u8]) -> Option<Self> {
BigEndian::<T>::read(bytes).as_ref().map(BigEndian::get)
}
}
impl<T> FixedSize for T
where
T: Scalar,
<T as Scalar>::Raw: FixedSize,
{
const RAW_BYTE_LEN: usize = <T as Scalar>::Raw::RAW_BYTE_LEN;
}
impl<T: Scalar + FixedSize> FixedSize for BigEndian<T> {
const RAW_BYTE_LEN: usize = T::RAW_BYTE_LEN;
}
#[macro_export]
macro_rules! newtype_scalar {
($ty:ident, $raw:ty) => {
impl $crate::raw::Scalar for $ty {
type Raw = $raw;
fn to_raw(self) -> $raw {
self.0.to_raw()
}
fn from_raw(raw: $raw) -> Self {
Self($crate::raw::Scalar::from_raw(raw))
}
}
};
}
macro_rules! int_scalar {
($ty:ty, $raw:ty) => {
impl crate::raw::Scalar for $ty {
type Raw = $raw;
fn to_raw(self) -> $raw {
self.to_be_bytes()
}
fn from_raw(raw: $raw) -> $ty {
Self::from_be_bytes(raw)
}
}
};
}
int_scalar!(u8, [u8; 1]);
int_scalar!(i8, [u8; 1]);
int_scalar!(u16, [u8; 2]);
int_scalar!(i16, [u8; 2]);
int_scalar!(u32, [u8; 4]);
int_scalar!(i32, [u8; 4]);
int_scalar!(i64, [u8; 8]);
int_scalar!(crate::Uint24, [u8; 3]);
impl<T: std::fmt::Debug + Scalar + Copy> std::fmt::Debug for BigEndian<T> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.get().fmt(f)
}
}
impl<T: std::fmt::Display + Scalar + Copy> std::fmt::Display for BigEndian<T> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.get().fmt(f)
}
}