Skip to content

Commit 1c33eb5

Browse files
committed
propagate infallibility
1 parent d32ccc1 commit 1c33eb5

File tree

7 files changed

+53
-54
lines changed

7 files changed

+53
-54
lines changed

src/raw/bson_ref.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl<'a> RawBsonRef<'a> {
293293
}
294294

295295
#[inline]
296-
pub(crate) fn append_to(self, dest: &mut Vec<u8>) -> Result<()> {
296+
pub(crate) fn append_to(self, dest: &mut Vec<u8>) {
297297
match self {
298298
Self::Int32(val) => dest.extend(val.to_le_bytes()),
299299
Self::Int64(val) => dest.extend(val.to_le_bytes()),
@@ -333,7 +333,6 @@ impl<'a> RawBsonRef<'a> {
333333
}
334334
Self::Null | Self::Undefined | Self::MinKey | Self::MaxKey => {}
335335
}
336-
Ok(())
337336
}
338337
}
339338

src/raw/document_buf.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ impl RawDocumentBuf {
220220
key: impl AsRef<str>,
221221
value: impl BindRawBsonRef,
222222
) -> crate::error::Result<()> {
223-
value.bind(|value_ref| {
224-
raw_writer::RawWriter::new(&mut self.data).append(key.as_ref(), value_ref)
225-
})
223+
let key = key.as_ref().try_into()?;
224+
Ok(value
225+
.bind(|value_ref| raw_writer::RawWriter::new(&mut self.data).append(key, value_ref)))
226226
}
227227
}
228228

src/raw/document_buf/raw_writer.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{raw::write_cstring, RawBsonRef};
1+
use crate::{raw::CStr, RawBsonRef};
22

33
pub(super) struct RawWriter<'a> {
44
data: &'a mut Vec<u8>,
@@ -9,19 +9,17 @@ impl<'a> RawWriter<'a> {
99
Self { data }
1010
}
1111

12-
pub(super) fn append(&mut self, key: &str, value: RawBsonRef) -> crate::error::Result<()> {
12+
pub(super) fn append(&mut self, key: &CStr, value: RawBsonRef) {
1313
let original_len = self.data.len();
1414
self.data[original_len - 1] = value.element_type() as u8;
1515

16-
write_cstring(self.data, key)?;
17-
value.append_to(self.data)?;
16+
key.append_to(self.data);
17+
value.append_to(self.data);
1818

1919
// append trailing null byte
2020
self.data.push(0);
2121
// update length
2222
let new_len = (self.data.len() as i32).to_le_bytes();
2323
self.data[0..4].copy_from_slice(&new_len);
24-
25-
Ok(())
2624
}
2725
}

src/raw/serde/seeded_visitor.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,7 @@ impl<'de> Visitor<'de> for SeededVisitor<'_, 'de> {
273273
// Cases that don't
274274
_ => {
275275
let bson = bson.as_ref();
276-
bson.append_to(self.buffer.get_owned_buffer())
277-
.map_err(A::Error::custom)?;
276+
bson.append_to(self.buffer.get_owned_buffer());
278277
Ok(bson.element_type())
279278
}
280279
}

