rustc_serialize/
serialize.rs

1//! Support code for encoding and decoding types.
2
3use std::borrow::Cow;
4use std::cell::{Cell, RefCell};
5use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
6use std::hash::{BuildHasher, Hash};
7use std::marker::PhantomData;
8use std::num::NonZero;
9use std::path;
10use std::rc::Rc;
11use std::sync::Arc;
12
13use rustc_hashes::{Hash64, Hash128};
14use smallvec::{Array, SmallVec};
15use thin_vec::ThinVec;
16
17/// A byte that [cannot occur in UTF8 sequences][utf8]. Used to mark the end of a string.
18/// This way we can skip validation and still be relatively sure that deserialization
19/// did not desynchronize.
20///
21/// [utf8]: https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=1058865525#Codepage_layout
22const STR_SENTINEL: u8 = 0xC1;
23
24/// A note about error handling.
25///
26/// Encoders may be fallible, but in practice failure is rare and there are so
27/// many nested calls that typical Rust error handling (via `Result` and `?`)
28/// is pervasive and has non-trivial cost. Instead, impls of this trait must
29/// implement a delayed error handling strategy. If a failure occurs, they
30/// should record this internally, and all subsequent encoding operations can
31/// be processed or ignored, whichever is appropriate. Then they should provide
32/// a `finish` method that finishes up encoding. If the encoder is fallible,
33/// `finish` should return a `Result` that indicates success or failure.
34///
35/// This current does not support `f32` nor `f64`, as they're not needed in any
36/// serialized data structures. That could be changed, but consider whether it
37/// really makes sense to store floating-point values at all.
38/// (If you need it, revert <https://github.com/rust-lang/rust/pull/109984>.)
39pub trait Encoder {
40    fn emit_usize(&mut self, v: usize);
41    fn emit_u128(&mut self, v: u128);
42    fn emit_u64(&mut self, v: u64);
43    fn emit_u32(&mut self, v: u32);
44    fn emit_u16(&mut self, v: u16);
45    fn emit_u8(&mut self, v: u8);
46
47    fn emit_isize(&mut self, v: isize);
48    fn emit_i128(&mut self, v: i128);
49    fn emit_i64(&mut self, v: i64);
50    fn emit_i32(&mut self, v: i32);
51    fn emit_i16(&mut self, v: i16);
52
53    #[inline]
54    fn emit_i8(&mut self, v: i8) {
55        self.emit_u8(v as u8);
56    }
57
58    #[inline]
59    fn emit_bool(&mut self, v: bool) {
60        self.emit_u8(if v { 1 } else { 0 });
61    }
62
63    #[inline]
64    fn emit_char(&mut self, v: char) {
65        self.emit_u32(v as u32);
66    }
67
68    #[inline]
69    fn emit_str(&mut self, v: &str) {
70        self.emit_usize(v.len());
71        self.emit_raw_bytes(v.as_bytes());
72        self.emit_u8(STR_SENTINEL);
73    }
74
75    fn emit_raw_bytes(&mut self, s: &[u8]);
76}
77
78// Note: all the methods in this trait are infallible, which may be surprising.
79// They used to be fallible (i.e. return a `Result`) but many of the impls just
80// panicked when something went wrong, and for the cases that didn't the
81// top-level invocation would also just panic on failure. Switching to
82// infallibility made things faster and lots of code a little simpler and more
83// concise.
84///
85/// This current does not support `f32` nor `f64`, as they're not needed in any
86/// serialized data structures. That could be changed, but consider whether it
87/// really makes sense to store floating-point values at all.
88/// (If you need it, revert <https://github.com/rust-lang/rust/pull/109984>.)
89pub trait Decoder {
90    fn read_usize(&mut self) -> usize;
91    fn read_u128(&mut self) -> u128;
92    fn read_u64(&mut self) -> u64;
93    fn read_u32(&mut self) -> u32;
94    fn read_u16(&mut self) -> u16;
95    fn read_u8(&mut self) -> u8;
96
97    fn read_isize(&mut self) -> isize;
98    fn read_i128(&mut self) -> i128;
99    fn read_i64(&mut self) -> i64;
100    fn read_i32(&mut self) -> i32;
101    fn read_i16(&mut self) -> i16;
102
103    #[inline]
104    fn read_i8(&mut self) -> i8 {
105        self.read_u8() as i8
106    }
107
108    #[inline]
109    fn read_bool(&mut self) -> bool {
110        let value = self.read_u8();
111        value != 0
112    }
113
114    #[inline]
115    fn read_char(&mut self) -> char {
116        let bits = self.read_u32();
117        std::char::from_u32(bits).unwrap()
118    }
119
120    #[inline]
121    fn read_str(&mut self) -> &str {
122        let len = self.read_usize();
123        let bytes = self.read_raw_bytes(len + 1);
124        assert!(bytes[len] == STR_SENTINEL);
125        unsafe { std::str::from_utf8_unchecked(&bytes[..len]) }
126    }
127
128    fn read_raw_bytes(&mut self, len: usize) -> &[u8];
129
130    fn peek_byte(&self) -> u8;
131    fn position(&self) -> usize;
132}
133
134/// Trait for types that can be serialized
135///
136/// This can be implemented using the `Encodable`, `TyEncodable` and
137/// `MetadataEncodable` macros.
138///
139/// * `Encodable` should be used in crates that don't depend on
140///   `rustc_middle`.
141/// * `MetadataEncodable` is used in `rustc_metadata` for types that contain
142///   `rustc_metadata::rmeta::Lazy`.
143/// * `TyEncodable` should be used for types that are only serialized in crate
144///   metadata or the incremental cache. This is most types in `rustc_middle`.
145pub trait Encodable<S: Encoder> {
146    fn encode(&self, s: &mut S);
147}
148
149/// Trait for types that can be deserialized
150///
151/// This can be implemented using the `Decodable`, `TyDecodable` and
152/// `MetadataDecodable` macros.
153///
154/// * `Decodable` should be used in crates that don't depend on
155///   `rustc_middle`.
156/// * `MetadataDecodable` is used in `rustc_metadata` for types that contain
157///   `rustc_metadata::rmeta::Lazy`.
158/// * `TyDecodable` should be used for types that are only serialized in crate
159///   metadata or the incremental cache. This is most types in `rustc_middle`.
160pub trait Decodable<D: Decoder>: Sized {
161    fn decode(d: &mut D) -> Self;
162}
163
164macro_rules! direct_serialize_impls {
165    ($($ty:ident $emit_method:ident $read_method:ident),*) => {
166        $(
167            impl<S: Encoder> Encodable<S> for $ty {
168                fn encode(&self, s: &mut S) {
169                    s.$emit_method(*self);
170                }
171            }
172
173            impl<D: Decoder> Decodable<D> for $ty {
174                fn decode(d: &mut D) -> $ty {
175                    d.$read_method()
176                }
177            }
178        )*
179    }
180}
181
182direct_serialize_impls! {
183    usize emit_usize read_usize,
184    u8 emit_u8 read_u8,
185    u16 emit_u16 read_u16,
186    u32 emit_u32 read_u32,
187    u64 emit_u64 read_u64,
188    u128 emit_u128 read_u128,
189
190    isize emit_isize read_isize,
191    i8 emit_i8 read_i8,
192    i16 emit_i16 read_i16,
193    i32 emit_i32 read_i32,
194    i64 emit_i64 read_i64,
195    i128 emit_i128 read_i128,
196
197    bool emit_bool read_bool,
198    char emit_char read_char
199}
200
201impl<S: Encoder, T: ?Sized> Encodable<S> for &T
202where
203    T: Encodable<S>,
204{
205    fn encode(&self, s: &mut S) {
206        (**self).encode(s)
207    }
208}
209
210impl<S: Encoder> Encodable<S> for ! {
211    fn encode(&self, _s: &mut S) {
212        unreachable!();
213    }
214}
215
216impl<D: Decoder> Decodable<D> for ! {
217    fn decode(_d: &mut D) -> ! {
218        unreachable!()
219    }
220}
221
222impl<S: Encoder> Encodable<S> for NonZero<u32> {
223    fn encode(&self, s: &mut S) {
224        s.emit_u32(self.get());
225    }
226}
227
228impl<D: Decoder> Decodable<D> for NonZero<u32> {
229    fn decode(d: &mut D) -> Self {
230        NonZero::new(d.read_u32()).unwrap()
231    }
232}
233
234impl<S: Encoder> Encodable<S> for str {
235    fn encode(&self, s: &mut S) {
236        s.emit_str(self);
237    }
238}
239
240impl<S: Encoder> Encodable<S> for String {
241    fn encode(&self, s: &mut S) {
242        s.emit_str(&self[..]);
243    }
244}
245
246impl<D: Decoder> Decodable<D> for String {
247    fn decode(d: &mut D) -> String {
248        d.read_str().to_owned()
249    }
250}
251
252impl<S: Encoder> Encodable<S> for () {
253    fn encode(&self, _s: &mut S) {}
254}
255
256impl<D: Decoder> Decodable<D> for () {
257    fn decode(_: &mut D) {}
258}
259
260impl<S: Encoder, T> Encodable<S> for PhantomData<T> {
261    fn encode(&self, _s: &mut S) {}
262}
263
264impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
265    fn decode(_: &mut D) -> PhantomData<T> {
266        PhantomData
267    }
268}
269
270impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<[T]> {
271    fn decode(d: &mut D) -> Box<[T]> {
272        let v: Vec<T> = Decodable::decode(d);
273        v.into_boxed_slice()
274    }
275}
276
277impl<S: Encoder, T: Encodable<S>> Encodable<S> for Rc<T> {
278    fn encode(&self, s: &mut S) {
279        (**self).encode(s);
280    }
281}
282
283impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<T> {
284    fn decode(d: &mut D) -> Rc<T> {
285        Rc::new(Decodable::decode(d))
286    }
287}
288
289impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T] {
290    default fn encode(&self, s: &mut S) {
291        s.emit_usize(self.len());
292        for e in self {
293            e.encode(s);
294        }
295    }
296}
297
298impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
299    fn encode(&self, s: &mut S) {
300        self.as_slice().encode(s);
301    }
302}
303
304impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> {
305    default fn decode(d: &mut D) -> Vec<T> {
306        let len = d.read_usize();
307        (0..len).map(|_| Decodable::decode(d)).collect()
308    }
309}
310
311impl<S: Encoder, T: Encodable<S>, const N: usize> Encodable<S> for [T; N] {
312    fn encode(&self, s: &mut S) {
313        self.as_slice().encode(s);
314    }
315}
316
317impl<D: Decoder, const N: usize> Decodable<D> for [u8; N] {
318    fn decode(d: &mut D) -> [u8; N] {
319        let len = d.read_usize();
320        assert!(len == N);
321        let mut v = [0u8; N];
322        for i in 0..len {
323            v[i] = Decodable::decode(d);
324        }
325        v
326    }
327}
328
329impl<S: Encoder, T: Encodable<S>> Encodable<S> for Cow<'_, [T]>
330where
331    [T]: ToOwned<Owned = Vec<T>>,
332{
333    fn encode(&self, s: &mut S) {
334        let slice: &[T] = self;
335        slice.encode(s);
336    }
337}
338
339impl<D: Decoder, T: Decodable<D> + ToOwned> Decodable<D> for Cow<'static, [T]>
340where
341    [T]: ToOwned<Owned = Vec<T>>,
342{
343    fn decode(d: &mut D) -> Cow<'static, [T]> {
344        let v: Vec<T> = Decodable::decode(d);
345        Cow::Owned(v)
346    }
347}
348
349impl<S: Encoder> Encodable<S> for Cow<'_, str> {
350    fn encode(&self, s: &mut S) {
351        let val: &str = self;
352        val.encode(s)
353    }
354}
355
356impl<D: Decoder> Decodable<D> for Cow<'_, str> {
357    fn decode(d: &mut D) -> Cow<'static, str> {
358        let v: String = Decodable::decode(d);
359        Cow::Owned(v)
360    }
361}
362
363impl<S: Encoder, T: Encodable<S>> Encodable<S> for Option<T> {
364    fn encode(&self, s: &mut S) {
365        match *self {
366            None => s.emit_u8(0),
367            Some(ref v) => {
368                s.emit_u8(1);
369                v.encode(s);
370            }
371        }
372    }
373}
374
375impl<D: Decoder, T: Decodable<D>> Decodable<D> for Option<T> {
376    fn decode(d: &mut D) -> Option<T> {
377        match d.read_u8() {
378            0 => None,
379            1 => Some(Decodable::decode(d)),
380            _ => panic!("Encountered invalid discriminant while decoding `Option`."),
381        }
382    }
383}
384
385impl<S: Encoder, T1: Encodable<S>, T2: Encodable<S>> Encodable<S> for Result<T1, T2> {
386    fn encode(&self, s: &mut S) {
387        match *self {
388            Ok(ref v) => {
389                s.emit_u8(0);
390                v.encode(s);
391            }
392            Err(ref v) => {
393                s.emit_u8(1);
394                v.encode(s);
395            }
396        }
397    }
398}
399
400impl<D: Decoder, T1: Decodable<D>, T2: Decodable<D>> Decodable<D> for Result<T1, T2> {
401    fn decode(d: &mut D) -> Result<T1, T2> {
402        match d.read_u8() {
403            0 => Ok(T1::decode(d)),
404            1 => Err(T2::decode(d)),
405            _ => panic!("Encountered invalid discriminant while decoding `Result`."),
406        }
407    }
408}
409
410macro_rules! peel {
411    ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
412}
413
414macro_rules! tuple {
415    () => ();
416    ( $($name:ident,)+ ) => (
417        impl<D: Decoder, $($name: Decodable<D>),+> Decodable<D> for ($($name,)+) {
418            fn decode(d: &mut D) -> ($($name,)+) {
419                ($({ let element: $name = Decodable::decode(d); element },)+)
420            }
421        }
422        impl<S: Encoder, $($name: Encodable<S>),+> Encodable<S> for ($($name,)+) {
423            #[allow(non_snake_case)]
424            fn encode(&self, s: &mut S) {
425                let ($(ref $name,)+) = *self;
426                $($name.encode(s);)+
427            }
428        }
429        peel! { $($name,)+ }
430    )
431}
432
433tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
434
435impl<S: Encoder> Encodable<S> for path::Path {
436    fn encode(&self, e: &mut S) {
437        self.to_str().unwrap().encode(e);
438    }
439}
440
441impl<S: Encoder> Encodable<S> for path::PathBuf {
442    fn encode(&self, e: &mut S) {
443        path::Path::encode(self, e);
444    }
445}
446
447impl<D: Decoder> Decodable<D> for path::PathBuf {
448    fn decode(d: &mut D) -> path::PathBuf {
449        let bytes: String = Decodable::decode(d);
450        path::PathBuf::from(bytes)
451    }
452}
453
454impl<S: Encoder, T: Encodable<S> + Copy> Encodable<S> for Cell<T> {
455    fn encode(&self, s: &mut S) {
456        self.get().encode(s);
457    }
458}
459
460impl<D: Decoder, T: Decodable<D> + Copy> Decodable<D> for Cell<T> {
461    fn decode(d: &mut D) -> Cell<T> {
462        Cell::new(Decodable::decode(d))
463    }
464}
465
466impl<S: Encoder, T: Encodable<S>> Encodable<S> for RefCell<T> {
467    fn encode(&self, s: &mut S) {
468        self.borrow().encode(s);
469    }
470}
471
472impl<D: Decoder, T: Decodable<D>> Decodable<D> for RefCell<T> {
473    fn decode(d: &mut D) -> RefCell<T> {
474        RefCell::new(Decodable::decode(d))
475    }
476}
477
478impl<S: Encoder, T: Encodable<S>> Encodable<S> for Arc<T> {
479    fn encode(&self, s: &mut S) {
480        (**self).encode(s);
481    }
482}
483
484impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> {
485    fn decode(d: &mut D) -> Arc<T> {
486        Arc::new(Decodable::decode(d))
487    }
488}
489
490impl<S: Encoder, T: ?Sized + Encodable<S>> Encodable<S> for Box<T> {
491    fn encode(&self, s: &mut S) {
492        (**self).encode(s)
493    }
494}
495
496impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<T> {
497    fn decode(d: &mut D) -> Box<T> {
498        Box::new(Decodable::decode(d))
499    }
500}
501
502impl<S: Encoder, A: Array<Item: Encodable<S>>> Encodable<S> for SmallVec<A> {
503    fn encode(&self, s: &mut S) {
504        self.as_slice().encode(s);
505    }
506}
507
508impl<D: Decoder, A: Array<Item: Decodable<D>>> Decodable<D> for SmallVec<A> {
509    fn decode(d: &mut D) -> SmallVec<A> {
510        let len = d.read_usize();
511        (0..len).map(|_| Decodable::decode(d)).collect()
512    }
513}
514
515impl<S: Encoder, T: Encodable<S>> Encodable<S> for ThinVec<T> {
516    fn encode(&self, s: &mut S) {
517        self.as_slice().encode(s);
518    }
519}
520
521impl<D: Decoder, T: Decodable<D>> Decodable<D> for ThinVec<T> {
522    fn decode(d: &mut D) -> ThinVec<T> {
523        let len = d.read_usize();
524        (0..len).map(|_| Decodable::decode(d)).collect()
525    }
526}
527
528impl<S: Encoder, T: Encodable<S>> Encodable<S> for VecDeque<T> {
529    fn encode(&self, s: &mut S) {
530        s.emit_usize(self.len());
531        for e in self {
532            e.encode(s);
533        }
534    }
535}
536
537impl<D: Decoder, T: Decodable<D>> Decodable<D> for VecDeque<T> {
538    fn decode(d: &mut D) -> VecDeque<T> {
539        let len = d.read_usize();
540        (0..len).map(|_| Decodable::decode(d)).collect()
541    }
542}
543
544impl<S: Encoder, K, V> Encodable<S> for BTreeMap<K, V>
545where
546    K: Encodable<S> + PartialEq + Ord,
547    V: Encodable<S>,
548{
549    fn encode(&self, e: &mut S) {
550        e.emit_usize(self.len());
551        for (key, val) in self {
552            key.encode(e);
553            val.encode(e);
554        }
555    }
556}
557
558impl<D: Decoder, K, V> Decodable<D> for BTreeMap<K, V>
559where
560    K: Decodable<D> + PartialEq + Ord,
561    V: Decodable<D>,
562{
563    fn decode(d: &mut D) -> BTreeMap<K, V> {
564        let len = d.read_usize();
565        (0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
566    }
567}
568
569impl<S: Encoder, T> Encodable<S> for BTreeSet<T>
570where
571    T: Encodable<S> + PartialEq + Ord,
572{
573    fn encode(&self, s: &mut S) {
574        s.emit_usize(self.len());
575        for e in self {
576            e.encode(s);
577        }
578    }
579}
580
581impl<D: Decoder, T> Decodable<D> for BTreeSet<T>
582where
583    T: Decodable<D> + PartialEq + Ord,
584{
585    fn decode(d: &mut D) -> BTreeSet<T> {
586        let len = d.read_usize();
587        (0..len).map(|_| Decodable::decode(d)).collect()
588    }
589}
590
591impl<E: Encoder, K, V, S> Encodable<E> for HashMap<K, V, S>
592where
593    K: Encodable<E> + Eq,
594    V: Encodable<E>,
595    S: BuildHasher,
596{
597    fn encode(&self, e: &mut E) {
598        e.emit_usize(self.len());
599        for (key, val) in self {
600            key.encode(e);
601            val.encode(e);
602        }
603    }
604}
605
606impl<D: Decoder, K, V, S> Decodable<D> for HashMap<K, V, S>
607where
608    K: Decodable<D> + Hash + Eq,
609    V: Decodable<D>,
610    S: BuildHasher + Default,
611{
612    fn decode(d: &mut D) -> HashMap<K, V, S> {
613        let len = d.read_usize();
614        (0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
615    }
616}
617
618impl<E: Encoder, T, S> Encodable<E> for HashSet<T, S>
619where
620    T: Encodable<E> + Eq,
621    S: BuildHasher,
622{
623    fn encode(&self, s: &mut E) {
624        s.emit_usize(self.len());
625        for e in self {
626            e.encode(s);
627        }
628    }
629}
630
631impl<D: Decoder, T, S> Decodable<D> for HashSet<T, S>
632where
633    T: Decodable<D> + Hash + Eq,
634    S: BuildHasher + Default,
635{
636    fn decode(d: &mut D) -> HashSet<T, S> {
637        let len = d.read_usize();
638        (0..len).map(|_| Decodable::decode(d)).collect()
639    }
640}
641
642impl<E: Encoder, K, V, S> Encodable<E> for indexmap::IndexMap<K, V, S>
643where
644    K: Encodable<E> + Hash + Eq,
645    V: Encodable<E>,
646    S: BuildHasher,
647{
648    fn encode(&self, e: &mut E) {
649        e.emit_usize(self.len());
650        for (key, val) in self {
651            key.encode(e);
652            val.encode(e);
653        }
654    }
655}
656
657impl<D: Decoder, K, V, S> Decodable<D> for indexmap::IndexMap<K, V, S>
658where
659    K: Decodable<D> + Hash + Eq,
660    V: Decodable<D>,
661    S: BuildHasher + Default,
662{
663    fn decode(d: &mut D) -> indexmap::IndexMap<K, V, S> {
664        let len = d.read_usize();
665        (0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
666    }
667}
668
669impl<E: Encoder, T, S> Encodable<E> for indexmap::IndexSet<T, S>
670where
671    T: Encodable<E> + Hash + Eq,
672    S: BuildHasher,
673{
674    fn encode(&self, s: &mut E) {
675        s.emit_usize(self.len());
676        for e in self {
677            e.encode(s);
678        }
679    }
680}
681
682impl<D: Decoder, T, S> Decodable<D> for indexmap::IndexSet<T, S>
683where
684    T: Decodable<D> + Hash + Eq,
685    S: BuildHasher + Default,
686{
687    fn decode(d: &mut D) -> indexmap::IndexSet<T, S> {
688        let len = d.read_usize();
689        (0..len).map(|_| Decodable::decode(d)).collect()
690    }
691}
692
693impl<E: Encoder, T: Encodable<E>> Encodable<E> for Rc<[T]> {
694    fn encode(&self, s: &mut E) {
695        let slice: &[T] = self;
696        slice.encode(s);
697    }
698}
699
700impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<[T]> {
701    fn decode(d: &mut D) -> Rc<[T]> {
702        let vec: Vec<T> = Decodable::decode(d);
703        vec.into()
704    }
705}
706
707impl<E: Encoder, T: Encodable<E>> Encodable<E> for Arc<[T]> {
708    fn encode(&self, s: &mut E) {
709        let slice: &[T] = self;
710        slice.encode(s);
711    }
712}
713
714impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<[T]> {
715    fn decode(d: &mut D) -> Arc<[T]> {
716        let vec: Vec<T> = Decodable::decode(d);
717        vec.into()
718    }
719}
720
721impl<S: Encoder> Encodable<S> for Hash64 {
722    #[inline]
723    fn encode(&self, s: &mut S) {
724        s.emit_raw_bytes(&self.as_u64().to_le_bytes());
725    }
726}
727
728impl<S: Encoder> Encodable<S> for Hash128 {
729    #[inline]
730    fn encode(&self, s: &mut S) {
731        s.emit_raw_bytes(&self.as_u128().to_le_bytes());
732    }
733}
734
735impl<D: Decoder> Decodable<D> for Hash64 {
736    #[inline]
737    fn decode(d: &mut D) -> Self {
738        Self::new(u64::from_le_bytes(d.read_raw_bytes(8).try_into().unwrap()))
739    }
740}
741
742impl<D: Decoder> Decodable<D> for Hash128 {
743    #[inline]
744    fn decode(d: &mut D) -> Self {
745        Self::new(u128::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap()))
746    }
747}