bitcode/serde/
mod.rs

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
use crate::error::{error_from_display, Error};
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::fmt::Display;

mod de;
mod guard;
mod ser;
mod variant;

pub use de::*;
pub use ser::*;

// Saves code size over calling panic in type_changed macro directly.
enum Never {}
#[cold]
#[inline(never)]
fn panic_type_changed() -> Never {
    panic!("type changed")
}
macro_rules! type_changed {
    () => {
        #[allow(unreachable_code)]
        match super::panic_type_changed() {} // ! is unstable.
    };
}
use type_changed;

fn default_box_slice<T: Default>(len: usize) -> Box<[T]> {
    let mut vec = vec![];
    vec.resize_with(len, Default::default);
    vec.into()
}

#[inline(always)]
fn get_mut_or_resize<T: Default>(vec: &mut Vec<T>, index: usize) -> &mut T {
    if index >= vec.len() {
        #[cold]
        #[inline(never)]
        fn cold<T: Default>(vec: &mut Vec<T>, index: usize) {
            vec.resize_with(index + 1, Default::default);
        }
        cold(vec, index);
    }
    // Safety we've just resized `vec.len()` to be > than `index`.
    unsafe { vec.get_unchecked_mut(index) }
}

#[cfg(not(feature = "std"))]
impl serde::ser::StdError for Error {}

impl serde::ser::Error for Error {
    fn custom<T>(t: T) -> Self
    where
        T: Display,
    {
        error_from_display(t)
    }
}

impl serde::de::Error for Error {
    fn custom<T>(t: T) -> Self
    where
        T: Display,
    {
        error_from_display(t)
    }
}