irox_bits/
codec.rs

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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// SPDX-License-Identifier: MIT
// Copyright 2024 IROX Contributors
//

//!
//! Bits encoding and decoding functions

use crate::{Error, MutBits};

/// Splits the input into two equal sized arrays.
pub const fn array_concat_2(a: [u8; 2], b: [u8; 2]) -> [u8; 4] {
    let [c, d] = b;
    let [a, b] = a;
    [a, b, c, d]
}
/// Splits the input into two equal sized arrays.
pub const fn array_split_2(a: [u8; 4]) -> ([u8; 2], [u8; 2]) {
    let [a, b, c, d] = a;
    ([a, b], [c, d])
}
/// Splits the input into two equal sized arrays.
pub const fn array_concat_4(a: [u8; 4], b: [u8; 4]) -> [u8; 8] {
    let [e, f, g, h] = b;
    let [a, b, c, d] = a;
    [a, b, c, d, e, f, g, h]
}
/// Splits the input into two equal sized arrays.
pub const fn array_split_4(a: [u8; 8]) -> ([u8; 4], [u8; 4]) {
    let [a, b, c, d, e, f, g, h] = a;
    ([a, b, c, d], [e, f, g, h])
}
/// Splits the input into two equal sized arrays.
pub const fn array_concat_8(a: [u8; 8], b: [u8; 8]) -> [u8; 16] {
    let [i, j, k, l, m, n, o, p] = b;
    let [a, b, c, d, e, f, g, h] = a;
    [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p]
}
/// Splits the input into two equal sized arrays.
pub const fn array_split_8(a: [u8; 16]) -> ([u8; 8], [u8; 8]) {
    let [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] = a;
    ([a, b, c, d, e, f, g, h], [i, j, k, l, m, n, o, p])
}
/// Splits the input into two equal sized arrays.
pub const fn array_concat_16(a: [u8; 16], b: [u8; 16]) -> [u8; 32] {
    let [q, r, s, t, u, v, w, x, y, z, aa, ab, ac, ad, ae, af] = b;
    let [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] = a;
    [
        a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, aa, ab, ac,
        ad, ae, af,
    ]
}
/// Splits the input into two equal sized arrays.
pub const fn array_split_16(a: [u8; 32]) -> ([u8; 16], [u8; 16]) {
    let [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, aa, ab, ac, ad, ae, af] =
        a;
    (
        [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p],
        [q, r, s, t, u, v, w, x, y, z, aa, ab, ac, ad, ae, af],
    )
}
/// Converts the value into a constant number of bytes
pub trait ToBEBytes<const N: usize> {
    fn to_be_bytes(&self) -> [u8; N];
}
/// Converts to the value from a constant number of bytes
pub trait FromBEBytes<const N: usize> {
    fn from_be_bytes(bytes: [u8; N]) -> Self;
}

impl ToBEBytes<1> for u8 {
    fn to_be_bytes(&self) -> [u8; 1] {
        [*self]
    }
}
impl FromBEBytes<1> for u8 {
    fn from_be_bytes(bytes: [u8; 1]) -> u8 {
        bytes[0]
    }
}

impl ToBEBytes<2> for u16 {
    fn to_be_bytes(&self) -> [u8; 2] {
        u16::to_be_bytes(*self)
    }
}
impl FromBEBytes<2> for u16 {
    fn from_be_bytes(bytes: [u8; 2]) -> u16 {
        u16::from_be_bytes(bytes)
    }
}
impl ToBEBytes<4> for u32 {
    fn to_be_bytes(&self) -> [u8; 4] {
        u32::to_be_bytes(*self)
    }
}

impl ToBEBytes<8> for [u32; 2] {
    fn to_be_bytes(&self) -> [u8; 8] {
        let [a, b] = *self;
        array_concat_4(u32::to_be_bytes(a), u32::to_be_bytes(b))
    }
}

impl FromBEBytes<4> for u32 {
    fn from_be_bytes(bytes: [u8; 4]) -> u32 {
        u32::from_be_bytes(bytes)
    }
}
impl FromBEBytes<8> for [u32; 2] {
    fn from_be_bytes(bytes: [u8; 8]) -> [u32; 2] {
        let (a, b) = array_split_4(bytes);
        [u32::from_be_bytes(a), u32::from_be_bytes(b)]
    }
}
impl ToBEBytes<4> for f32 {
    fn to_be_bytes(&self) -> [u8; 4] {
        f32::to_be_bytes(*self)
    }
}
impl FromBEBytes<4> for f32 {
    fn from_be_bytes(bytes: [u8; 4]) -> f32 {
        f32::from_be_bytes(bytes)
    }
}
impl ToBEBytes<8> for [f32; 2] {
    fn to_be_bytes(&self) -> [u8; 8] {
        let [a, b] = *self;
        array_concat_4(f32::to_be_bytes(a), f32::to_be_bytes(b))
    }
}
impl FromBEBytes<8> for [f32; 2] {
    fn from_be_bytes(bytes: [u8; 8]) -> [f32; 2] {
        let (a, b) = array_split_4(bytes);
        [f32::from_be_bytes(a), f32::from_be_bytes(b)]
    }
}

