@@ -7,13 +7,7 @@ use std::{
7
7
8
8
use serde:: { Deserialize , Serialize } ;
9
9
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 } ;
17
11
18
12
use super :: {
19
13
bson:: RawBson ,
@@ -215,6 +209,18 @@ impl RawDocumentBuf {
215
209
/// # Ok::<(), Error>(())
216
210
/// ```
217
211
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 > > ) {
218
224
fn append_string ( doc : & mut RawDocumentBuf , value : & str ) {
219
225
doc. data
220
226
. extend ( ( ( value. as_bytes ( ) . len ( ) + 1 ) as i32 ) . to_le_bytes ( ) ) ;
@@ -240,78 +246,71 @@ impl RawDocumentBuf {
240
246
let element_type = value. element_type ( ) ;
241
247
242
248
match value {
243
- RawBson :: Int32 ( i) => {
249
+ RawBsonRef :: Int32 ( i) => {
244
250
self . data . extend ( i. to_le_bytes ( ) ) ;
245
251
}
246
- RawBson :: String ( s) => {
247
- append_string ( self , s. as_str ( ) ) ;
252
+ RawBsonRef :: String ( s) => {
253
+ append_string ( self , s) ;
248
254
}
249
- RawBson :: Document ( d) => {
250
- self . data . extend ( d. into_bytes ( ) ) ;
255
+ RawBsonRef :: Document ( d) => {
256
+ self . data . extend ( d. as_bytes ( ) ) ;
251
257
}
252
- RawBson :: Array ( a) => {
253
- self . data . extend ( a. into_vec ( ) ) ;
258
+ RawBsonRef :: Array ( a) => {
259
+ self . data . extend ( a. as_bytes ( ) ) ;
254
260
}
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 ( ) ;
261
263
self . data . extend ( len. to_le_bytes ( ) ) ;
262
264
self . data . push ( b. subtype . into ( ) ) ;
263
265
if let BinarySubtype :: BinaryOld = b. subtype {
264
266
self . data . extend ( ( len - 4 ) . to_le_bytes ( ) )
265
267
}
266
268
self . data . extend ( b. bytes ) ;
267
269
}
268
- RawBson :: Boolean ( b) => {
270
+ RawBsonRef :: Boolean ( b) => {
269
271
self . data . push ( b as u8 ) ;
270
272
}
271
- RawBson :: DateTime ( dt) => {
273
+ RawBsonRef :: DateTime ( dt) => {
272
274
self . data . extend ( dt. timestamp_millis ( ) . to_le_bytes ( ) ) ;
273
275
}
274
- RawBson :: DbPointer ( dbp) => {
275
- append_string ( self , dbp. namespace . as_str ( ) ) ;
276
+ RawBsonRef :: DbPointer ( dbp) => {
277
+ append_string ( self , dbp. namespace ) ;
276
278
self . data . extend ( dbp. id . bytes ( ) ) ;
277
279
}
278
- RawBson :: Decimal128 ( d) => {
280
+ RawBsonRef :: Decimal128 ( d) => {
279
281
self . data . extend ( d. bytes ( ) ) ;
280
282
}
281
- RawBson :: Double ( d) => {
283
+ RawBsonRef :: Double ( d) => {
282
284
self . data . extend ( d. to_le_bytes ( ) ) ;
283
285
}
284
- RawBson :: Int64 ( i) => {
286
+ RawBsonRef :: Int64 ( i) => {
285
287
self . data . extend ( i. to_le_bytes ( ) ) ;
286
288
}
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 ) ;
290
292
}
291
- RawBson :: JavaScriptCode ( js) => {
292
- append_string ( self , js. as_str ( ) ) ;
293
+ RawBsonRef :: JavaScriptCode ( js) => {
294
+ append_string ( self , js) ;
293
295
}
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 ( ) ;
300
298
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 ( ) ) ;
303
301
}
304
- RawBson :: Timestamp ( ts) => {
302
+ RawBsonRef :: Timestamp ( ts) => {
305
303
self . data . extend ( ts. to_le_i64 ( ) . to_le_bytes ( ) ) ;
306
304
}
307
- RawBson :: ObjectId ( oid) => {
305
+ RawBsonRef :: ObjectId ( oid) => {
308
306
self . data . extend ( oid. bytes ( ) ) ;
309
307
}
310
- RawBson :: Symbol ( s) => {
311
- append_string ( self , s. as_str ( ) ) ;
308
+ RawBsonRef :: Symbol ( s) => {
309
+ append_string ( self , s) ;
312
310
}
313
- RawBson :: Null | RawBson :: Undefined | RawBson :: MinKey | RawBson :: MaxKey => { }
311
+ RawBsonRef :: Null | RawBsonRef :: Undefined | RawBsonRef :: MinKey | RawBsonRef :: MaxKey => { }
314
312
}
313
+
315
314
// update element type
316
315
self . data [ original_len - 1 ] = element_type as u8 ;
317
316
// append trailing null byte
0 commit comments