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