array_fusion/bytes/
mod.rs

1//! Byte oriented array utils.
2//!
3//! This module provides various utilities to encode and decode byte-aware
4//! date types from and into arrays.
5//!
6//! Also see the [`encoding`] sub-module for converting data types into byte
7//! arrays, and [`decoding`] sub-module for converting byte arrays into data
8//! types.
9//!
10
11
12use hybrid_array::AssocArraySize;
13use hybrid_array::sizes::U1;
14use hybrid_array::sizes::U2;
15use hybrid_array::sizes::U4;
16use hybrid_array::sizes::U8;
17use hybrid_array::sizes::U16;
18
19
20
21pub mod decoding;
22pub mod encoding;
23
24
25
26type SizeOf<T, const N: usize> = <[T; N] as AssocArraySize>::Size;
27
28
29
30macro_rules! impl_from_bytes_for_int_wrapper {
31	{
32		wrapper: $name:ident;
33		conv_method: $conv_method:ident;
34		ints: $( ( $int:ident , $size:ident ) ),* $(,)? $(;)?
35	} => {
36		$(
37			impl $crate::bytes::decoding::FromByteArray for $name<$int> {
38				type Size = $size;
39
40				fn from_bytes(array: $crate::Array<u8, Self::Size>) -> Self {
41					$name($int::$conv_method(array.0))
42				}
43			}
44		)*
45	};
46}
47
48macro_rules! impl_into_bytes_for_int_wrapper {
49	{
50		wrapper: $name:ident;
51		conv_method: $conv_method:ident;
52		ints: $( ( $int:ident , $size:ident ) ),* $(,)? $(;)?
53	} => {
54		$(
55			impl $crate::bytes::encoding::IntoByteArray for $name<$int> {
56				type Size = $size;
57
58				fn into_bytes(self) -> $crate::Array<u8, Self::Size> {
59					let value = self.0;
60					$crate::Array(value.$conv_method())
61				}
62			}
63		)*
64	};
65}
66
67
68pub type Be<T> = BigEndian<T>;
69pub type Net<T> = BigEndian<T>;
70
71/// A wrapper type for big-endian integer parsing.
72#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
73pub struct BigEndian<T>(pub T);
74
75impl_from_bytes_for_int_wrapper! {
76	wrapper: BigEndian;
77	conv_method: from_be_bytes;
78	ints:
79		(u8, U1),
80		(u16, U2),
81		(u32, U4),
82		(u64, U8),
83		(u128, U16),
84		(i8, U1),
85		(i16, U2),
86		(i32, U4),
87		(i64, U8),
88		(i128, U16),
89		(f32, U4),
90		(f64, U8),
91}
92impl_into_bytes_for_int_wrapper! {
93	wrapper: BigEndian;
94	conv_method: to_be_bytes;
95	ints:
96		(u8, U1),
97		(u16, U2),
98		(u32, U4),
99		(u64, U8),
100		(u128, U16),
101		(i8, U1),
102		(i16, U2),
103		(i32, U4),
104		(i64, U8),
105		(i128, U16),
106		(f32, U4),
107		(f64, U8),
108}
109
110pub type Le<T> = LittleEndian<T>;
111
112/// A wrapper type for little-endian integer parsing.
113#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
114pub struct LittleEndian<T>(pub T);
115
116impl_from_bytes_for_int_wrapper! {
117	wrapper: LittleEndian;
118	conv_method: from_le_bytes;
119	ints:
120		(u8, U1),
121		(u16, U2),
122		(u32, U4),
123		(u64, U8),
124		(u128, U16),
125		(i8, U1),
126		(i16, U2),
127		(i32, U4),
128		(i64, U8),
129		(i128, U16),
130		(f32, U4),
131		(f64, U8),
132}
133impl_into_bytes_for_int_wrapper! {
134	wrapper: LittleEndian;
135	conv_method: to_le_bytes;
136	ints:
137		(u8, U1),
138		(u16, U2),
139		(u32, U4),
140		(u64, U8),
141		(u128, U16),
142		(i8, U1),
143		(i16, U2),
144		(i32, U4),
145		(i64, U8),
146		(i128, U16),
147		(f32, U4),
148		(f64, U8),
149}
150
151pub type Ne<T> = NativeEndian<T>;
152
153/// A wrapper type for native-endian integer parsing.
154#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
155pub struct NativeEndian<T>(pub T);
156
157impl_from_bytes_for_int_wrapper! {
158	wrapper: NativeEndian;
159	conv_method: from_ne_bytes;
160	ints:
161		(u8, U1),
162		(u16, U2),
163		(u32, U4),
164		(u64, U8),
165		(u128, U16),
166		(i8, U1),
167		(i16, U2),
168		(i32, U4),
169		(i64, U8),
170		(i128, U16),
171		(f32, U4),
172		(f64, U8),
173}
174impl_into_bytes_for_int_wrapper! {
175	wrapper: NativeEndian;
176	conv_method: to_ne_bytes;
177	ints:
178		(u8, U1),
179		(u16, U2),
180		(u32, U4),
181		(u64, U8),
182		(u128, U16),
183		(i8, U1),
184		(i16, U2),
185		(i32, U4),
186		(i64, U8),
187		(i128, U16),
188		(f32, U4),
189		(f64, U8),
190}