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
use super::*;
use core::fmt;
use core::mem;
macro_rules! tuple_ule {
($name:ident, $len:literal, [ $($t:ident $i:tt),+ ]) => {
#[doc = concat!("ULE type for tuples with ", $len, " elements.")]
#[repr(packed)]
pub struct $name<$($t),+>($(pub $t),+);
unsafe impl<$($t: ULE),+> ULE for $name<$($t),+> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
let ule_bytes: usize = 0usize $(+ mem::size_of::<$t>())+;
if bytes.len() % ule_bytes != 0 {
return Err(ZeroVecError::length::<Self>(bytes.len()));
}
for chunk in bytes.chunks(ule_bytes) {
let mut i: usize = 0;
$(
let j = i;
i += mem::size_of::<$t>();
#[allow(clippy::indexing_slicing)]
<$t>::validate_byte_slice(&chunk[j..i])?;
)+
}
Ok(())
}
}
impl<$($t: AsULE),+> AsULE for ($($t),+) {
type ULE = $name<$(<$t>::ULE),+>;
#[inline]
fn to_unaligned(self) -> Self::ULE {
$name($(
self.$i.to_unaligned()
),+)
}
#[inline]
fn from_unaligned(unaligned: Self::ULE) -> Self {
($(
<$t>::from_unaligned(unaligned.$i)
),+)
}
}
impl<$($t: fmt::Debug + ULE),+> fmt::Debug for $name<$($t),+> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
($(self.$i),+).fmt(f)
}
}
impl<$($t: PartialEq + ULE),+> PartialEq for $name<$($t),+> {
fn eq(&self, other: &Self) -> bool {
($(self.$i),+).eq(&($(other.$i),+))
}
}
impl<$($t: Eq + ULE),+> Eq for $name<$($t),+> {}
impl<$($t: PartialOrd + ULE),+> PartialOrd for $name<$($t),+> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
($(self.$i),+).partial_cmp(&($(other.$i),+))
}
}
impl<$($t: Ord + ULE),+> Ord for $name<$($t),+> {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
($(self.$i),+).cmp(&($(other.$i),+))
}
}
impl<$($t: ULE),+> Clone for $name<$($t),+> {
fn clone(&self) -> Self {
let stack = ($(self.$i),+);
$name($(stack.$i),+)
}
}
impl<$($t: ULE),+> Copy for $name<$($t),+> {}
impl<'a, $($t: Ord + AsULE + 'static),+> crate::map::ZeroMapKV<'a> for ($($t),+) {
type Container = crate::ZeroVec<'a, ($($t),+)>;
type Slice = crate::ZeroSlice<($($t),+)>;
type GetType = $name<$(<$t>::ULE),+>;
type OwnedType = ($($t),+);
}
};
}
tuple_ule!(Tuple2ULE, "2", [ A 0, B 1 ]);
tuple_ule!(Tuple3ULE, "3", [ A 0, B 1, C 2 ]);
tuple_ule!(Tuple4ULE, "4", [ A 0, B 1, C 2, D 3 ]);
tuple_ule!(Tuple5ULE, "5", [ A 0, B 1, C 2, D 3, E 4 ]);
tuple_ule!(Tuple6ULE, "6", [ A 0, B 1, C 2, D 3, E 4, F 5 ]);
#[test]
fn test_pairule_validate() {
use crate::ZeroVec;
let vec: Vec<(u32, char)> = vec![(1, 'a'), (1234901, '啊'), (100, 'अ')];
let zerovec: ZeroVec<(u32, char)> = vec.iter().copied().collect();
let bytes = zerovec.as_bytes();
let zerovec2 = ZeroVec::parse_byte_slice(bytes).unwrap();
assert_eq!(zerovec, zerovec2);
let zerovec3 = ZeroVec::<(char, u32)>::parse_byte_slice(bytes);
assert!(matches!(zerovec3, Err(_)));
}
#[test]
fn test_tripleule_validate() {
use crate::ZeroVec;
let vec: Vec<(u32, char, i8)> = vec![(1, 'a', -5), (1234901, '啊', 3), (100, 'अ', -127)];
let zerovec: ZeroVec<(u32, char, i8)> = vec.iter().copied().collect();
let bytes = zerovec.as_bytes();
let zerovec2 = ZeroVec::parse_byte_slice(bytes).unwrap();
assert_eq!(zerovec, zerovec2);
let zerovec3 = ZeroVec::<(char, i8, u32)>::parse_byte_slice(bytes);
assert!(matches!(zerovec3, Err(_)));
}
#[test]
fn test_quadule_validate() {
use crate::ZeroVec;
let vec: Vec<(u32, char, i8, u16)> =
vec![(1, 'a', -5, 3), (1234901, '啊', 3, 11), (100, 'अ', -127, 0)];
let zerovec: ZeroVec<(u32, char, i8, u16)> = vec.iter().copied().collect();
let bytes = zerovec.as_bytes();
let zerovec2 = ZeroVec::parse_byte_slice(bytes).unwrap();
assert_eq!(zerovec, zerovec2);
let zerovec3 = ZeroVec::<(char, i8, u16, u32)>::parse_byte_slice(bytes);
assert!(matches!(zerovec3, Err(_)));
}