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
use core::convert::TryInto;
use crate::endian::Endian;
pub trait View {
fn read<E>(&self, offset: usize) -> E
where
E: Endian,
[(); E::NBYTES]:;
fn write<E>(&mut self, offset: usize, value: E)
where
E: Endian,
[u8; E::NBYTES]:;
}
impl View for [u8] {
#[inline(always)]
fn read<T>(&self, offset: usize) -> T
where
T: Endian,
[u8; T::NBYTES]:,
{
#[cfg(not(any(feature = "BE", feature = "NE")))]
return T::from_bytes_le(self[offset..offset + T::NBYTES].try_into().unwrap());
#[cfg(feature = "BE")]
return T::from_bytes_be(self[offset..offset + T::NBYTES].try_into().unwrap());
#[cfg(feature = "NE")]
return T::from_bytes_ne(self[offset..offset + T::NBYTES].try_into().unwrap());
}
#[inline(always)]
fn write<T>(&mut self, offset: usize, value: T)
where
T: Endian,
[(); T::NBYTES]:,
{
#[cfg(not(any(feature = "BE", feature = "NE")))]
self[offset..offset + T::NBYTES].copy_from_slice(&value.to_bytes_le());
#[cfg(feature = "BE")]
self[offset..offset + T::NBYTES].copy_from_slice(&value.to_bytes_be());
#[cfg(feature = "NE")]
self[offset..offset + T::NBYTES].copy_from_slice(&value.to_bytes_ne());
}
}