@@ -201,18 +201,18 @@ impl Document {
201
201
}
202
202
203
203
/// Returns a reference to the Bson corresponding to the key.
204
- pub fn get ( & self , key : & str ) -> Option < & Bson > {
205
- self . inner . get ( key)
204
+ pub fn get ( & self , key : impl AsRef < str > ) -> Option < & Bson > {
205
+ self . inner . get ( key. as_ref ( ) )
206
206
}
207
207
208
208
/// Gets a mutable reference to the Bson corresponding to the key
209
- pub fn get_mut ( & mut self , key : & str ) -> Option < & mut Bson > {
210
- self . inner . get_mut ( key)
209
+ pub fn get_mut ( & mut self , key : impl AsRef < str > ) -> Option < & mut Bson > {
210
+ self . inner . get_mut ( key. as_ref ( ) )
211
211
}
212
212
213
213
/// Get a floating point value for this key if it exists and has
214
214
/// the correct type.
215
- pub fn get_f64 ( & self , key : & str ) -> ValueAccessResult < f64 > {
215
+ pub fn get_f64 ( & self , key : impl AsRef < str > ) -> ValueAccessResult < f64 > {
216
216
match self . get ( key) {
217
217
Some ( & Bson :: Double ( v) ) => Ok ( v) ,
218
218
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -222,7 +222,7 @@ impl Document {
222
222
223
223
/// Get a mutable reference to a floating point value for this key if it exists and has
224
224
/// the correct type.
225
- pub fn get_f64_mut ( & mut self , key : & str ) -> ValueAccessResult < & mut f64 > {
225
+ pub fn get_f64_mut ( & mut self , key : impl AsRef < str > ) -> ValueAccessResult < & mut f64 > {
226
226
match self . get_mut ( key) {
227
227
Some ( & mut Bson :: Double ( ref mut v) ) => Ok ( v) ,
228
228
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -232,7 +232,7 @@ impl Document {
232
232
233
233
/// Get a reference to a Decimal128 value for key, if it exists.
234
234
#[ cfg( feature = "decimal128" ) ]
235
- pub fn get_decimal128 ( & self , key : & str ) -> ValueAccessResult < & Decimal128 > {
235
+ pub fn get_decimal128 ( & self , key : impl AsRef < str > ) -> ValueAccessResult < & Decimal128 > {
236
236
match self . get ( key) {
237
237
Some ( & Bson :: Decimal128 ( ref v) ) => Ok ( v) ,
238
238
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -242,7 +242,10 @@ impl Document {
242
242
243
243
/// Get a mutable reference to a Decimal128 value for key, if it exists.
244
244
#[ cfg( feature = "decimal128" ) ]
245
- pub fn get_decimal128_mut ( & mut self , key : & str ) -> ValueAccessResult < & mut Decimal128 > {
245
+ pub fn get_decimal128_mut (
246
+ & mut self ,
247
+ key : impl AsRef < str > ,
248
+ ) -> ValueAccessResult < & mut Decimal128 > {
246
249
match self . get_mut ( key) {
247
250
Some ( & mut Bson :: Decimal128 ( ref mut v) ) => Ok ( v) ,
248
251
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -251,7 +254,7 @@ impl Document {
251
254
}
252
255
253
256
/// Get a string slice this key if it exists and has the correct type.
254
- pub fn get_str ( & self , key : & str ) -> ValueAccessResult < & str > {
257
+ pub fn get_str ( & self , key : impl AsRef < str > ) -> ValueAccessResult < & str > {
255
258
match self . get ( key) {
256
259
Some ( & Bson :: String ( ref v) ) => Ok ( v) ,
257
260
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -260,7 +263,7 @@ impl Document {
260
263
}
261
264
262
265
/// Get a mutable string slice this key if it exists and has the correct type.
263
- pub fn get_str_mut ( & mut self , key : & str ) -> ValueAccessResult < & mut str > {
266
+ pub fn get_str_mut ( & mut self , key : impl AsRef < str > ) -> ValueAccessResult < & mut str > {
264
267
match self . get_mut ( key) {
265
268
Some ( & mut Bson :: String ( ref mut v) ) => Ok ( v) ,
266
269
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -270,7 +273,7 @@ impl Document {
270
273
271
274
/// Get a reference to an array for this key if it exists and has
272
275
/// the correct type.
273
- pub fn get_array ( & self , key : & str ) -> ValueAccessResult < & Array > {
276
+ pub fn get_array ( & self , key : impl AsRef < str > ) -> ValueAccessResult < & Array > {
274
277
match self . get ( key) {
275
278
Some ( & Bson :: Array ( ref v) ) => Ok ( v) ,
276
279
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -280,7 +283,7 @@ impl Document {
280
283
281
284
/// Get a mutable reference to an array for this key if it exists and has
282
285
/// the correct type.
283
- pub fn get_array_mut ( & mut self , key : & str ) -> ValueAccessResult < & mut Array > {
286
+ pub fn get_array_mut ( & mut self , key : impl AsRef < str > ) -> ValueAccessResult < & mut Array > {
284
287
match self . get_mut ( key) {
285
288
Some ( & mut Bson :: Array ( ref mut v) ) => Ok ( v) ,
286
289
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -290,7 +293,7 @@ impl Document {
290
293
291
294
/// Get a reference to a document for this key if it exists and has
292
295
/// the correct type.
293
- pub fn get_document ( & self , key : & str ) -> ValueAccessResult < & Document > {
296
+ pub fn get_document ( & self , key : impl AsRef < str > ) -> ValueAccessResult < & Document > {
294
297
match self . get ( key) {
295
298
Some ( & Bson :: Document ( ref v) ) => Ok ( v) ,
296
299
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -300,7 +303,7 @@ impl Document {
300
303
301
304
/// Get a mutable reference to a document for this key if it exists and has
302
305
/// the correct type.
303
- pub fn get_document_mut ( & mut self , key : & str ) -> ValueAccessResult < & mut Document > {
306
+ pub fn get_document_mut ( & mut self , key : impl AsRef < str > ) -> ValueAccessResult < & mut Document > {
304
307
match self . get_mut ( key) {
305
308
Some ( & mut Bson :: Document ( ref mut v) ) => Ok ( v) ,
306
309
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -309,7 +312,7 @@ impl Document {
309
312
}
310
313
311
314
/// Get a bool value for this key if it exists and has the correct type.
312
- pub fn get_bool ( & self , key : & str ) -> ValueAccessResult < bool > {
315
+ pub fn get_bool ( & self , key : impl AsRef < str > ) -> ValueAccessResult < bool > {
313
316
match self . get ( key) {
314
317
Some ( & Bson :: Boolean ( v) ) => Ok ( v) ,
315
318
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -318,7 +321,7 @@ impl Document {
318
321
}
319
322
320
323
/// Get a mutable reference to a bool value for this key if it exists and has the correct type.
321
- pub fn get_bool_mut ( & mut self , key : & str ) -> ValueAccessResult < & mut bool > {
324
+ pub fn get_bool_mut ( & mut self , key : impl AsRef < str > ) -> ValueAccessResult < & mut bool > {
322
325
match self . get_mut ( key) {
323
326
Some ( & mut Bson :: Boolean ( ref mut v) ) => Ok ( v) ,
324
327
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -327,12 +330,12 @@ impl Document {
327
330
}
328
331
329
332
/// Returns wether this key has a null value
330
- pub fn is_null ( & self , key : & str ) -> bool {
333
+ pub fn is_null ( & self , key : impl AsRef < str > ) -> bool {
331
334
self . get ( key) == Some ( & Bson :: Null )
332
335
}
333
336
334
337
/// Get an i32 value for this key if it exists and has the correct type.
335
- pub fn get_i32 ( & self , key : & str ) -> ValueAccessResult < i32 > {
338
+ pub fn get_i32 ( & self , key : impl AsRef < str > ) -> ValueAccessResult < i32 > {
336
339
match self . get ( key) {
337
340
Some ( & Bson :: Int32 ( v) ) => Ok ( v) ,
338
341
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -341,7 +344,7 @@ impl Document {
341
344
}
342
345
343
346
/// Get a mutable reference to an i32 value for this key if it exists and has the correct type.
344
- pub fn get_i32_mut ( & mut self , key : & str ) -> ValueAccessResult < & mut i32 > {
347
+ pub fn get_i32_mut ( & mut self , key : impl AsRef < str > ) -> ValueAccessResult < & mut i32 > {
345
348
match self . get_mut ( key) {
346
349
Some ( & mut Bson :: Int32 ( ref mut v) ) => Ok ( v) ,
347
350
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -350,7 +353,7 @@ impl Document {
350
353
}
351
354
352
355
/// Get an i64 value for this key if it exists and has the correct type.
353
- pub fn get_i64 ( & self , key : & str ) -> ValueAccessResult < i64 > {
356
+ pub fn get_i64 ( & self , key : impl AsRef < str > ) -> ValueAccessResult < i64 > {
354
357
match self . get ( key) {
355
358
Some ( & Bson :: Int64 ( v) ) => Ok ( v) ,
356
359
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -359,7 +362,7 @@ impl Document {
359
362
}
360
363
361
364
/// Get a mutable reference to an i64 value for this key if it exists and has the correct type.
362
- pub fn get_i64_mut ( & mut self , key : & str ) -> ValueAccessResult < & mut i64 > {
365
+ pub fn get_i64_mut ( & mut self , key : impl AsRef < str > ) -> ValueAccessResult < & mut i64 > {
363
366
match self . get_mut ( key) {
364
367
Some ( & mut Bson :: Int64 ( ref mut v) ) => Ok ( v) ,
365
368
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -368,7 +371,7 @@ impl Document {
368
371
}
369
372
370
373
/// Get a time stamp value for this key if it exists and has the correct type.
371
- pub fn get_timestamp ( & self , key : & str ) -> ValueAccessResult < Timestamp > {
374
+ pub fn get_timestamp ( & self , key : impl AsRef < str > ) -> ValueAccessResult < Timestamp > {
372
375
match self . get ( key) {
373
376
Some ( & Bson :: Timestamp ( timestamp) ) => Ok ( timestamp) ,
374
377
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -378,7 +381,7 @@ impl Document {
378
381
379
382
/// Get a mutable reference to a time stamp value for this key if it exists and has the correct
380
383
/// type.
381
- pub fn get_timestamp_mut ( & mut self , key : & str ) -> ValueAccessResult < & mut Timestamp > {
384
+ pub fn get_timestamp_mut ( & mut self , key : impl AsRef < str > ) -> ValueAccessResult < & mut Timestamp > {
382
385
match self . get_mut ( key) {
383
386
Some ( & mut Bson :: Timestamp ( ref mut timestamp) ) => Ok ( timestamp) ,
384
387
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -388,7 +391,7 @@ impl Document {
388
391
389
392
/// Get a reference to a generic binary value for this key if it exists and has the correct
390
393
/// type.
391
- pub fn get_binary_generic ( & self , key : & str ) -> ValueAccessResult < & Vec < u8 > > {
394
+ pub fn get_binary_generic ( & self , key : impl AsRef < str > ) -> ValueAccessResult < & Vec < u8 > > {
392
395
match self . get ( key) {
393
396
Some ( & Bson :: Binary ( Binary {
394
397
subtype : BinarySubtype :: Generic ,
@@ -401,7 +404,10 @@ impl Document {
401
404
402
405
/// Get a mutable reference generic binary value for this key if it exists and has the correct
403
406
/// type.
404
- pub fn get_binary_generic_mut ( & mut self , key : & str ) -> ValueAccessResult < & mut Vec < u8 > > {
407
+ pub fn get_binary_generic_mut (
408
+ & mut self ,
409
+ key : impl AsRef < str > ,
410
+ ) -> ValueAccessResult < & mut Vec < u8 > > {
405
411
match self . get_mut ( key) {
406
412
Some ( & mut Bson :: Binary ( Binary {
407
413
subtype : BinarySubtype :: Generic ,
@@ -413,7 +419,7 @@ impl Document {
413
419
}
414
420
415
421
/// Get an object id value for this key if it exists and has the correct type.
416
- pub fn get_object_id ( & self , key : & str ) -> ValueAccessResult < ObjectId > {
422
+ pub fn get_object_id ( & self , key : impl AsRef < str > ) -> ValueAccessResult < ObjectId > {
417
423
match self . get ( key) {
418
424
Some ( & Bson :: ObjectId ( v) ) => Ok ( v) ,
419
425
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -423,7 +429,7 @@ impl Document {
423
429
424
430
/// Get a mutable reference to an object id value for this key if it exists and has the correct
425
431
/// type.
426
- pub fn get_object_id_mut ( & mut self , key : & str ) -> ValueAccessResult < & mut ObjectId > {
432
+ pub fn get_object_id_mut ( & mut self , key : impl AsRef < str > ) -> ValueAccessResult < & mut ObjectId > {
427
433
match self . get_mut ( key) {
428
434
Some ( & mut Bson :: ObjectId ( ref mut v) ) => Ok ( v) ,
429
435
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -432,7 +438,7 @@ impl Document {
432
438
}
433
439
434
440
/// Get a reference to a UTC datetime value for this key if it exists and has the correct type.
435
- pub fn get_datetime ( & self , key : & str ) -> ValueAccessResult < & DateTime < Utc > > {
441
+ pub fn get_datetime ( & self , key : impl AsRef < str > ) -> ValueAccessResult < & DateTime < Utc > > {
436
442
match self . get ( key) {
437
443
Some ( & Bson :: DateTime ( ref v) ) => Ok ( v) ,
438
444
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -442,7 +448,10 @@ impl Document {
442
448
443
449
/// Get a mutable reference to a UTC datetime value for this key if it exists and has the
444
450
/// correct type.
445
- pub fn get_datetime_mut ( & mut self , key : & str ) -> ValueAccessResult < & mut DateTime < Utc > > {
451
+ pub fn get_datetime_mut (
452
+ & mut self ,
453
+ key : impl AsRef < str > ,
454
+ ) -> ValueAccessResult < & mut DateTime < Utc > > {
446
455
match self . get_mut ( key) {
447
456
Some ( & mut Bson :: DateTime ( ref mut v) ) => Ok ( v) ,
448
457
Some ( _) => Err ( ValueAccessError :: UnexpectedType ) ,
@@ -451,8 +460,8 @@ impl Document {
451
460
}
452
461
453
462
/// Returns true if the map contains a value for the specified key.
454
- pub fn contains_key ( & self , key : & str ) -> bool {
455
- self . inner . contains_key ( key)
463
+ pub fn contains_key ( & self , key : impl AsRef < str > ) -> bool {
464
+ self . inner . contains_key ( key. as_ref ( ) )
456
465
}
457
466
458
467
/// Gets a collection of all keys in the document.
@@ -488,8 +497,8 @@ impl Document {
488
497
489
498
/// Takes the value of the entry out of the document, and returns it.
490
499
/// Computes in **O(n)** time (average).
491
- pub fn remove ( & mut self , key : & str ) -> Option < Bson > {
492
- self . inner . shift_remove ( key)
500
+ pub fn remove ( & mut self , key : impl AsRef < str > ) -> Option < Bson > {
501
+ self . inner . shift_remove ( key. as_ref ( ) )
493
502
}
494
503
495
504
pub fn entry ( & mut self , k : String ) -> Entry {
0 commit comments