src/ser/raw.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use serde::{
99
use self::value_serializer::{ValueSerializer, ValueType};
1010

1111
use crate::{
12-
raw::{write_cstring, RAW_ARRAY_NEWTYPE, RAW_DOCUMENT_NEWTYPE},
12+
raw::{write_cstring, CStr, RAW_ARRAY_NEWTYPE, RAW_DOCUMENT_NEWTYPE},
1313
ser::{Error, Result},
1414
serde_helpers::HUMAN_READABLE_NEWTYPE,
1515
spec::{BinarySubtype, ElementType},
@@ -108,7 +108,7 @@ impl Serializer {
108108

109109
fn serialize_raw(&mut self, v: RawBsonRef) -> Result<()> {
110110
self.update_element_type(v.element_type())?;
111-
v.append_to(&mut self.bytes)?;
111+
v.append_to(&mut self.bytes);
112112
Ok(())
113113
}
114114
}
@@ -290,7 +290,7 @@ impl<'a> serde::Serializer for &'a mut Serializer {
290290
T: serde::Serialize + ?Sized,
291291
{
292292
self.update_element_type(ElementType::EmbeddedDocument)?;
293-
let mut d = DocumentSerializer::start(&mut *self)?;
293+
let mut d = DocumentSerializer::start(&mut *self);
294294
d.serialize_entry(variant, value)?;
295295
d.end_doc()?;
296296
Ok(())
@@ -299,7 +299,7 @@ impl<'a> serde::Serializer for &'a mut Serializer {
299299
#[inline]
300300
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
301301
self.update_element_type(ElementType::Array)?;
302-
DocumentSerializer::start(&mut *self)
302+
Ok(DocumentSerializer::start(&mut *self))
303303
}
304304

305305
#[inline]
@@ -325,13 +325,17 @@ impl<'a> serde::Serializer for &'a mut Serializer {
325325
_len: usize,
326326
) -> Result<Self::SerializeTupleVariant> {
327327
self.update_element_type(ElementType::EmbeddedDocument)?;
328-
VariantSerializer::start(&mut *self, variant, VariantInnerType::Tuple)
328+
Ok(VariantSerializer::start(
329+
&mut *self,
330+
variant.try_into()?,
331+
VariantInnerType::Tuple,
332+
))
329333
}
330334

331335
#[inline]
332336
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
333337
self.update_element_type(ElementType::EmbeddedDocument)?;
334-
DocumentSerializer::start(&mut *self)
338+
Ok(DocumentSerializer::start(&mut *self))
335339
}
336340

337341
#[inline]
@@ -360,7 +364,7 @@ impl<'a> serde::Serializer for &'a mut Serializer {
360364
)?;
361365
match value_type {
362366
Some(vt) => Ok(StructSerializer::Value(ValueSerializer::new(self, vt))),
363-
None => Ok(StructSerializer::Document(DocumentSerializer::start(self)?)),
367+
None => Ok(StructSerializer::Document(DocumentSerializer::start(self))),
364368
}
365369
}
366370

@@ -373,7 +377,11 @@ impl<'a> serde::Serializer for &'a mut Serializer {
373377
_len: usize,
374378
) -> Result<Self::SerializeStructVariant> {
375379
self.update_element_type(ElementType::EmbeddedDocument)?;
376-
VariantSerializer::start(&mut *self, variant, VariantInnerType::Struct)
380+
Ok(VariantSerializer::start(
381+
&mut *self,
382+
variant.try_into()?,
383+
VariantInnerType::Struct,
384+
))
377385
}
378386
}
379387

@@ -431,32 +439,28 @@ pub(crate) struct VariantSerializer<'a> {
431439
}
432440

