Skip to content

Commit 39d90f6

Browse files
authored
RUST-1992 Convert raw deserializer to use raw document iteration (#483)
1 parent b554142 commit 39d90f6

File tree

10 files changed

+518
-915
lines changed

10 files changed

+518
-915
lines changed

src/de/error.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ pub enum Error {
3838
},
3939
}
4040

41+
impl Error {
42+
pub(crate) fn deserialization(msg: impl ToString) -> Self {
43+
Self::DeserializationError {
44+
message: msg.to_string(),
45+
}
46+
}
47+
}
48+
4149
impl From<io::Error> for Error {
4250
fn from(err: io::Error) -> Error {
4351
Error::Io(Arc::new(err))

src/de/mod.rs

Lines changed: 4 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ use std::io::Read;
3434

3535
use crate::{
3636
bson::{Bson, Document, Timestamp},
37-
oid::ObjectId,
38-
raw::RawBinaryRef,
3937
ser::write_i32,
4038
spec::BinarySubtype,
41-
Decimal128,
4239
};
4340

4441
use ::serde::{
@@ -71,38 +68,6 @@ enum DeserializerHint {
7168
RawBson,
7269
}
7370

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-
10671
pub(crate) fn read_bool<R: Read>(mut reader: R) -> Result<bool> {
10772
let val = read_u8(&mut reader)?;
10873
if val > 1 {
@@ -129,73 +94,6 @@ pub(crate) fn read_i32<R: Read + ?Sized>(reader: &mut R) -> Result<i32> {
12994
Ok(i32::from_le_bytes(buf))
13095
}
13196

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-
19997
impl Timestamp {
20098
pub(crate) fn from_reader<R: Read>(mut reader: R) -> Result<Self> {
20199
let mut bytes = [0; 8];
@@ -204,14 +102,6 @@ impl Timestamp {
204102
}
205103
}
206104

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-
215105
/// Deserialize a `T` from the provided [`Bson`] value.
216106
///
217107
/// 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>
335225
where
336226
T: Deserialize<'de>,
337227
{
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)
340230
}
341231

342232
/// 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>
349239
where
350240
T: Deserialize<'de>,
351241
{
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)
354244
}

0 commit comments

Comments
 (0)