Debian Codegraph @

debian/rust-vendor/zerovec/src/ule/encode.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 use crate::ule::*;
6 use crate::varzerovec::VarZeroVecFormat;
7 use crate::{VarZeroSlice, VarZeroVec, ZeroSlice, ZeroVec};
8 #[cfg(feature = "alloc")]
9 use alloc::borrow::{Cow, ToOwned};
10 #[cfg(feature = "alloc")]
11 use alloc::boxed::Box;
12 #[cfg(feature = "alloc")]
13 use alloc::string::String;
14 #[cfg(feature = "alloc")]
15 use alloc::{vec, vec::Vec};
16
17 /// Allows types to be encoded as [`VarULE`]s. This is highly useful for implementing [`VarULE`] on
18 /// custom DSTs where the type cannot be obtained as a reference to some other type.
19 ///
20 /// [`Self::encode_var_ule_as_slices()`] should be implemented by providing an encoded slice for each field
21 /// of the [`VarULE`] type to the callback, in order. For an implementation to be safe, the slices
22 /// to the callback must, when concatenated, be a valid instance of the [`VarULE`] type.
23 ///
24 /// See the [custom `VarULEdocumentation`](crate::ule::custom) for examples.
25 ///
26 /// [`Self::encode_var_ule_as_slices()`] is only used to provide default implementations for [`Self::encode_var_ule_write()`]
27 /// and [`Self::encode_var_ule_len()`]. If you override the default implementations it is totally valid to
28 /// replace [`Self::encode_var_ule_as_slices()`]'s body with `unreachable!()`. This can be done for cases where
29 /// it is not possible to implement [`Self::encode_var_ule_as_slices()`] but the other methods still work.
30 ///
31 /// A typical implementation will take each field in the order found in the [`VarULE`] type,
32 /// convert it to ULE, call [`ULE::slice_as_bytes()`] on them, and pass the slices to `cb` in order.
33 /// A trailing [`ZeroVec`](crate::ZeroVec) or [`VarZeroVec`](crate::VarZeroVec) can have their underlying
34 /// byte representation passed through.
35 ///
36 /// In case the compiler is not optimizing [`Self::encode_var_ule_len()`], it can be overridden. A typical
37 /// implementation will add up the sizes of each field on the [`VarULE`] type and then add in the byte length of the
38 /// dynamically-sized part.
39 ///
40 /// # Reverse-encoding [`VarULE`]
41 ///
42 /// This trait maps a struct to its bytes representation ("serialization"), and
43 /// [`ZeroFrom`](zerofrom::ZeroFrom) performs the opposite operation, taking those bytes and
44 /// creating a struct from them ("deserialization").
45 ///
46 /// # Safety
47 ///
48 /// The safety invariants of [`Self::encode_var_ule_as_slices()`] are:
49 /// - It must call `cb` (only once)
50 /// - The slices passed to `cb`, if concatenated, should be a valid instance of the `T` [`VarULE`] type
51 /// (i.e. if fed to [`VarULE::validate_bytes()`] they must produce a successful result)
52 /// - It must return the return value of `cb` to the caller
53 ///
54 /// One or more of [`Self::encode_var_ule_len()`] and [`Self::encode_var_ule_write()`] may be provided.
55 /// If both are, then `zerovec` code is guaranteed to not call [`Self::encode_var_ule_as_slices()`], and it may be replaced
56 /// with `unreachable!()`.
57 ///
58 /// The safety invariants of [`Self::encode_var_ule_len()`] are:
59 /// - It must return the length of the corresponding [`VarULE`] type
60 ///
61 /// The safety invariants of [`Self::encode_var_ule_write()`] are:
62 /// - The slice written to `dst` must be a valid instance of the `T` [`VarULE`] type
63 pub unsafe trait EncodeAsVarULE<T: VarULE + ?Sized> {
64 /// Calls `cb` with a piecewise list of byte slices that when concatenated
65 /// produce the memory pattern of the corresponding instance of `T`.
66 ///
67 /// Do not call this function directly; instead use the other two. Some implementors
68 /// may define this function to panic.
69 fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R;
70
71 /// Return the length, in bytes, of the corresponding [`VarULE`] type
72 fn encode_var_ule_len(&self) -> usize {
73 self.encode_var_ule_as_slices(|slices| slices.iter().map(|s| s.len()).sum())
74 }
75
76 /// Write the corresponding [`VarULE`] type to the `dst` buffer. `dst` should
77 /// be the size of [`Self::encode_var_ule_len()`]
78 fn encode_var_ule_write(&self, mut dst: &mut [u8]) {
79 debug_assert_eq!(self.encode_var_ule_len(), dst.len());
80 self.encode_var_ule_as_slices(move |slices| {
81 #[expect(clippy::indexing_slicing)] // by debug_assert
82 for slice in slices {
83 dst[..slice.len()].copy_from_slice(slice);
84 dst = &mut dst[slice.len()..];
85 }
86 });
87 }
88 }
89
90 /// Given an [`EncodeAsVarULE`] type `S`, encode it into a `Box<T>`
91 ///
92 /// This is primarily useful for generating `Deserialize` impls for [`VarULE`] types
93 #[cfg(feature = "alloc")]
94 pub fn encode_varule_to_box<S: EncodeAsVarULE<T> + ?Sized, T: VarULE + ?Sized>(x: &S) -> Box<T> {
95 // zero-fill the vector to avoid uninitialized data UB
96 let mut vec: Vec<u8> = vec![0; x.encode_var_ule_len()];
97 x.encode_var_ule_write(&mut vec);
98 // SAFETY:
99 // - T::from_bytes_unchecked is safe because the bytes were written by x.encode_var_ule_write
100 // which guarantees a valid representation of T.
101 unsafe { cast_box(vec.into_boxed_slice()) }
102 }
103
104 unsafe impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for T {
105 fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R {
106 cb(&[T::as_bytes(self)])
107 }
108 }
109
110 unsafe impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for &'_ T {
111 fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R {
112 cb(&[T::as_bytes(self)])
113 }
114 }
115
116 unsafe impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for &'_ &'_ T {
117 fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R {
118 cb(&[T::as_bytes(self)])
119 }
120 }
121
122 #[cfg(feature = "alloc")]
123 unsafe impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for Cow<'_, T>
124 where
125 T: ToOwned,
126 {
127 fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R {
128 cb(&[T::as_bytes(self.as_ref())])
129 }
130 }
131
132 #[cfg(feature = "alloc")]
133 unsafe impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for Box<T> {
134 fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R {
135 cb(&[T::as_bytes(self)])
136 }
137 }
138
139 #[cfg(feature = "alloc")]
140 unsafe impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for &'_ Box<T> {
141 fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R {
142 cb(&[T::as_bytes(self)])
143 }
144 }
145
146 #[cfg(feature = "alloc")]
147 unsafe impl EncodeAsVarULE<str> for String {
148 fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R {
149 cb(&[self.as_bytes()])
150 }
151 }
152
153 #[cfg(feature = "alloc")]
154 unsafe impl EncodeAsVarULE<str> for &'_ String {
155 fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R {
156 cb(&[self.as_bytes()])
157 }
158 }
159
160 // Note: This impl could technically use `T: AsULE`, but we want users to prefer `ZeroSlice<T>`
161 // for cases where T is not a ULE. Therefore, we can use the more efficient `memcpy` impl here.
162 #[cfg(feature = "alloc")]
163 unsafe impl<T> EncodeAsVarULE<[T]> for Vec<T>
164 where
165 T: ULE,
166 {
167 fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R {
168 cb(&[<[T] as VarULE>::as_bytes(self)])
169 }
170 }
171
172 unsafe impl<T> EncodeAsVarULE<ZeroSlice<T>> for &'_ [T]
173 where
174 T: AsULE + 'static,
175 {
176 fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R {
177 // unnecessary if the other two are implemented
178 unreachable!()
179 }
180
181 #[inline]
182 fn encode_var_ule_len(&self) -> usize {
183 self.len() * size_of::<T::ULE>()
184 }
185
186 fn encode_var_ule_write(&self, dst: &mut [u8]) {
187 #[allow(non_snake_case)]
188 let S = size_of::<T::ULE>();
189 debug_assert_eq!(self.len() * S, dst.len());
190 for (item, ref mut chunk) in self.iter().zip(dst.chunks_mut(S)) {
191 let ule = item.to_unaligned();
192 chunk.copy_from_slice(ULE::slice_as_bytes(slice::from_ref(&ule)));
193 }
194 }
195 }
196
197 #[cfg(feature = "alloc")]
198 unsafe impl<T> EncodeAsVarULE<ZeroSlice<T>> for Vec<T>
199 where
200 T: AsULE + 'static,
201 {
202 fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R {
203 // unnecessary if the other two are implemented
204 unreachable!()
205 }
206
207 #[inline]
208 fn encode_var_ule_len(&self) -> usize {
209 self.as_slice().encode_var_ule_len()
210 }
211
212 #[inline]
213 fn encode_var_ule_write(&self, dst: &mut [u8]) {
214 self.as_slice().encode_var_ule_write(dst)
215 }
216 }
217
218 unsafe impl<T> EncodeAsVarULE<ZeroSlice<T>> for ZeroVec<'_, T>
219 where
220 T: AsULE + 'static,
221 {
222 fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R {
223 // unnecessary if the other two are implemented
224 unreachable!()
225 }
226
227 #[inline]
228 fn encode_var_ule_len(&self) -> usize {
229 self.as_bytes().len()
230 }
231
232 fn encode_var_ule_write(&self, dst: &mut [u8]) {
233 debug_assert_eq!(self.as_bytes().len(), dst.len());
234 dst.copy_from_slice(self.as_bytes());
235 }
236 }
237
238 unsafe impl<T, E, F> EncodeAsVarULE<VarZeroSlice<T, F>> for &'_ [E]
239 where
240 T: VarULE + ?Sized,
241 E: EncodeAsVarULE<T>,
242 F: VarZeroVecFormat,
243 {
244 fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R {
245 // unnecessary if the other two are implemented
246 unimplemented!()
247 }
248
249 #[expect(clippy::unwrap_used)] // TODO(#1410): Rethink length errors in VZV.
250 fn encode_var_ule_len(&self) -> usize {
251 crate::varzerovec::components::compute_serializable_len::<T, E, F>(self).unwrap() as usize
252 }
253
254 fn encode_var_ule_write(&self, dst: &mut [u8]) {
255 crate::varzerovec::components::write_serializable_bytes::<T, E, F>(self, dst)
256 }
257 }
258
259 #[cfg(feature = "alloc")]
260 unsafe impl<T, E, F> EncodeAsVarULE<VarZeroSlice<T, F>> for Vec<E>
261 where
262 T: VarULE + ?Sized,
263 E: EncodeAsVarULE<T>,
264 F: VarZeroVecFormat,
265 {
266 fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R {
267 // unnecessary if the other two are implemented
268 unreachable!()
269 }
270
271 #[inline]
272 fn encode_var_ule_len(&self) -> usize {
273 <_ as EncodeAsVarULE<VarZeroSlice<T, F>>>::encode_var_ule_len(&self.as_slice())
274 }
275
276 #[inline]
277 fn encode_var_ule_write(&self, dst: &mut [u8]) {
278 <_ as EncodeAsVarULE<VarZeroSlice<T, F>>>::encode_var_ule_write(&self.as_slice(), dst)
279 }
280 }
281
282 unsafe impl<T, F> EncodeAsVarULE<VarZeroSlice<T, F>> for VarZeroVec<'_, T, F>
283 where
284 T: VarULE + ?Sized,
285 F: VarZeroVecFormat,
286 {
287 fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R {
288 // unnecessary if the other two are implemented
289 unreachable!()
290 }
291
292 #[inline]
293 fn encode_var_ule_len(&self) -> usize {
294 self.as_bytes().len()
295 }
296
297 #[inline]
298 fn encode_var_ule_write(&self, dst: &mut [u8]) {
299 debug_assert_eq!(self.as_bytes().len(), dst.len());
300 dst.copy_from_slice(self.as_bytes());
301 }
302 }
303
304 #[cfg(test)]
305 mod test {
306 use super::*;
307
308 const STRING_ARRAY: [&str; 2] = ["hello", "world"];
309
310 const STRING_SLICE: &[&str] = &STRING_ARRAY;
311
312 const U8_ARRAY: [u8; 8] = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
313
314 const U8_2D_ARRAY: [&[u8]; 2] = [&U8_ARRAY, &U8_ARRAY];
315
316 const U8_2D_SLICE: &[&[u8]] = &[&U8_ARRAY, &U8_ARRAY];
317
318 const U8_3D_ARRAY: [&[&[u8]]; 2] = [U8_2D_SLICE, U8_2D_SLICE];
319
320 const U8_3D_SLICE: &[&[&[u8]]] = &[U8_2D_SLICE, U8_2D_SLICE];
321
322 const U32_ARRAY: [u32; 4] = [0x00010203, 0x04050607, 0x08090A0B, 0x0C0D0E0F];
323
324 const U32_2D_ARRAY: [&[u32]; 2] = [&U32_ARRAY, &U32_ARRAY];
325
326 const U32_2D_SLICE: &[&[u32]] = &[&U32_ARRAY, &U32_ARRAY];
327
328 const U32_3D_ARRAY: [&[&[u32]]; 2] = [U32_2D_SLICE, U32_2D_SLICE];
329
330 const U32_3D_SLICE: &[&[&[u32]]] = &[U32_2D_SLICE, U32_2D_SLICE];
331
332 #[test]
333 fn test_vzv_from() {
334 type VZV<'a, T> = VarZeroVec<'a, T>;
335 type ZS<T> = ZeroSlice<T>;
336 type VZS<T> = VarZeroSlice<T>;
337
338 let u8_zerovec: ZeroVec<u8> = ZeroVec::from_slice_or_alloc(&U8_ARRAY);
339 let u8_2d_zerovec: [ZeroVec<u8>; 2] = [u8_zerovec.clone(), u8_zerovec.clone()];
340 let u8_2d_vec: Vec<Vec<u8>> = vec![U8_ARRAY.into(), U8_ARRAY.into()];
341 let u8_3d_vec: Vec<Vec<Vec<u8>>> = vec![u8_2d_vec.clone(), u8_2d_vec.clone()];
342
343 let u32_zerovec: ZeroVec<u32> = ZeroVec::from_slice_or_alloc(&U32_ARRAY);
344 let u32_2d_zerovec: [ZeroVec<u32>; 2] = [u32_zerovec.clone(), u32_zerovec.clone()];
345 let u32_2d_vec: Vec<Vec<u32>> = vec![U32_ARRAY.into(), U32_ARRAY.into()];
346 let u32_3d_vec: Vec<Vec<Vec<u32>>> = vec![u32_2d_vec.clone(), u32_2d_vec.clone()];
347
348 let a: VZV<str> = VarZeroVec::from(&STRING_ARRAY);
349 let b: VZV<str> = VarZeroVec::from(STRING_SLICE);
350 let c: VZV<str> = VarZeroVec::from(&Vec::from(STRING_SLICE));
351 assert_eq!(a, STRING_SLICE);
352 assert_eq!(a, b);
353 assert_eq!(a, c);
354
355 let a: VZV<[u8]> = VarZeroVec::from(&U8_2D_ARRAY);
356 let b: VZV<[u8]> = VarZeroVec::from(U8_2D_SLICE);
357 let c: VZV<[u8]> = VarZeroVec::from(&u8_2d_vec);
358 assert_eq!(a, U8_2D_SLICE);
359 assert_eq!(a, b);
360 assert_eq!(a, c);
361 let u8_3d_vzv_brackets = &[a.clone(), a.clone()];
362
363 let a: VZV<ZS<u8>> = VarZeroVec::from(&U8_2D_ARRAY);
364 let b: VZV<ZS<u8>> = VarZeroVec::from(U8_2D_SLICE);
365 let c: VZV<ZS<u8>> = VarZeroVec::from(&u8_2d_vec);
366 let d: VZV<ZS<u8>> = VarZeroVec::from(&u8_2d_zerovec);
367 assert_eq!(a, U8_2D_SLICE);
368 assert_eq!(a, b);
369 assert_eq!(a, c);
370 assert_eq!(a, d);
371 let u8_3d_vzv_zeroslice = &[a.clone(), a.clone()];
372
373 let a: VZV<VZS<[u8]>> = VarZeroVec::from(&U8_3D_ARRAY);
374 let b: VZV<VZS<[u8]>> = VarZeroVec::from(U8_3D_SLICE);
375 let c: VZV<VZS<[u8]>> = VarZeroVec::from(&u8_3d_vec);
376 let d: VZV<VZS<[u8]>> = VarZeroVec::from(u8_3d_vzv_brackets);
377 assert_eq!(
378 a.iter()
379 .map(|x| x.iter().map(|y| y.to_vec()).collect::<Vec<Vec<u8>>>())
380 .collect::<Vec<Vec<Vec<u8>>>>(),
381 u8_3d_vec
382 );
383 assert_eq!(a, b);
384 assert_eq!(a, c);
385 assert_eq!(a, d);
386
387 let a: VZV<VZS<ZS<u8>>> = VarZeroVec::from(&U8_3D_ARRAY);
388 let b: VZV<VZS<ZS<u8>>> = VarZeroVec::from(U8_3D_SLICE);
389 let c: VZV<VZS<ZS<u8>>> = VarZeroVec::from(&u8_3d_vec);
390 let d: VZV<VZS<ZS<u8>>> = VarZeroVec::from(u8_3d_vzv_zeroslice);
391 assert_eq!(
392 a.iter()
393 .map(|x| x
394 .iter()
395 .map(|y| y.iter().collect::<Vec<u8>>())
396 .collect::<Vec<Vec<u8>>>())
397 .collect::<Vec<Vec<Vec<u8>>>>(),
398 u8_3d_vec
399 );
400 assert_eq!(a, b);
401 assert_eq!(a, c);
402 assert_eq!(a, d);
403
404 let a: VZV<ZS<u32>> = VarZeroVec::from(&U32_2D_ARRAY);
405 let b: VZV<ZS<u32>> = VarZeroVec::from(U32_2D_SLICE);
406 let c: VZV<ZS<u32>> = VarZeroVec::from(&u32_2d_vec);
407 let d: VZV<ZS<u32>> = VarZeroVec::from(&u32_2d_zerovec);
408 assert_eq!(a, u32_2d_zerovec);
409 assert_eq!(a, b);
410 assert_eq!(a, c);
411 assert_eq!(a, d);
412 let u32_3d_vzv = &[a.clone(), a.clone()];
413
414 let a: VZV<VZS<ZS<u32>>> = VarZeroVec::from(&U32_3D_ARRAY);
415 let b: VZV<VZS<ZS<u32>>> = VarZeroVec::from(U32_3D_SLICE);
416 let c: VZV<VZS<ZS<u32>>> = VarZeroVec::from(&u32_3d_vec);
417 let d: VZV<VZS<ZS<u32>>> = VarZeroVec::from(u32_3d_vzv);
418 assert_eq!(
419 a.iter()
420 .map(|x| x
421 .iter()
422 .map(|y| y.iter().collect::<Vec<u32>>())
423 .collect::<Vec<Vec<u32>>>())
424 .collect::<Vec<Vec<Vec<u32>>>>(),
425 u32_3d_vec
426 );
427 assert_eq!(a, b);
428 assert_eq!(a, c);
429 assert_eq!(a, d);
430 }
431 }