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
227
228
229
230
231
232
233
234
235
|
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
use std::borrow::Cow;
use zerofrom::ZeroFrom;
use zerovec::{ule::AsULE, *};
#[make_varule(VarStructULE)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, serde::Serialize, serde::Deserialize)]
#[zerovec::derive(Serialize, Deserialize, Debug)]
struct VarStruct<'a> {
a: u32,
b: char,
#[serde(borrow)]
c: Cow<'a, str>,
}
#[make_varule(VarStructOutOfOrderULE)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, serde::Serialize, serde::Deserialize)]
#[zerovec::derive(Serialize, Deserialize, Debug)]
struct VarStructOutOfOrder<'a> {
a: u32,
#[serde(borrow)]
b: Cow<'a, str>,
c: char,
d: u8,
}
#[make_varule(VarTupleStructULE)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, serde::Serialize, serde::Deserialize)]
#[zerovec::derive(Serialize, Deserialize, Debug)]
struct VarTupleStruct<'a>(u32, char, #[serde(borrow)] VarZeroVec<'a, str>);
#[make_varule(NoKVULE)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, serde::Serialize, serde::Deserialize)]
#[zerovec::skip_derive(ZeroMapKV)]
#[zerovec::derive(Serialize, Deserialize, Debug)]
struct NoKV<'a>(u32, char, #[serde(borrow)] VarZeroVec<'a, str>);
#[make_varule(NoOrdULE)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, serde::Serialize, serde::Deserialize)]
#[zerovec::skip_derive(ZeroMapKV, Ord)]
#[zerovec::derive(Serialize, Deserialize, Debug)]
struct NoOrd<'a>(u32, char, #[serde(borrow)] VarZeroVec<'a, str>);
#[make_varule(MultiFieldStructULE)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, serde::Serialize, serde::Deserialize)]
#[zerovec::derive(Serialize, Deserialize, Debug)]
struct MultiFieldStruct<'a> {
a: u32,
b: char,
#[serde(borrow)]
c: Cow<'a, str>,
d: u8,
#[serde(borrow)]
e: Cow<'a, str>,
f: char,
}
#[make_varule(MultiFieldConsecutiveStructULE)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, serde::Serialize, serde::Deserialize)]
#[zerovec::derive(Serialize, Deserialize, Debug)]
struct MultiFieldConsecutiveStruct<'a> {
#[serde(borrow)]
a: Cow<'a, str>,
#[serde(borrow)]
b: Cow<'a, str>,
#[serde(borrow)]
c: Cow<'a, str>,
#[serde(borrow)]
d: Cow<'a, str>,
}
#[make_varule(CustomVarFieldULE)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, serde::Serialize, serde::Deserialize)]
#[zerovec::derive(Serialize, Deserialize, Debug)]
struct CustomVarField<'a> {
#[zerovec::varule(MultiFieldStructULE)]
#[serde(borrow)]
a: MultiFieldStruct<'a>,
b: u32,
}
#[make_varule(MultiFieldTupleULE)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, serde::Serialize, serde::Deserialize)]
#[zerovec::derive(Serialize, Deserialize, Debug)]
struct MultiFieldTuple<'a>(
u8,
char,
#[serde(borrow)] VarZeroVec<'a, str>,
#[serde(borrow)] VarZeroVec<'a, [u8]>,
#[serde(borrow)] Cow<'a, str>,
);
/// The `assert` function should have the body `|(stack, zero)| assert_eq!(stack, &U::zero_from(&zero))`
///
/// We cannot do this internally because we technically need a different `U` with a shorter lifetime here
/// which would require some gnarly lifetime bounds and perhaps a Yoke dependency. This is just a test, so it's
/// not important to get this 100% perfect
fn assert_zerovec<T, U, F>(slice: &[U], assert: F)
where
T: ule::VarULE + ?Sized + serde::Serialize,
U: ule::EncodeAsVarULE<T> + serde::Serialize,
F: Fn(&U, &T),
for<'a> Box<T>: serde::Deserialize<'a>,
{
let varzerovec: VarZeroVec<T> = slice.into();
assert_eq!(varzerovec.len(), slice.len());
for (stack, zero) in slice.iter().zip(varzerovec.iter()) {
assert(stack, zero)
}
let bytes = varzerovec.as_bytes();
let name = std::any::type_name::<T>();
let reparsed: VarZeroVec<T> = VarZeroVec::parse_byte_slice(bytes)
.unwrap_or_else(|_| panic!("Parsing VarZeroVec<{name}> should succeed"));
assert_eq!(reparsed.len(), slice.len());
for (stack, zero) in slice.iter().zip(reparsed.iter()) {
assert(stack, zero)
}
let bincode = bincode::serialize(&varzerovec).unwrap();
let deserialized: VarZeroVec<T> = bincode::deserialize(&bincode).unwrap();
for (stack, zero) in slice.iter().zip(deserialized.iter()) {
assert(stack, zero)
}
let json_slice = serde_json::to_string(&slice).unwrap();
let json_vzv = serde_json::to_string(&varzerovec).unwrap();
assert_eq!(json_slice, json_vzv);
let deserialized: VarZeroVec<T> = serde_json::from_str(&json_vzv).unwrap();
for (stack, zero) in slice.iter().zip(deserialized.iter()) {
assert(stack, zero)
}
}
fn main() {
assert_zerovec::<VarStructULE, VarStruct, _>(TEST_VARSTRUCTS, |stack, zero| {
assert_eq!(stack, &VarStruct::zero_from(zero))
});
assert_zerovec::<MultiFieldStructULE, MultiFieldStruct, _>(TEST_MULTIFIELD, |stack, zero| {
assert_eq!(stack, &MultiFieldStruct::zero_from(zero))
});
assert_zerovec::<MultiFieldConsecutiveStructULE, MultiFieldConsecutiveStruct, _>(
TEST_MULTICONSECUTIVE,
|stack, zero| assert_eq!(stack, &MultiFieldConsecutiveStruct::zero_from(zero)),
);
let vartuples = &[
VarTupleStruct(101, 'ø', TEST_STRINGS1.into()),
VarTupleStruct(9499, '⸘', TEST_STRINGS2.into()),
VarTupleStruct(3478, '月', TEST_STRINGS3.into()),
];
assert_zerovec::<VarTupleStructULE, VarTupleStruct, _>(vartuples, |stack, zero| {
assert_eq!(stack, &VarTupleStruct::zero_from(zero))
});
// Test that all fields are accessible on a type using multifieldule
let multi_ule = ule::encode_varule_to_box(&TEST_MULTIFIELD[0]);
assert_eq!(multi_ule.a, TEST_MULTIFIELD[0].a.to_unaligned());
assert_eq!(multi_ule.b, TEST_MULTIFIELD[0].b.to_unaligned());
assert_eq!(multi_ule.c(), TEST_MULTIFIELD[0].c);
assert_eq!(multi_ule.d, TEST_MULTIFIELD[0].d);
assert_eq!(multi_ule.e(), TEST_MULTIFIELD[0].e);
assert_eq!(multi_ule.f, TEST_MULTIFIELD[0].f.to_unaligned());
}
const TEST_VARSTRUCTS: &[VarStruct<'static>] = &[
VarStruct {
a: 101,
b: 'ø',
c: Cow::Borrowed("testīng strīng"),
},
VarStruct {
a: 9499,
b: '⸘',
c: Cow::Borrowed("a diffərənt ştring"),
},
VarStruct {
a: 3478,
b: '月',
c: Cow::Borrowed("好多嘅 string"),
},
];
const TEST_STRINGS1: &[&str] = &["foo", "bar", "baz"];
const TEST_STRINGS2: &[&str] = &["hellø", "wørłd"];
const TEST_STRINGS3: &[&str] = &["łořem", "ɨpsu₥"];
const TEST_MULTIFIELD: &[MultiFieldStruct<'static>] = &[
MultiFieldStruct {
a: 101,
b: 'ø',
c: Cow::Borrowed("testīng strīng"),
d: 8,
e: Cow::Borrowed("another testīng strīng"),
f: 'å',
},
MultiFieldStruct {
a: 9499,
b: '⸘',
c: Cow::Borrowed("a diffərənt ştring"),
d: 120,
e: Cow::Borrowed("a diffərənt testing ştring"),
f: 'ł',
},
MultiFieldStruct {
a: 3478,
b: '月',
c: Cow::Borrowed("好多嘅 string"),
d: 89,
e: Cow::Borrowed("many 好多嘅 string"),
f: 'ə',
},
];
const TEST_MULTICONSECUTIVE: &[MultiFieldConsecutiveStruct<'static>] =
&[MultiFieldConsecutiveStruct {
a: Cow::Borrowed("one"),
b: Cow::Borrowed("2"),
c: Cow::Borrowed("three"),
d: Cow::Borrowed("four"),
}];
|