devela/num/primitive/
join.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// devela::data::conversion::cast::join
//
//! construct an unsigned primitive by joining an array of smaller unsigned primitives.
//
// TOC
// - trait definition
// - trait implementation
// - wrapper implementations

use crate::{paste, Cast};

/// Offers methods to construct a primitive from an array or slice of smaller primitives.
///
/// Methods expecting an array are more efficient than the ones expecting an
/// slice. On the other hand slices of any lenght are supported as follows:
/// - If the slice contains fewer elements than required, the method will
///   fill in the missing values with zeros.
/// - If the slice contains more elements than required, the method will
///   ignore the extra elements.
///
/// See also the [`Cast`] type for the equivalent *const* methods, and the
/// [`PrimitiveSplit`][super::PrimitiveSplit] trait for the opposite operations.
pub trait PrimitiveJoin<T, U, const LEN: usize> {
    /// Constructs a primitive `T` from an array of `U` in big-endian order.
    #[must_use]
    fn from_array_be(values: [U; LEN]) -> T;
    /// Constructs a primitive `T` from an array of `U` in little-endian order.
    #[must_use]
    fn from_array_le(values: [U; LEN]) -> T;
    /// Constructs a primitive `T` from an array of `U` in native-endian order.
    #[must_use]
    fn from_array_ne(values: [U; LEN]) -> T;

    /// Constructs a primitive `T` from a slice of `U` in big-endian order.
    #[must_use]
    fn from_slice_be(values: &[U]) -> T;
    /// Constructs a primitive `T` from a slice of `U` in little-endian order.
    #[must_use]
    fn from_slice_le(values: &[U]) -> T;
    /// Constructs a primitive `T` from a slice of `U` in native-endian order.
    #[must_use]
    fn from_slice_ne(values: &[U]) -> T;
}

/// Implements the trait methods.
macro_rules! impl_from_trait {
    ( $( $T:ident, $U:ident, $LEN:literal );+ $(;)? ) => {
        $( impl_from_trait![@$T, $U, $LEN]; )+
    };
    (@$T:ident, $U:ident, $LEN:literal) => { paste! {
        impl PrimitiveJoin<$T, $U, $LEN> for $T {
            fn from_array_be(values: [$U; $LEN]) -> $T { Cast::<$T>::[<from_ $U _be>](values) }
            fn from_array_le(values: [$U; $LEN]) -> $T { Cast::<$T>::[<from_ $U _le>](values) }
            fn from_array_ne(values: [$U; $LEN]) -> $T { Cast::<$T>::[<from_ $U _ne>](values) }
            fn from_slice_be(values: &[$U]) -> $T {
                let mut array = [0; $LEN];
                for (i, &v) in values.iter().enumerate() {
                    array[i] = v;
                }
                Cast::<$T>::[<from_ $U _be>](array)
            }
            fn from_slice_le(values: &[$U]) -> $T {
                let mut array = [0; $LEN];
                for (i, &v) in values.iter().enumerate() {
                    array[i] = v;
                }
                Cast::<$T>::[<from_ $U _le>](array)
            }
            fn from_slice_ne(values: &[$U]) -> $T {
                let mut array = [0; $LEN];
                for (i, &v) in values.iter().enumerate() {
                    array[i] = v;
                }
                Cast::<$T>::[<from_ $U _ne>](array)
            }
        }
    }};
}
impl_from_trait![
    u128, u64, 2; u128, u32, 4; u128, u16, 8; u128, u8, 16;
    u64, u32, 2; u64, u16, 4; u64, u8, 8;
    u32, u16, 2; u32, u8, 4;
    u16, u8, 2;
];

/* implements the Cast wrapper methods */

#[rustfmt::skip]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "join")))]
impl Cast<u16> {
    /// Constructs a `u16` from an array of `[u8; 2]` in big-endian order.
    #[must_use]
    pub const fn from_u8_be(v: [u8; 2]) -> u16 { u16::from_be_bytes(v) }

    /// Constructs a `u16` from an array of `[u8; 2]` in little-endian order.
    #[must_use]
    pub const fn from_u8_le(v: [u8; 2]) -> u16 { u16::from_le_bytes(v) }

