Skip to content

Commit 546fbea

Browse files
authored
RUST-1512 Provide a method to append a borrowed value (#463)
1 parent 5890071 commit 546fbea

File tree

2 files changed

+44
-49
lines changed

2 files changed

+44
-49
lines changed

Diff for: src/raw/array_buf.rs

-4
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ impl RawArrayBuf {
9696
self.inner.append(self.len.to_string(), value);
9797
self.len += 1;
9898
}
99-
100-
pub(crate) fn into_vec(self) -> Vec<u8> {
101-
self.inner.into_bytes()
102-
}
10399
}
104100

105101
impl Debug for RawArrayBuf {

Diff for: src/raw/document_buf.rs

+44-45
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@ use std::{
77

88
use serde::{Deserialize, Serialize};
99

10-
use crate::{
11-
de::MIN_BSON_DOCUMENT_SIZE,
12-
spec::BinarySubtype,
13-
Document,
14-
RawBinaryRef,
15-
RawJavaScriptCodeWithScopeRef,
16-
};
10+
use crate::{de::MIN_BSON_DOCUMENT_SIZE, spec::BinarySubtype, Document};
1711

1812
use super::{
1913
bson::RawBson,
@@ -215,6 +209,18 @@ impl RawDocumentBuf {
215209
/// # Ok::<(), Error>(())
216210
/// ```
217211
pub fn append(&mut self, key: impl AsRef<str>, value: impl Into<RawBson>) {
212+
let value = value.into();
213+
self.append_ref(key, value.as_raw_bson_ref())
214+
}
215+
216+
/// Append a key value pair to the end of the document without checking to see if
217+
/// the key already exists.
218+
///
219+
/// It is a user error to append the same key more than once to the same document, and it may
220+
/// result in errors when communicating with MongoDB.
221+
///
222+
/// If the provided key contains an interior null byte, this method will panic.
223+
pub fn append_ref<'a>(&mut self, key: impl AsRef<str>, value: impl Into<RawBsonRef<'a>>) {
218224
fn append_string(doc: &mut RawDocumentBuf, value: &str) {
219225
doc.data
220226
.extend(((value.as_bytes().len() + 1) as i32).to_le_bytes());
@@ -240,78 +246,71 @@ impl RawDocumentBuf {
240246
let element_type = value.element_type();
241247

242248
match value {
243-
RawBson::Int32(i) => {
249+
RawBsonRef::Int32(i) => {
244250
self.data.extend(i.to_le_bytes());
245251
}
246-
RawBson::String(s) => {
247-
append_string(self, s.as_str());
252+
RawBsonRef::String(s) => {
253+
append_string(self, s);
248254
}
249-
RawBson::Document(d) => {
250-
self.data.extend(d.into_bytes());
255+
RawBsonRef::Document(d) => {
256+
self.data.extend(d.as_bytes());
251257
}
252-
RawBson::Array(a) => {
253-
self.data.extend(a.into_vec());
258+
RawBsonRef::Array(a) => {
259+
self.data.extend(a.as_bytes());
254260
}
255-
RawBson::Binary(b) => {
256-
let len = RawBinaryRef {
257-
bytes: b.bytes.as_slice(),
258-
subtype: b.subtype,
259-
}
260-
.len();
261+
RawBsonRef::Binary(b) => {
262+
let len = b.len();
261263
self.data.extend(len.to_le_bytes());
262264
self.data.push(b.subtype.into());
263265
if let BinarySubtype::BinaryOld = b.subtype {
264266
self.data.extend((len - 4).to_le_bytes())
265267
}
266268
self.data.extend(b.bytes);
267269
}
268-
RawBson::Boolean(b) => {
270+
RawBsonRef::Boolean(b) => {
269271
self.data.push(b as u8);
270272
}
271-
RawBson::DateTime(dt) => {
273+
RawBsonRef::DateTime(dt) => {
272274
self.data.extend(dt.timestamp_millis().to_le_bytes());
273275
}
274-
RawBson::DbPointer(dbp) => {
275-
append_string(self, dbp.namespace.as_str());
276+
RawBsonRef::DbPointer(dbp) => {
277+
append_string(self, dbp.namespace);
276278
self.data.extend(dbp.id.bytes());
277279
}
278-
RawBson::Decimal128(d) => {
280+
RawBsonRef::Decimal128(d) => {
279281
self.data.extend(d.bytes());
280282
}
281-
RawBson::Double(d) => {
283+
RawBsonRef::Double(d) => {
282284
self.data.extend(d.to_le_bytes());
283285
}
284-
RawBson::Int64(i) => {
286+
RawBsonRef::Int64(i) => {
285287
self.data.extend(i.to_le_bytes());
286288
}
287-
RawBson::RegularExpression(re) => {
288-
append_cstring(self, re.pattern.as_str());
289-
append_cstring(self, re.options.as_str());
289+
RawBsonRef::RegularExpression(re) => {
290+
append_cstring(self, re.pattern);
291+
append_cstring(self, re.options);
290292
}
291-
RawBson::JavaScriptCode(js) => {
292-
append_string(self, js.as_str());
293+
RawBsonRef::JavaScriptCode(js) => {
294+
append_string(self, js);
293295
}
294-
RawBson::JavaScriptCodeWithScope(code_w_scope) => {
295-
let len = RawJavaScriptCodeWithScopeRef {
296-
code: code_w_scope.code.as_str(),
297-
scope: &code_w_scope.scope,
298-
}
299-
.len();
296+
RawBsonRef::JavaScriptCodeWithScope(code_w_scope) => {
297+
let len = code_w_scope.len();
300298
self.data.extend(len.to_le_bytes());
301-
append_string(self, code_w_scope.code.as_str());
302-
self.data.extend(code_w_scope.scope.into_bytes());
299+
append_string(self, code_w_scope.code);
300+
self.data.extend(code_w_scope.scope.as_bytes());
303301
}
304-
RawBson::Timestamp(ts) => {
302+
RawBsonRef::Timestamp(ts) => {
305303
self.data.extend(ts.to_le_i64().to_le_bytes());
306304
}
307-
RawBson::ObjectId(oid) => {
305+
RawBsonRef::ObjectId(oid) => {
308306
self.data.extend(oid.bytes());
309307
}
310-
RawBson::Symbol(s) => {
311-
append_string(self, s.as_str());
308+
RawBsonRef::Symbol(s) => {
309+
append_string(self, s);
312310
}
313-
RawBson::Null | RawBson::Undefined | RawBson::MinKey | RawBson::MaxKey => {}
311+
RawBsonRef::Null | RawBsonRef::Undefined | RawBsonRef::MinKey | RawBsonRef::MaxKey => {}
314312
}
313+
315314
// update element type
316315
self.data[original_len - 1] = element_type as u8;
317316
// append trailing null byte

0 commit comments

Comments
 (0)