Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ pub use self::{
},
decimal128::Decimal128,
raw::{
RawBson, RawArray, RawArrayBuf, RawBinaryRef, RawBsonRef, RawDbPointerRef, RawDocument,
RawArray, RawArrayBuf, RawBinaryRef, RawBson, RawBsonRef, RawDbPointerRef, RawDocument,
RawDocumentBuf, RawJavaScriptCodeWithScopeRef, RawRegexRef,
},
ser::{
to_bson, to_bson_with_options, to_document, to_document_with_options, to_vec, Serializer,
SerializerOptions,
to_bson, to_bson_with_options, to_document, to_document_with_options, to_raw_document_buf,
to_vec, Serializer, SerializerOptions,
},
uuid::{Uuid, UuidRepresentation},
};
Expand Down
26 changes: 26 additions & 0 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::{
de::MAX_BSON_SIZE,
spec::BinarySubtype,
Binary,
RawDocumentBuf,
};
use ::serde::{ser::Error as SerdeError, Serialize};

Expand Down Expand Up @@ -283,3 +284,28 @@ where
value.serialize(&mut serializer)?;
Ok(serializer.into_vec())
}

/// Serialize the given `T` as a [`RawDocumentBuf`].
///
/// ```rust
/// use serde::Serialize;
/// use bson::rawdoc;
///
/// #[derive(Serialize)]
/// struct Cat {
/// name: String,
/// age: i32
/// }
///
/// let cat = Cat { name: "Garfield".to_string(), age: 43 };
/// let doc = bson::to_raw_document_buf(&cat)?;
/// assert_eq!(doc, rawdoc! { "name": "Garfield", "age": 43 });
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn to_raw_document_buf<T>(value: &T) -> Result<RawDocumentBuf>
where
T: Serialize,
{
RawDocumentBuf::from_bytes(to_vec(value)?).map_err(Error::custom)
}