    /// Constructs a `u16` from an array of `[u8; 2]` in native-endian order.
    #[must_use]
    pub const fn from_u8_ne(v: [u8; 2]) -> u16 { u16::from_ne_bytes(v) }
}

#[rustfmt::skip]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "join")))]
impl Cast<u32> {
    /// Constructs a `u32` from an array of `[u16; 2]` in big-endian order.
    #[must_use]
    pub const fn from_u16_be(v: [u16; 2]) -> u32 {
        ((v[0] as u32) << 16) | (v[1] as u32)
    }

    /// Constructs a `u32` from an array of `[u16; 2]` in little-endian order.
    #[must_use]
    pub const fn from_u16_le(v: [u16; 2]) -> u32 {
        ((v[1] as u32) << 16) | (v[0] as u32)
    }

    /// Constructs a `u32` from an array of `[u16; 2]` in native-endian order.
    #[must_use]
    pub const fn from_u16_ne(v: [u16; 2]) -> u32 {
        if cfg!(target_endian = "big") {
            Cast::<u32>::from_u16_be(v)
        } else {
            Cast::<u32>::from_u16_le(v)
        }
    }

    /// Constructs a `u32` from an array of `[u8; 4]` in big-endian order.
    #[must_use]
    pub const fn from_u8_be(v: [u8; 4]) -> u32 { u32::from_be_bytes(v) }

    /// Constructs a `u32` from an array of `[u8; 4]` in little-endian order.
    #[must_use]
    pub const fn from_u8_le(v: [u8; 4]) -> u32 { u32::from_le_bytes(v) }

    /// Constructs a `u32` from an array of `[u8; 4]` in native-endian order.
    #[must_use]
    pub const fn from_u8_ne(v: [u8; 4]) -> u32 { u32::from_ne_bytes(v) }
}

#[rustfmt::skip]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "join")))]
impl Cast<u64> {
    /// Constructs a `u64` from an array of `[u32; 2]` in big-endian order.
    #[must_use]
    pub const fn from_u32_be(v: [u32; 2]) -> u64 {
        ((v[0] as u64) << 32) | (v[1] as u64)
    }

    /// Constructs a `u64` from an array of `[u32; 2]` in little-endian order.
    #[must_use]
    pub const fn from_u32_le(v: [u32; 2]) -> u64 {
        ((v[1] as u64) << 32) | (v[0] as u64)
    }

    /// Constructs a `u64` from an array of `[u32; 2]` in native-endian order.
    #[must_use]
    pub const fn from_u32_ne(v: [u32; 2]) -> u64 {
        if cfg!(target_endian = "big") {
            Cast::<u64>::from_u32_be(v)
        } else {
            Cast::<u64>::from_u32_le(v)
        }
    }

    /// Constructs a `u64` from an array of `[u16; 4]` in big-endian order.
    #[must_use]
    pub const fn from_u16_be(v: [u16; 4]) -> u64 {
        ((v[0] as u64) << (16 * 3))
            | ((v[1] as u64) << (16 * 2))
            | ((v[2] as u64) << 16)
            | (v[3] as u64)
    }

    /// Constructs a `u64` from an array of `[u16; 4]` in little-endian order.
    #[must_use]
    pub const fn from_u16_le(v: [u16; 4]) -> u64 {
        ((v[3] as u64) << (16 * 3))
            | ((v[2] as u64) << (16 * 2))
            | ((v[1] as u64) << 16)
            | (v[0] as u64)
    }

    /// Constructs a `u64` from an array of `[u16; 4]` in native-endian order.
    #[must_use]
    pub const fn from_u16_ne(v: [u16; 4]) -> u64 {
        if cfg!(target_endian = "big") {
            Cast::<u64>::from_u16_be(v)
        } else {
            Cast::<u64>::from_u16_le(v)
        }
    }

    /// Constructs a `u64` from an array of `[u8; 8]` in big-endian order.
    #[must_use]
    pub const fn from_u8_be(v: [u8; 8]) -> u64 { u64::from_be_bytes(v) }

    /// Constructs a `u64` from an array of `[u8; 8]` in little-endian order.
    #[must_use]
    pub const fn from_u8_le(v: [u8; 8]) -> u64 { u64::from_le_bytes(v) }

    /// Constructs a `u64` from an array of `[u8; 8]` in native-endian order.
    #[must_use]
    pub const fn from_u8_ne(v: [u8; 8]) -> u64 { u64::from_ne_bytes(v) }
}

