Skip to content

Commit 2bf0c30

Browse files
authored
minor: expose raw iterator value bytes (#648)
1 parent 37b60c1 commit 2bf0c30

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

src/de/raw.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl<'de> serde::de::Deserializer<'de> for RawDeserializer<'de> {
255255
V: serde::de::Visitor<'de>,
256256
{
257257
match self.element.element_type() {
258-
ElementType::ObjectId => visitor.visit_borrowed_bytes(self.element.slice()),
258+
ElementType::ObjectId => visitor.visit_borrowed_bytes(self.element.value_bytes()),
259259
_ => self.deserialize_any(visitor),
260260
}
261261
}

src/raw/iter.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -184,31 +184,31 @@ impl<'a> RawElement<'a> {
184184
ElementType::MinKey => RawBsonRef::MinKey,
185185
ElementType::MaxKey => RawBsonRef::MaxKey,
186186
ElementType::ObjectId => RawBsonRef::ObjectId(self.get_oid_at(self.start_at)?),
187-
ElementType::Int32 => RawBsonRef::Int32(i32_from_slice(self.slice())?),
188-
ElementType::Int64 => RawBsonRef::Int64(i64_from_slice(self.slice())?),
189-
ElementType::Double => RawBsonRef::Double(f64_from_slice(self.slice())?),
187+
ElementType::Int32 => RawBsonRef::Int32(i32_from_slice(self.value_bytes())?),
188+
ElementType::Int64 => RawBsonRef::Int64(i64_from_slice(self.value_bytes())?),
189+
ElementType::Double => RawBsonRef::Double(f64_from_slice(self.value_bytes())?),
190190
ElementType::String => RawBsonRef::String(self.read_str()?),
191191
ElementType::EmbeddedDocument => {
192-
RawBsonRef::Document(RawDocument::from_bytes(self.slice())?)
193-
}
194-
ElementType::Array => {
195-
RawBsonRef::Array(RawArray::from_doc(RawDocument::from_bytes(self.slice())?))
192+
RawBsonRef::Document(RawDocument::from_bytes(self.value_bytes())?)
196193
}
194+
ElementType::Array => RawBsonRef::Array(RawArray::from_doc(RawDocument::from_bytes(
195+
self.value_bytes(),
196+
)?)),
197197
ElementType::Boolean => RawBsonRef::Boolean(
198-
bool_from_slice(self.slice()).map_err(|e| self.malformed_error(e))?,
198+
bool_from_slice(self.value_bytes()).map_err(|e| self.malformed_error(e))?,
199199
),
200200
ElementType::DateTime => {
201-
RawBsonRef::DateTime(DateTime::from_millis(i64_from_slice(self.slice())?))
201+
RawBsonRef::DateTime(DateTime::from_millis(i64_from_slice(self.value_bytes())?))
202202
}
203203
ElementType::Decimal128 => RawBsonRef::Decimal128(Decimal128::from_bytes(
204-
self.slice()
204+
self.value_bytes()
205205
.try_into()
206206
.map_err(|e| self.malformed_error(e))?,
207207
)),
208208
ElementType::JavaScriptCode => RawBsonRef::JavaScriptCode(self.read_str()?),
209209
ElementType::Symbol => RawBsonRef::Symbol(self.read_str()?),
210210
ElementType::DbPointer => RawBsonRef::DbPointer(RawDbPointerRef {
211-
namespace: read_lenencode(self.slice())?,
211+
namespace: read_lenencode(self.value_bytes())?,
212212
id: self.get_oid_at(self.start_at + (self.size - 12))?,
213213
}),
214214
ElementType::RegularExpression => {
@@ -221,7 +221,7 @@ impl<'a> RawElement<'a> {
221221
})
222222
}
223223
ElementType::Timestamp => RawBsonRef::Timestamp({
224-
let bytes: [u8; 8] = self.slice()[0..8]
224+
let bytes: [u8; 8] = self.value_bytes()[0..8]
225225
.try_into()
226226
.map_err(|e| self.malformed_error(e))?;
227227
Timestamp::from_le_bytes(bytes)
@@ -268,7 +268,7 @@ impl<'a> RawElement<'a> {
268268
return Err(self.malformed_error("code with scope length too small"));
269269
}
270270

271-
let slice = self.slice();
271+
let slice = self.value_bytes();
272272
let code = read_lenencode(&slice[4..])?;
273273
let scope_start = 4 + 4 + code.len() + 1;
274274
let scope = RawDocument::from_bytes(&slice[scope_start..])?;
@@ -296,7 +296,7 @@ impl<'a> RawElement<'a> {
296296
return Err(self.malformed_error("code with scope length too small"));
297297
}
298298

299-
let slice = self.slice();
299+
let slice = self.value_bytes();
300300
let code = String::from_utf8_lossy(read_lenencode_bytes(&slice[4..])?).into_owned();
301301
let scope_start = 4 + 4 + code.len() + 1;
302302
if scope_start >= slice.len() {
@@ -311,7 +311,7 @@ impl<'a> RawElement<'a> {
311311
}
312312
ElementType::Symbol => Utf8LossyBson::Symbol(self.read_utf8_lossy()),
313313
ElementType::DbPointer => Utf8LossyBson::DbPointer(crate::DbPointer {
314-
namespace: String::from_utf8_lossy(read_lenencode_bytes(self.slice())?)
314+
namespace: String::from_utf8_lossy(read_lenencode_bytes(self.value_bytes())?)
315315
.into_owned(),
316316
id: self.get_oid_at(self.start_at + (self.size - 12))?,
317317
}),
@@ -336,7 +336,8 @@ impl<'a> RawElement<'a> {
336336
Error::malformed_bytes(e).with_key(self.key.as_str())
337337
}
338338

339-
pub(crate) fn slice(&self) -> &'a [u8] {
339+
/// The raw bytes of the value.
340+
pub fn value_bytes(&self) -> &'a [u8] {
340341
self.slice_bounds(self.start_at, self.size)
341342
}
342343

0 commit comments

Comments
 (0)