Skip to content

Commit f4e538f

Browse files
committed
RUST-765 from_reader and to_writer take reader/writer by value (C-RW-VALUE)
1 parent 98798d0 commit f4e538f

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

src/document.rs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,14 +500,29 @@ impl Document {
500500
}
501501

502502
/// Attempts to serialize the `Document` into a byte stream.
503-
pub fn to_writer<W: Write + ?Sized>(&self, writer: &mut W) -> crate::ser::Result<()> {
503+
///
504+
/// While the method signature indicates an owned writer must be passed in, a mutable reference
505+
/// may also be passed in due to blanket implementations of `Write` provided in the standard
506+
/// library.
507+
///
508+
/// ```
509+
/// # fn main() -> bson::ser::Result<()> {
510+
/// use bson::doc;
511+
///
512+
/// let mut v: Vec<u8> = Vec::new();
513+
/// let doc = doc! { "x" : 1 };
514+
/// doc.to_writer(&mut v)?;
515+
/// # Ok(())
516+
/// # }
517+
/// ```
518+
pub fn to_writer<W: Write>(&self, mut writer: W) -> crate::ser::Result<()> {
504519
let mut buf = Vec::new();
505520
for (key, val) in self.into_iter() {
506521
serialize_bson(&mut buf, key.as_ref(), val)?;
507522
}
508523

509524
write_i32(
510-
writer,
525+
&mut writer,
511526
(buf.len() + mem::size_of::<i32>() + mem::size_of::<u8>()) as i32,
512527
)?;
513528
writer.write_all(&buf)?;
@@ -551,8 +566,35 @@ impl Document {
551566
}
552567

553568
/// Attempts to deserialize a `Document` from a byte stream.
554-
pub fn from_reader<R: Read + ?Sized>(reader: &mut R) -> crate::de::Result<Document> {
555-
Self::decode(reader, false)
569+
///
570+
/// While the method signature indicates an owned reader must be passed in, a mutable reference
571+
/// may also be passed in due to blanket implementations of `Read` provided in the standard
572+
/// library.
573+
///
574+
/// ```
575+
/// # use std::error::Error;
576+
/// # fn main() -> std::result::Result<(), Box<dyn Error>> {
577+
/// use bson::{doc, Document};
578+
/// use std::io::Cursor;
579+
///
580+
/// let mut v: Vec<u8> = Vec::new();
581+
/// let doc = doc! { "x" : 1 };
582+
/// doc.to_writer(&mut v)?;
583+
///
584+
/// // read from mutable reference
585+
/// let mut reader = Cursor::new(v.clone());
586+
/// let doc1 = Document::from_reader(&mut reader)?;
587+
///
588+
/// // read from owned value
589+
/// let doc2 = Document::from_reader(Cursor::new(v))?;
590+
///
591+
/// assert_eq!(doc, doc1);
592+
/// assert_eq!(doc, doc2);
593+
/// # Ok(())
594+
/// # }
595+
/// ```
596+
pub fn from_reader<R: Read>(mut reader: R) -> crate::de::Result<Document> {
597+
Self::decode(&mut reader, false)
556598
}
557599

558600
/// Attempt to deserialize a `Document` that may contain invalid UTF-8 strings from a byte
@@ -561,8 +603,8 @@ impl Document {
561603
/// This is mainly useful when reading raw BSON returned from a MongoDB server, which
562604
/// in rare cases can contain invalidly truncated strings (https://jira.mongodb.org/browse/SERVER-24007).
563605
/// For most use cases, `Document::from_reader` can be used instead.
564-
pub fn from_reader_utf8_lossy<R: Read + ?Sized>(reader: &mut R) -> crate::de::Result<Document> {
565-
Self::decode(reader, true)
606+
pub fn from_reader_utf8_lossy<R: Read>(mut reader: R) -> crate::de::Result<Document> {
607+
Self::decode(&mut reader, true)
566608
}
567609
}
568610

0 commit comments

Comments
 (0)