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
#[macro_export]
macro_rules! make_array_newtype {
($name:ident, $ty:ty, $len:expr) => {
pub struct $name([$ty; $len]);
impl $name {
pub fn new(source: [$ty; $len]) -> Self {
$name(source)
}
pub fn into_inner(self) -> [$ty; $len] {
self.0
}
}
impl Clone for $name {
fn clone(&self) -> $name {
let &$name(ref dat) = self;
$name(dat.clone())
}
}
impl Copy for $name {}
impl PartialEq for $name {
fn eq(&self, other: &$name) -> bool {
&self[..] == &other[..]
}
}
impl Eq for $name {}
impl PartialOrd for $name {
fn partial_cmp(&self, other: &$name) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for $name {
fn cmp(&self, other: &$name) -> core::cmp::Ordering {
self.0.cmp(&other.0)
}
}
impl core::ops::Index<usize> for $name {
type Output = $ty;
fn index(&self, index: usize) -> &$ty {
let &$name(ref dat) = self;
&dat[index]
}
}
impl core::ops::Index<core::ops::Range<usize>> for $name {
type Output = [$ty];
fn index(&self, index: core::ops::Range<usize>) -> &[$ty] {
let &$name(ref dat) = self;
&dat[index]
}
}
impl core::ops::Index<core::ops::RangeTo<usize>> for $name {
type Output = [$ty];
fn index(&self, index: core::ops::RangeTo<usize>) -> &[$ty] {
let &$name(ref dat) = self;
&dat[index]
}
}
impl core::ops::Index<core::ops::RangeFrom<usize>> for $name {
type Output = [$ty];
fn index(&self, index: core::ops::RangeFrom<usize>) -> &[$ty] {
let &$name(ref dat) = self;
&dat[index]
}
}
impl core::ops::Index<core::ops::RangeFull> for $name {
type Output = [$ty];
fn index(&self, _: core::ops::RangeFull) -> &[$ty] {
let &$name(ref dat) = self;
&dat[..]
}
}
impl core::fmt::Debug for $name {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
write!(f, "{}([", stringify!($name))?;
write!(f, "{:?}", self.0[0])?;
for item in self.0[1..].iter() {
write!(f, ", {:?}", item)?;
}
write!(f, "])")
}
}
impl bytesrepr::ToBytes for $name {
fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
self.0.to_bytes()
}
fn serialized_length(&self) -> usize {
self.0.serialized_length()
}
}
impl bytesrepr::FromBytes for $name {
fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
let (dat, rem) = <[$ty; $len]>::from_bytes(bytes)?;
Ok(($name(dat), rem))
}
}
};
}