@@ -34,11 +34,8 @@ use std::io::Read;
34
34
35
35
use crate :: {
36
36
bson:: { Bson , Document , Timestamp } ,
37
- oid:: ObjectId ,
38
- raw:: RawBinaryRef ,
39
37
ser:: write_i32,
40
38
spec:: BinarySubtype ,
41
- Decimal128 ,
42
39
} ;
43
40
44
41
use :: serde:: {
@@ -71,38 +68,6 @@ enum DeserializerHint {
71
68
RawBson ,
72
69
}
73
70
74
- pub ( crate ) fn read_string < R : Read + ?Sized > ( reader : & mut R , utf8_lossy : bool ) -> Result < String > {
75
- let len = read_i32 ( reader) ?;
76
-
77
- // UTF-8 String must have at least 1 byte (the last 0x00).
78
- if len < 1 {
79
- return Err ( Error :: invalid_length (
80
- len as usize ,
81
- & "UTF-8 string must have at least 1 byte" ,
82
- ) ) ;
83
- }
84
-
85
- let s = if utf8_lossy {
86
- let mut buf = Vec :: with_capacity ( len as usize - 1 ) ;
87
- reader. take ( len as u64 - 1 ) . read_to_end ( & mut buf) ?;
88
- String :: from_utf8_lossy ( & buf) . to_string ( )
89
- } else {
90
- let mut s = String :: with_capacity ( len as usize - 1 ) ;
91
- reader. take ( len as u64 - 1 ) . read_to_string ( & mut s) ?;
92
- s
93
- } ;
94
-
95
- // read the null terminator
96
- if read_u8 ( reader) ? != 0 {
97
- return Err ( Error :: invalid_length (
98
- len as usize ,
99
- & "contents of string longer than provided length" ,
100
- ) ) ;
101
- }
102
-
103
- Ok ( s)
104
- }
105
-
106
71
pub ( crate ) fn read_bool < R : Read > ( mut reader : R ) -> Result < bool > {
107
72
let val = read_u8 ( & mut reader) ?;
108
73
if val > 1 {
@@ -129,73 +94,6 @@ pub(crate) fn read_i32<R: Read + ?Sized>(reader: &mut R) -> Result<i32> {
129
94
Ok ( i32:: from_le_bytes ( buf) )
130
95
}
131
96
132
- #[ inline]
133
- pub ( crate ) fn read_i64 < R : Read + ?Sized > ( reader : & mut R ) -> Result < i64 > {
134
- let mut buf = [ 0 ; 8 ] ;
135
- reader. read_exact ( & mut buf) ?;
136
- Ok ( i64:: from_le_bytes ( buf) )
137
- }
138
-
139
- #[ inline]
140
- fn read_f64 < R : Read + ?Sized > ( reader : & mut R ) -> Result < f64 > {
141
- let mut buf = [ 0 ; 8 ] ;
142
- reader. read_exact ( & mut buf) ?;
143
- Ok ( f64:: from_le_bytes ( buf) )
144
- }
145
-
146
- /// Placeholder decoder for `Decimal128`. Reads 128 bits and just stores them, does no validation or
147
- /// parsing.
148
- #[ inline]
149
- fn read_f128 < R : Read + ?Sized > ( reader : & mut R ) -> Result < Decimal128 > {
150
- let mut buf = [ 0u8 ; 128 / 8 ] ;
151
- reader. read_exact ( & mut buf) ?;
152
- Ok ( Decimal128 { bytes : buf } )
153
- }
154
-
155
- impl < ' a > RawBinaryRef < ' a > {
156
- pub ( crate ) fn from_slice_with_len_and_payload (
157
- mut bytes : & ' a [ u8 ] ,
158
- mut len : i32 ,
159
- subtype : BinarySubtype ,
160
- ) -> Result < Self > {
161
- if !( 0 ..=MAX_BSON_SIZE ) . contains ( & len) {
162
- return Err ( Error :: invalid_length (
163
- len as usize ,
164
- & format ! ( "binary length must be between 0 and {}" , MAX_BSON_SIZE ) . as_str ( ) ,
165
- ) ) ;
166
- } else if len as usize > bytes. len ( ) {
167
- return Err ( Error :: invalid_length (
168
- len as usize ,
169
- & format ! (
170
- "binary length {} exceeds buffer length {}" ,
171
- len,
172
- bytes. len( )
173
- )
174
- . as_str ( ) ,
175
- ) ) ;
176
- }
177
-
178
- // Skip length data in old binary.
179
- if let BinarySubtype :: BinaryOld = subtype {
180
- let data_len = read_i32 ( & mut bytes) ?;
181
-
182
- if data_len + 4 != len {
183
- return Err ( Error :: invalid_length (
184
- data_len as usize ,
185
- & "0x02 length did not match top level binary length" ,
186
- ) ) ;
187
- }
188
-
189
- len -= 4 ;
190
- }
191
-
192
- Ok ( Self {
193
- bytes : & bytes[ 0 ..len as usize ] ,
194
- subtype,
195
- } )
196
- }
197
- }
198
-
199
97
impl Timestamp {
200
98
pub ( crate ) fn from_reader < R : Read > ( mut reader : R ) -> Result < Self > {
201
99
let mut bytes = [ 0 ; 8 ] ;
@@ -204,14 +102,6 @@ impl Timestamp {
204
102
}
205
103
}
206
104
207
- impl ObjectId {
208
- pub ( crate ) fn from_reader < R : Read > ( mut reader : R ) -> Result < Self > {
209
- let mut buf = [ 0u8 ; 12 ] ;
210
- reader. read_exact ( & mut buf) ?;
211
- Ok ( Self :: from_bytes ( buf) )
212
- }
213
- }
214
-
215
105
/// Deserialize a `T` from the provided [`Bson`] value.
216
106
///
217
107
/// The [`Deserializer`] used by this function presents itself as human readable, whereas the
@@ -335,8 +225,8 @@ pub fn from_slice<'de, T>(bytes: &'de [u8]) -> Result<T>
335
225
where
336
226
T : Deserialize < ' de > ,
337
227
{
338
- let mut deserializer = raw:: Deserializer :: new ( bytes, false ) ;
339
- T :: deserialize ( & mut deserializer)
228
+ let deserializer = raw:: Deserializer :: new ( bytes, false ) ? ;
229
+ T :: deserialize ( deserializer)
340
230
}
341
231
342
232
/// Deserialize an instance of type `T` from a slice of BSON bytes, replacing any invalid UTF-8
@@ -349,6 +239,6 @@ pub fn from_slice_utf8_lossy<'de, T>(bytes: &'de [u8]) -> Result<T>
349
239
where
350
240
T : Deserialize < ' de > ,
351
241
{
352
- let mut deserializer = raw:: Deserializer :: new ( bytes, true ) ;
353
- T :: deserialize ( & mut deserializer)
242
+ let deserializer = raw:: Deserializer :: new ( bytes, true ) ? ;
243
+ T :: deserialize ( deserializer)
354
244
}
0 commit comments