#[rustfmt::skip]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "join")))]
impl Cast<u128> {
    /// Constructs a `u128` from an array of `[u64; 2]` in big-endian order.
    #[must_use]
    pub const fn from_u64_be(v: [u64; 2]) -> u128 {
        ((v[0] as u128) << 64) | (v[1] as u128)
    }

    /// Constructs a `u128` from an array of `[u64; 2]` in little-endian order.
    #[must_use]
    pub const fn from_u64_le(v: [u64; 2]) -> u128 {
        ((v[1] as u128) << 64) | (v[0] as u128)
    }

    /// Constructs a `u128` from an array of `[u64; 2]` in native-endian order.
    #[must_use]
    pub const fn from_u64_ne(v: [u64; 2]) -> u128 {
        if cfg!(target_endian = "big") {
            Cast::<u128>::from_u64_be(v)
        } else {
            Cast::<u128>::from_u64_le(v)
        }
    }

    /// Constructs a `u128` from an array of `[u32; 4]` in big-endian order.
    #[must_use]
    pub const fn from_u32_be(v: [u32; 4]) -> u128 {
        ((v[0] as u128) << (32 * 3))
            | ((v[1] as u128) << (32 * 2))
            | ((v[2] as u128) << 32)
            | (v[3] as u128)
    }

    /// Constructs a `u128` from an array of `[u32; 4]` in little-endian order.
    #[must_use]
    pub const fn from_u32_le(v: [u32; 4]) -> u128 {
        ((v[3] as u128) << (32 * 3))
            | ((v[2] as u128) << (32 * 2))
            | ((v[1] as u128) << 32)
            | (v[0] as u128)
    }

    /// Constructs a `u128` from an array of `[u32; 4]` in native-endian order.
    #[must_use]
    pub const fn from_u32_ne(v: [u32; 4]) -> u128 {
        if cfg!(target_endian = "big") {
            Cast::<u128>::from_u32_be(v)
        } else {
            Cast::<u128>::from_u32_le(v)
        }
    }

    /// Constructs a `u128` from an array of `[u16; 8]` in big-endian order.
    #[must_use]
    pub const fn from_u16_be(v: [u16; 8]) -> u128 {
        ((v[0] as u128) << (16 * 7))
            | ((v[1] as u128) << (16 * 6))
            | ((v[2] as u128) << (16 * 5))
            | ((v[3] as u128) << (16 * 4))
            | ((v[4] as u128) << (16 * 3))
            | ((v[5] as u128) << (16 * 2))
            | ((v[6] as u128) << 16)
            | (v[7] as u128)
    }

    /// Constructs a `u128` from an array of `[u16; 8]` in little-endian order.
    #[must_use]
    pub const fn from_u16_le(v: [u16; 8]) -> u128 {
        ((v[7] as u128) << (16 * 7))
            | ((v[6] as u128) << (16 * 6))
            | ((v[5] as u128) << (16 * 5))
            | ((v[4] as u128) << (16 * 4))
            | ((v[3] as u128) << (16 * 3))
            | ((v[2] as u128) << (16 * 2))
            | ((v[1] as u128) << 16)
            | (v[0] as u128)
    }

    /// Constructs a `u128` from an array of `[u16; 8]` in native-endian order.
    #[must_use]
    pub const fn from_u16_ne(v: [u16; 8]) -> u128 {
        if cfg!(target_endian = "big") {
            Cast::<u128>::from_u16_be(v)
        } else {
            Cast::<u128>::from_u16_le(v)
        }
    }

    /// Constructs a `u128` from an array of `[u8; 16]` in big-endian order.
    #[must_use]
    pub const fn from_u8_be(v: [u8; 16]) -> u128 { u128::from_be_bytes(v) }

    /// Constructs a `u128` from an array of `[u8; 16]` in little-endian order.
    #[must_use]
    pub const fn from_u8_le(v: [u8; 16]) -> u128 { u128::from_le_bytes(v) }

    /// Constructs a `u128` from an array of `[u8; 16]` in native-endian order.
    #[must_use]
    pub const fn from_u8_ne(v: [u8; 16]) -> u128 { u128::from_ne_bytes(v) }
}