impl ToBEBytes<8> for u64 {
    fn to_be_bytes(&self) -> [u8; 8] {
        u64::to_be_bytes(*self)
    }
}
impl FromBEBytes<8> for u64 {
    fn from_be_bytes(bytes: [u8; 8]) -> u64 {
        u64::from_be_bytes(bytes)
    }
}

impl ToBEBytes<8> for f64 {
    fn to_be_bytes(&self) -> [u8; 8] {
        f64::to_be_bytes(*self)
    }
}
impl FromBEBytes<8> for f64 {
    fn from_be_bytes(bytes: [u8; 8]) -> f64 {
        f64::from_be_bytes(bytes)
    }
}
impl ToBEBytes<16> for [f64; 2] {
    fn to_be_bytes(&self) -> [u8; 16] {
        let [a, b] = *self;
        array_concat_8(a.to_be_bytes(), b.to_be_bytes())
    }
}
impl FromBEBytes<16> for [f64; 2] {
    fn from_be_bytes(bytes: [u8; 16]) -> [f64; 2] {
        let (a, b) = array_split_8(bytes);
        [f64::from_be_bytes(a), f64::from_be_bytes(b)]
    }
}

impl ToBEBytes<16> for [u64; 2] {
    fn to_be_bytes(&self) -> [u8; 16] {
        let [a, b] = *self;
        array_concat_8(u64::to_be_bytes(a), u64::to_be_bytes(b))
    }
}
impl FromBEBytes<16> for [u64; 2] {
    fn from_be_bytes(bytes: [u8; 16]) -> [u64; 2] {
        let (a, b) = array_split_8(bytes);
        [u64::from_be_bytes(a), u64::from_be_bytes(b)]
    }
}

impl ToBEBytes<16> for u128 {
    fn to_be_bytes(&self) -> [u8; 16] {
        u128::to_be_bytes(*self)
    }
}
impl FromBEBytes<16> for u128 {
    fn from_be_bytes(bytes: [u8; 16]) -> u128 {
        u128::from_be_bytes(bytes)
    }
}
impl ToBEBytes<32> for [u128; 2] {
    fn to_be_bytes(&self) -> [u8; 32] {
        let [a, b] = *self;
        array_concat_16(u128::to_be_bytes(a), u128::to_be_bytes(b))
    }
}
impl FromBEBytes<32> for [u128; 2] {
    fn from_be_bytes(bytes: [u8; 32]) -> [u128; 2] {
        let (a, b) = array_split_16(bytes);
        [u128::from_be_bytes(a), u128::from_be_bytes(b)]
    }
}

/// Writes 'self' to the provided [`MutBits`] impl in big endian order.
pub trait WriteToBEBits {
    fn write_be_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<(), Error>;
}

impl WriteToBEBits for u8 {
    fn write_be_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<(), Error> {
        bits.write_u8(*self)
    }
}
impl WriteToBEBits for u16 {
    fn write_be_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<(), Error> {
        bits.write_be_u16(*self)
    }
}

impl WriteToBEBits for u32 {
    fn write_be_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<(), Error> {
        bits.write_be_u32(*self)
    }
}
impl WriteToBEBits for u64 {
    fn write_be_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<(), Error> {
        bits.write_be_u64(*self)
    }
}
impl WriteToBEBits for u128 {
    fn write_be_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<(), Error> {
        bits.write_be_u128(*self)
    }
}
impl WriteToBEBits for f32 {
    fn write_be_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<(), Error> {
        bits.write_f32(*self)
    }
}
impl WriteToBEBits for f64 {
    fn write_be_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<(), Error> {
        bits.write_f64(*self)
    }
}
impl WriteToBEBits for i8 {
    fn write_be_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<(), Error> {
        bits.write_i8(*self)
    }
}
impl WriteToBEBits for i16 {
    fn write_be_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<(), Error> {
        bits.write_be_i16(*self)
    }
}
impl WriteToBEBits for i32 {
    fn write_be_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<(), Error> {
        bits.write_be_i32(*self)
    }
}
impl WriteToBEBits for i64 {
    fn write_be_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<(), Error> {
        bits.write_be_i64(*self)
    }
}
impl WriteToBEBits for i128 {
    fn write_be_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<(), Error> {
        bits.write_be_i128(*self)
    }
}