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///
114#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
115pub struct LittleEndian<T>(pub T);
116
117impl_from_bytes_for_int_wrapper! {
118	wrapper: LittleEndian;
119	conv_method: from_le_bytes;
120	ints:
121		(u8, U1),
122		(u16, U2),
123		(u32, U4),
124		(u64, U8),
125		(u128, U16),
126		(i8, U1),
127		(i16, U2),
128		(i32, U4),
129		(i64, U8),
130		(i128, U16),
131		(f32, U4),
132		(f64, U8),
133}
134impl_into_bytes_for_int_wrapper! {
135	wrapper: LittleEndian;
136	conv_method: to_le_bytes;
137	ints:
138		(u8, U1),
139		(u16, U2),
140		(u32, U4),
141		(u64, U8),
142		(u128, U16),
143		(i8, U1),
144		(i16, U2),
145		(i32, U4),
146		(i64, U8),
147		(i128, U16),
148		(f32, U4),
149		(f64, U8),
150}
151
152pub type Ne<T> = NativeEndian<T>;
153
154/// A wrapper type for native-endian integer parsing.
155///
156/// Notice that this encoding is not portable across different platforms with
157/// different endianness. Thus this wrapper should not be used for data that
158/// needs to be shared across different platforms.
159#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
160pub struct NativeEndian<T>(pub T);
161
162impl_from_bytes_for_int_wrapper! {
163	wrapper: NativeEndian;
164	conv_method: from_ne_bytes;
165	ints:
166		(u8, U1),
167		(u16, U2),
168		(u32, U4),
169		(u64, U8),
170		(u128, U16),
171		(i8, U1),
172		(i16, U2),
173		(i32, U4),
174		(i64, U8),
175		(i128, U16),
176		(f32, U4),
177		(f64, U8),
178}
179impl_into_bytes_for_int_wrapper! {
180	wrapper: NativeEndian;
181	conv_method: to_ne_bytes;
182	ints:
183		(u8, U1),
184		(u16, U2),
185		(u32, U4),
186		(u64, U8),
187		(u128, U16),
188		(i8, U1),
189		(i16, U2),
190		(i32, U4),
191		(i64, U8),
192		(i128, U16),
193		(f32, U4),
194		(f64, U8),
195}