1use serde;
2use std::io::{Read, Write};
3use std::marker::PhantomData;
4
5use config::{Infinite, InternalOptions, Options, SizeLimit, TrailingBytes};
6use de::read::BincodeRead;
7use Result;
8
9pub(crate) fn serialize_into<W, T: ?Sized, O>(writer: W, value: &T, mut options: O) -> Result<()>
10where
11 W: Write,
12 T: serde::Serialize,
13 O: InternalOptions,
14{
15 if options.limit().limit().is_some() {
16 // "compute" the size for the side-effect
17 // of returning Err if the bound was reached.
18 serialized_size(value, &mut options)?;
19 }
20
21 let mut serializer: Serializer = ::ser::Serializer::<_, O>::new(w:writer, options);
22 serde::Serialize::serialize(self:value, &mut serializer)
23}
24
25pub(crate) fn serialize<T: ?Sized, O>(value: &T, mut options: O) -> Result<Vec<u8>>
26where
27 T: serde::Serialize,
28 O: InternalOptions,
29{
30 let mut writer: Vec = {
31 let actual_size: u64 = serialized_size(value, &mut options)?;
32 Vec::with_capacity(actual_size as usize)
33 };
34
35 serialize_into(&mut writer, value, options:options.with_no_limit())?;
36 Ok(writer)
37}
38
39pub(crate) fn serialized_size<T: ?Sized, O: InternalOptions>(value: &T, options: O) -> Result<u64>
40where
41 T: serde::Serialize,
42{
43 let mut size_counter: SizeChecker = ::ser::SizeChecker { options, total: 0 };
44
45 let result: Result<(), Box> = value.serialize(&mut size_counter);
46 result.map(|_| size_counter.total)
47}
48
49pub(crate) fn deserialize_from<R, T, O>(reader: R, options: O) -> Result<T>
50where
51 R: Read,
52 T: serde::de::DeserializeOwned,
53 O: InternalOptions,
54{
55 deserialize_from_seed(seed:PhantomData, reader, options)
56}
57
58pub(crate) fn deserialize_from_seed<'a, R, T, O>(seed: T, reader: R, options: O) -> Result<T::Value>
59where
60 R: Read,
61 T: serde::de::DeserializeSeed<'a>,
62 O: InternalOptions,
63{
64 let reader: IoReader = ::de::read::IoReader::new(reader);
65 deserialize_from_custom_seed(seed, reader, options)
66}
67
68pub(crate) fn deserialize_from_custom<'a, R, T, O>(reader: R, options: O) -> Result<T>
69where
70 R: BincodeRead<'a>,
71 T: serde::de::DeserializeOwned,
72 O: InternalOptions,
73{
74 deserialize_from_custom_seed(seed:PhantomData, reader, options)
75}
76
77pub(crate) fn deserialize_from_custom_seed<'a, R, T, O>(
78 seed: T,
79 reader: R,
80 options: O,
81) -> Result<T::Value>
82where
83 R: BincodeRead<'a>,
84 T: serde::de::DeserializeSeed<'a>,
85 O: InternalOptions,
86{
87 let mut deserializer: Deserializer = ::de::Deserializer::<_, O>::with_bincode_read(r:reader, options);
88 seed.deserialize(&mut deserializer)
89}
90
91pub(crate) fn deserialize_in_place<'a, R, T, O>(reader: R, options: O, place: &mut T) -> Result<()>
92where
93 R: BincodeRead<'a>,
94 T: serde::de::Deserialize<'a>,
95 O: InternalOptions,
96{
97 let mut deserializer: Deserializer = ::de::Deserializer::<_, _>::with_bincode_read(r:reader, options);
98 serde::Deserialize::deserialize_in_place(&mut deserializer, place)
99}
100
101pub(crate) fn deserialize<'a, T, O>(bytes: &'a [u8], options: O) -> Result<T>
102where
103 T: serde::de::Deserialize<'a>,
104 O: InternalOptions,
105{
106 deserialize_seed(seed:PhantomData, bytes, options)
107}
108
109pub(crate) fn deserialize_seed<'a, T, O>(seed: T, bytes: &'a [u8], options: O) -> Result<T::Value>
110where
111 T: serde::de::DeserializeSeed<'a>,
112 O: InternalOptions,
113{
114 let options: WithOtherLimit = ::config::WithOtherLimit::new(options, limit:Infinite);
115
116 let reader: SliceReader<'_> = ::de::read::SliceReader::new(bytes);
117 let mut deserializer: Deserializer, …> = ::de::Deserializer::with_bincode_read(r:reader, options);
118 let val: ::Value = seed.deserialize(&mut deserializer)?;
119
120 match O::Trailing::check_end(&deserializer.reader) {
121 Ok(_) => Ok(val),
122 Err(err: Box) => Err(err),
123 }
124}
125