Skip to content

Commit 25d110f

Browse files
committed
Add serialize method that takes a supplied vec
1 parent 72035bc commit 25d110f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/ser/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,18 @@ where
283283
Ok(serializer.into_vec())
284284
}
285285

286+
/// Serialize the given `T` as a BSON byte vector into the provided `Vec`.
287+
#[inline]
288+
pub fn into_vec<T>(value: &T, vec: Vec<u8>) -> Result<Vec<u8>>
289+
where
290+
T: Serialize,
291+
{
292+
let mut serializer = raw::Serializer::with_vec(vec);
293+
value.serialize(&mut serializer)?;
294+
Ok(serializer.into_vec())
295+
}
296+
297+
286298
/// Serialize the given `T` as a [`RawDocumentBuf`].
287299
///
288300
/// ```rust

src/ser/raw/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ impl SerializerHint {
5656

5757
impl Serializer {
5858
pub(crate) fn new() -> Self {
59+
Self::with_vec(Vec::new())
60+
}
61+
62+
pub(crate) fn with_vec(mut bytes: Vec<u8>) -> Self {
63+
bytes.clear();
5964
Self {
60-
bytes: Vec::new(),
65+
bytes,
6166
type_index: 0,
6267
hint: SerializerHint::None,
6368
}

0 commit comments

Comments
 (0)