This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
109 lines (96 loc) · 2.92 KB
/
lib.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
pub trait ByteRepr {
const SIZE: usize;
type Bytes: AsRef<[u8]>;
// #![feature(associated_type_defaults)]
// type Bytes: AsRef<[u8]> = [u8; Self::SIZE];
fn from_bytes(bytes: Self::Bytes) -> Self;
fn to_bytes(&self) -> Self::Bytes;
}
impl<const N: usize> ByteRepr for [u8; N] {
const SIZE: usize = N;
type Bytes = [u8; N];
#[inline]
fn from_bytes(bytes: Self::Bytes) -> Self {
bytes
}
#[inline]
fn to_bytes(&self) -> Self::Bytes {
self.clone()
}
}
macro_rules! byterepr_num_impl {
($($ty:ty)*) => {$(
impl ByteRepr for $ty {
const SIZE: usize = core::mem::size_of::<Self>();
type Bytes = [u8; Self::SIZE];
#[inline]
fn from_bytes(bytes: Self::Bytes) -> Self {
Self::from_be_bytes(bytes)
}
#[inline]
fn to_bytes(&self) -> Self::Bytes {
self.to_be_bytes()
}
}
)*};
}
// should not implement for usize and isize
byterepr_num_impl!(u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 f32 f64);
#[macro_export]
macro_rules! byterepr_struct_impl {
{
$struct_name:ident {
$($field:ident: $ty:ty,)*
}
} => {
impl ByteRepr for $struct_name {
const SIZE: usize = { $(<$ty as ByteRepr>::SIZE + )* 0 };
type Bytes = [u8; Self::SIZE];
fn from_bytes(bytes: Self::Bytes) -> Self {
let mut offset: usize = 0;
$(let $field = {
let size = <$ty as ByteRepr>::SIZE;
let slice = bytes[offset..(offset + size)].try_into().unwrap();
let val = <$ty as ByteRepr>::from_bytes(slice);
offset += size;
val
};)*
assert_eq!(offset, Self::SIZE);
Self { $($field,)* }
}
fn to_bytes(&self) -> Self::Bytes {
let mut bytes = [0u8; Self::SIZE];
let mut offset: usize = 0;
$({
let size = <$ty as ByteRepr>::SIZE;
let slice = ByteRepr::to_bytes(&self.$field);
(&mut bytes[offset..(offset + size)]).copy_from_slice(slice.as_ref());
offset += size;
})*
assert_eq!(offset, Self::SIZE);
bytes
}
}
};
}
#[macro_export]
macro_rules! byterepr_struct {
{
$(#[$meta:meta])*
$pub:vis struct $struct_name:ident {
$(#[$field_meta:meta])*
$($field_pub:vis $field:ident: $ty:ty,)*
}
} => {
$(#[$meta])*
$pub struct $struct_name {
$(#[$field_meta])*
$($field_pub $field: $ty,)*
}
byterepr::byterepr_struct_impl! {
$struct_name {
$($field: $ty,)*
}
}
};
}