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
#[allow(unused_imports)]
use byteorder::{BigEndian, ByteOrder, LittleEndian, ReadBytesExt, WriteBytesExt};
use std::cell::{Ref, RefCell, RefMut};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use std::rc::Rc;
pub trait ModByteView<'a, T> {
fn val(&self) -> T;
fn set(&mut self, v: T);
}
pub trait ModMulByteView<'a, T, E> {
fn val(&self) -> T;
fn set(&mut self, v: T);
}
pub struct LitEnd;
pub struct BigEnd;
#[derive(Debug, PartialEq)]
pub struct ByteView<'a, T> {
val: &'a mut [u8],
_type: std::marker::PhantomData<T>,
}
impl<'a, T> ByteView<'a, T> {
pub fn mut_view(arr: &'a mut [u8]) -> (Self, &'a mut [u8]) {
let (val, leftover) = arr.split_at_mut(std::mem::size_of::<T>());
(
Self {
val,
_type: std::marker::PhantomData::<T>,
},
leftover,
)
}
}
#[macro_export]
macro_rules! impl_modbyteval {
($target:tt, $bytesized_type:tt) => {
impl<'a> ModByteView<'a, $bytesized_type> for $target<'a, $bytesized_type> {
fn val(&self) -> $bytesized_type {
self.val[0] as $bytesized_type
}
fn set(&mut self, v: $bytesized_type) {
self.val[0] = v as u8
}
}
};
}
impl_modbyteval!(ByteView, u8);
impl_modbyteval!(ByteView, i8);
#[derive(Debug, PartialEq)]
pub struct MulByteView<'a, T, E> {
val: &'a mut [u8],
_type: std::marker::PhantomData<T>,
_endian: std::marker::PhantomData<E>,
}
impl<'a, T, E> MulByteView<'a, T, E> {
pub fn mut_view(arr: &'a mut [u8]) -> (Self, &'a mut [u8]) {
let (val, leftover) = arr.split_at_mut(std::mem::size_of::<T>());
(
Self {
val,
_type: std::marker::PhantomData::<T>,
_endian: std::marker::PhantomData::<E>,
},
leftover,
)
}
}
#[macro_export]
macro_rules! impl_modmulbyteval {
($target:tt, $type:tt, $endian:tt, $endianident:ident, $read:ident, $write:ident) => {
impl<'a> ModMulByteView<'a, $type, $endian> for $target<'a, $type, $endian> {
fn val(&self) -> $type {
$endianident::$read(self.val)
}
fn set(&mut self, v: $type) {
$endianident::$write(self.val, v)
}
}
};
}
impl_modmulbyteval!(MulByteView, u16, LitEnd, LittleEndian, read_u16, write_u16);
impl_modmulbyteval!(MulByteView, u16, BigEnd, BigEndian, read_u16, write_u16);
impl_modmulbyteval!(MulByteView, u32, LitEnd, LittleEndian, read_u32, write_u32);
impl_modmulbyteval!(MulByteView, u32, BigEnd, BigEndian, read_u32, write_u32);
impl_modmulbyteval!(MulByteView, u64, LitEnd, LittleEndian, read_u64, write_u64);
impl_modmulbyteval!(MulByteView, u64, BigEnd, BigEndian, read_u64, write_u64);
impl_modmulbyteval!(
MulByteView,
u128,
LitEnd,
LittleEndian,
read_u128,
write_u128
);
impl_modmulbyteval!(MulByteView, u128, BigEnd, BigEndian, read_u128, write_u128);
impl_modmulbyteval!(MulByteView, i16, LitEnd, LittleEndian, read_i16, write_i16);
impl_modmulbyteval!(MulByteView, i16, BigEnd, BigEndian, read_i16, write_i16);
impl_modmulbyteval!(MulByteView, i32, LitEnd, LittleEndian, read_i32, write_i32);
impl_modmulbyteval!(MulByteView, i32, BigEnd, BigEndian, read_i32, write_i32);
impl_modmulbyteval!(MulByteView, i64, LitEnd, LittleEndian, read_i64, write_i64);
impl_modmulbyteval!(MulByteView, i64, BigEnd, BigEndian, read_i64, write_i64);
impl_modmulbyteval!(
MulByteView,
i128,
LitEnd,
LittleEndian,
read_i128,
write_i128
);
impl_modmulbyteval!(MulByteView, i128, BigEnd, BigEndian, read_i128, write_i128);
#[macro_export]
macro_rules! impl_oper_assign_overload {
($oper_name:ident, $bound:ident, $fname:ident, $oper:tt, $type:tt, $endian:tt) => {
impl<'a, $type, $endian> $oper_name<$type> for MulByteView<'a, $type, $endian>
where
MulByteView<'a, $type, $endian>: ModMulByteView<'a, $type, $endian>,
$type: $bound + $bound<Output = $type>,
{
fn $fname(&mut self, rhs: $type) {
self.set(self.val() $oper rhs)
}
}
impl<'a, $type> $oper_name<$type> for ByteView<'a, $type>
where
ByteView<'a, $type>: ModByteView<'a, $type>,
$type: $bound + $bound<Output = $type>,
{
fn $fname(&mut self, rhs: $type) {
self.set(self.val() $oper rhs)
}
}
};
}
impl_oper_assign_overload!(AddAssign, Add, add_assign, +, T, E);
impl_oper_assign_overload!(SubAssign, Sub, sub_assign, -, T, E);
impl_oper_assign_overload!(MulAssign, Mul, mul_assign, *, T, E);
impl_oper_assign_overload!(DivAssign, Div, div_assign, /, T, E);
#[derive(Debug, PartialEq)]
pub struct ArrayView<'a, T> {
buf: Rc<RefCell<&'a mut [u8]>>,
_type: std::marker::PhantomData<T>,
}
impl<'a, T> ArrayView<'a, T> {
pub fn mut_view(arr: &'a mut [u8]) -> (Self, &'a mut [u8]) {
let (val, leftover) = arr.split_at_mut(std::mem::size_of::<T>());
(
Self {
buf: Rc::new(RefCell::new(val)),
_type: std::marker::PhantomData::<T>,
},
leftover,
)
}
}
impl<'a, const L: usize> ArrayView<'a, [u8; L]> {
pub fn as_mut_ref(&self) -> RefMut<&'a mut [u8]> {
self.buf.borrow_mut()
}
pub fn as_ref(&self) -> Ref<&'a mut [u8]> {
self.buf.borrow()
}
pub fn rc_clone(&self) -> Rc<RefCell<&'a mut [u8]>> {
self.buf.clone()
}
pub fn set(&mut self, src: &[u8]) {
let mut dst = self.buf.borrow_mut();
for i in 0..L {
dst[i] = src[i]
}
}
}