433441
impl<'a> VariantSerializer<'a> {
434-
fn start(
435-
rs: &'a mut Serializer,
436-
variant: &'static str,
437-
inner_type: VariantInnerType,
438-
) -> Result<Self> {
442+
fn start(rs: &'a mut Serializer, variant: &'static CStr, inner_type: VariantInnerType) -> Self {
439443
let doc_start = rs.bytes.len();
440444
// write placeholder length for document, will be updated at end
441445
static ZERO: RawBsonRef = RawBsonRef::Int32(0);
442-
ZERO.append_to(&mut rs.bytes)?;
446+
ZERO.append_to(&mut rs.bytes);
443447

444448
let inner = match inner_type {
445449
VariantInnerType::Struct => ElementType::EmbeddedDocument,
446450
VariantInnerType::Tuple => ElementType::Array,
447451
};
448452
rs.bytes.push(inner as u8);
449-
write_cstring(&mut rs.bytes, variant)?;
453+
variant.append_to(&mut rs.bytes);
450454
let inner_start = rs.bytes.len();
451455
// write placeholder length for inner, will be updated at end
452-
ZERO.append_to(&mut rs.bytes)?;
456+
ZERO.append_to(&mut rs.bytes);
453457

454-
Ok(Self {
458+
Self {
455459
root_serializer: rs,
456460
num_elements_serialized: 0,
457461
doc_start,
458462
inner_start,
459-
})
463+
}
460464
}
461465

462466
#[inline]

src/ser/raw/document_serializer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ pub(crate) struct DocumentSerializer<'a> {
2020
}
2121

2222
impl<'a> DocumentSerializer<'a> {
23-
pub(crate) fn start(rs: &'a mut Serializer) -> crate::ser::Result<Self> {
23+
pub(crate) fn start(rs: &'a mut Serializer) -> Self {
2424
let start = rs.bytes.len();
25-
RawBsonRef::Int32(0).append_to(&mut rs.bytes)?;
26-
Ok(Self {
25+
RawBsonRef::Int32(0).append_to(&mut rs.bytes);
26+
Self {
2727
root_serializer: rs,
2828
num_keys_serialized: 0,
2929
start,
30-
})
30+
}
3131
}
3232

3333
/// Serialize a document key using the provided closure.

src/ser/raw/value_serializer.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<'b> serde::Serializer for &'b mut ValueSerializer<'_> {
199199
let increment = u32::try_from(v).map_err(Error::custom)?;
200200

201201
RawBsonRef::Timestamp(crate::Timestamp { time, increment })
202-
.append_to(&mut self.root_serializer.bytes)?;
202+
.append_to(&mut self.root_serializer.bytes);
203203

204204
Ok(())
205205
}
@@ -215,7 +215,7 @@ impl<'b> serde::Serializer for &'b mut ValueSerializer<'_> {
215215
subtype: v.into(),
216216
bytes,
217217
};
218-
RawBsonRef::Binary(binary).append_to(&mut self.root_serializer.bytes)?;
218+
RawBsonRef::Binary(binary).append_to(&mut self.root_serializer.bytes);
219219
self.state = SerializationStep::Done;
220220
Ok(())
221221
}
@@ -258,11 +258,11 @@ impl<'b> serde::Serializer for &'b mut ValueSerializer<'_> {
258258
SerializationStep::DateTimeNumberLong => {
259259
let millis = v.parse().map_err(Error::custom)?;
260260
RawBsonRef::DateTime(crate::DateTime::from_millis(millis))
261-
.append_to(&mut self.root_serializer.bytes)?;
261+
.append_to(&mut self.root_serializer.bytes);
262262
}
263263
SerializationStep::Oid => {
264264
let oid = ObjectId::parse_str(v).map_err(Error::custom)?;
265-
RawBsonRef::ObjectId(oid).append_to(&mut self.root_serializer.bytes)?;
265+
RawBsonRef::ObjectId(oid).append_to(&mut self.root_serializer.bytes);
266266
}
267267
SerializationStep::BinaryBytes => {
268268
self.state = SerializationStep::BinarySubType {
@@ -274,18 +274,18 @@ impl<'b> serde::Serializer for &'b mut ValueSerializer<'_> {
274274
let subtype: BinarySubtype = subtype_byte[0].into();
275275
let bytes = &base64::decode(base64.as_str()).map_err(Error::custom)?;
276276
let binary = RawBinaryRef { subtype, bytes };
277-
RawBsonRef::Binary(binary).append_to(&mut self.root_serializer.bytes)?;
277+
RawBsonRef::Binary(binary).append_to(&mut self.root_serializer.bytes);
278278
}
279279
SerializationStep::Symbol => {
280-
RawBsonRef::Symbol(v).append_to(&mut self.root_serializer.bytes)?;
280+
RawBsonRef::Symbol(v).append_to(&mut self.root_serializer.bytes);
281281
}
282282
SerializationStep::DbPointerRef => {
283283
self.state = SerializationStep::DbPointerId { ns: v.to_owned() };
284284
}
285285
SerializationStep::DbPointerId { ns } => {
286286
let id = ObjectId::parse_str(v).map_err(Error::custom)?;
287287
RawBsonRef::DbPointer(crate::RawDbPointerRef { namespace: ns, id })
288-
.append_to(&mut self.root_serializer.bytes)?;
288+
.append_to(&mut self.root_serializer.bytes);
289289
}
290290
SerializationStep::RegExPattern => {
291291
self.state = SerializationStep::RegExOptions {
@@ -301,10 +301,10 @@ impl<'b> serde::Serializer for &'b mut ValueSerializer<'_> {
301301
pattern: pattern.as_ref(),
302302
options: options.as_str().try_into()?,
303303
})
304-
.append_to(&mut self.root_serializer.bytes)?;
304+
.append_to(&mut self.root_serializer.bytes);
305305
}
306306
SerializationStep::Code => {
307-
RawBsonRef::JavaScriptCode(v).append_to(&mut self.root_serializer.bytes)?;
307+
RawBsonRef::JavaScriptCode(v).append_to(&mut self.root_serializer.bytes);
308308
}
309309
SerializationStep::CodeWithScopeCode => {
310310
self.state = SerializationStep::CodeWithScopeScope {
@@ -327,7 +327,7 @@ impl<'b> serde::Serializer for &'b mut ValueSerializer<'_> {
327327
match self.state {
328328
SerializationStep::Decimal128Value => {
329329
let dec = crate::Decimal128::from_bytes(v.try_into().map_err(Error::custom)?);
330-
RawBsonRef::Decimal128(dec).append_to(&mut self.root_serializer.bytes)?;
330+
RawBsonRef::Decimal128(dec).append_to(&mut self.root_serializer.bytes);
331331
Ok(())
332332
}
333333
SerializationStep::BinaryBytes => {
@@ -339,8 +339,7 @@ impl<'b> serde::Serializer for &'b mut ValueSerializer<'_> {
339339
code,
340340
scope: RawDocument::decode_from_bytes(v).map_err(Error::custom)?,
341341
};
342-
RawBsonRef::JavaScriptCodeWithScope(raw)
343-
.append_to(&mut self.root_serializer.bytes)?;
342+
RawBsonRef::JavaScriptCodeWithScope(raw).append_to(&mut self.root_serializer.bytes);
344343
self.state = SerializationStep::Done;
345344
Ok(())
346345
}
@@ -448,9 +447,9 @@ impl<'b> serde::Serializer for &'b mut ValueSerializer<'_> {
448447
#[inline]
449448
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
450449
match self.state {
451-
SerializationStep::CodeWithScopeScope { ref code, raw } if !raw => {
452-
CodeWithScopeSerializer::start(code.as_str(), self.root_serializer)
453-
}
450+
SerializationStep::CodeWithScopeScope { ref code, raw } if !raw => Ok(
451+
CodeWithScopeSerializer::start(code.as_str(), self.root_serializer),
452+
),
454453
_ => Err(self.invalid_step("map")),
455454
}
456455
}
@@ -613,13 +612,13 @@ pub(crate) struct CodeWithScopeSerializer<'a> {
613612

614613
impl<'a> CodeWithScopeSerializer<'a> {
615614
#[inline]
616-
fn start(code: &str, rs: &'a mut Serializer) -> Result<Self> {
615+
fn start(code: &str, rs: &'a mut Serializer) -> Self {
617616
let start = rs.bytes.len();
618-
RawBsonRef::Int32(0).append_to(&mut rs.bytes)?; // placeholder length
617+
RawBsonRef::Int32(0).append_to(&mut rs.bytes); // placeholder length
619618
write_string(&mut rs.bytes, code);
620619

621-
let doc = DocumentSerializer::start(rs)?;
622-
Ok(Self { start, doc })
620+
let doc = DocumentSerializer::start(rs);
621+
Self { start, doc }
623622
}
624623
}
625624

0 commit comments

Comments
 (0)