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
use serde;
use std::io::{Read, Write};
use std::marker::PhantomData;

use config::{Infinite, InternalOptions, Options, SizeLimit, TrailingBytes};
use de::read::BincodeRead;
use Result;

pub(crate) fn serialize_into<W, T: ?Sized, O>(writer: W, value: &T, mut options: O) -> Result<()>
where
    W: Write,
    T: serde::Serialize,
    O: InternalOptions,
{
    if options.limit().limit().is_some() {
        // "compute" the size for the side-effect
        // of returning Err if the bound was reached.
        serialized_size(value, &mut options)?;
    }

    let mut serializer = ::ser::Serializer::<_, O>::new(writer, options);
    serde::Serialize::serialize(value, &mut serializer)
}

pub(crate) fn serialize<T: ?Sized, O>(value: &T, mut options: O) -> Result<Vec<u8>>
where
    T: serde::Serialize,
    O: InternalOptions,
{
    let mut writer = {
        let actual_size = serialized_size(value, &mut options)?;
        Vec::with_capacity(actual_size as usize)
    };

    serialize_into(&mut writer, value, options.with_no_limit())?;
    Ok(writer)
}

pub(crate) fn serialized_size<T: ?Sized, O: InternalOptions>(value: &T, options: O) -> Result<u64>
where
    T: serde::Serialize,
{
    let mut size_counter = ::ser::SizeChecker { options, total: 0 };

    let result = value.serialize(&mut size_counter);
    result.map(|_| size_counter.total)
}

pub(crate) fn deserialize_from<R, T, O>(reader: R, options: O) -> Result<T>
where
    R: Read,
    T: serde::de::DeserializeOwned,
    O: InternalOptions,
{
    deserialize_from_seed(PhantomData, reader, options)
}

pub(crate) fn deserialize_from_seed<'a, R, T, O>(seed: T, reader: R, options: O) -> Result<T::Value>
where
    R: Read,
    T: serde::de::DeserializeSeed<'a>,
    O: InternalOptions,
{
    let reader = ::de::read::IoReader::new(reader);
    deserialize_from_custom_seed(seed, reader, options)
}

pub(crate) fn deserialize_from_custom<'a, R, T, O>(reader: R, options: O) -> Result<T>
where
    R: BincodeRead<'a>,
    T: serde::de::DeserializeOwned,
    O: InternalOptions,
{
    deserialize_from_custom_seed(PhantomData, reader, options)
}

pub(crate) fn deserialize_from_custom_seed<'a, R, T, O>(
    seed: T,
    reader: R,
    options: O,
) -> Result<T::Value>
where
    R: BincodeRead<'a>,
    T: serde::de::DeserializeSeed<'a>,
    O: InternalOptions,
{
    let mut deserializer = ::de::Deserializer::<_, O>::with_bincode_read(reader, options);
    seed.deserialize(&mut deserializer)
}

pub(crate) fn deserialize_in_place<'a, R, T, O>(reader: R, options: O, place: &mut T) -> Result<()>
where
    R: BincodeRead<'a>,
    T: serde::de::Deserialize<'a>,
    O: InternalOptions,
{
    let mut deserializer = ::de::Deserializer::<_, _>::with_bincode_read(reader, options);
    serde::Deserialize::deserialize_in_place(&mut deserializer, place)
}

pub(crate) fn deserialize<'a, T, O>(bytes: &'a [u8], options: O) -> Result<T>
where
    T: serde::de::Deserialize<'a>,
    O: InternalOptions,
{
    deserialize_seed(PhantomData, bytes, options)
}

pub(crate) fn deserialize_seed<'a, T, O>(seed: T, bytes: &'a [u8], options: O) -> Result<T::Value>
where
    T: serde::de::DeserializeSeed<'a>,
    O: InternalOptions,
{
    let options = ::config::WithOtherLimit::new(options, Infinite);

    let reader = ::de::read::SliceReader::new(bytes);
    let mut deserializer = ::de::Deserializer::with_bincode_read(reader, options);
    let val = seed.deserialize(&mut deserializer)?;

    match O::Trailing::check_end(&deserializer.reader) {
        Ok(_) => Ok(val),
        Err(err) => Err(err),
    }
}