Skip to content

Commit 87b3358

Browse files
committed
RUST-788 Accept impl AsRef<str> in Document methods (C-GENERIC)
1 parent 3e7c06d commit 87b3358

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

src/document.rs

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,18 @@ impl Document {
201201
}
202202

203203
/// 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())
206206
}
207207

208208
/// 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())
211211
}
212212

213213
/// Get a floating point value for this key if it exists and has
214214
/// 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> {
216216
match self.get(key) {
217217
Some(&Bson::Double(v)) => Ok(v),
218218
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -222,7 +222,7 @@ impl Document {
222222

223223
/// Get a mutable reference to a floating point value for this key if it exists and has
224224
/// 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> {
226226
match self.get_mut(key) {
227227
Some(&mut Bson::Double(ref mut v)) => Ok(v),
228228
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -232,7 +232,7 @@ impl Document {
232232

233233
/// Get a reference to a Decimal128 value for key, if it exists.
234234
#[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> {
236236
match self.get(key) {
237237
Some(&Bson::Decimal128(ref v)) => Ok(v),
238238
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -242,7 +242,10 @@ impl Document {
242242

243243
/// Get a mutable reference to a Decimal128 value for key, if it exists.
244244
#[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> {
246249
match self.get_mut(key) {
247250
Some(&mut Bson::Decimal128(ref mut v)) => Ok(v),
248251
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -251,7 +254,7 @@ impl Document {
251254
}
252255

253256
/// 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> {
255258
match self.get(key) {
256259
Some(&Bson::String(ref v)) => Ok(v),
257260
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -260,7 +263,7 @@ impl Document {
260263
}
261264

262265
/// 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> {
264267
match self.get_mut(key) {
265268
Some(&mut Bson::String(ref mut v)) => Ok(v),
266269
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -270,7 +273,7 @@ impl Document {
270273

271274
/// Get a reference to an array for this key if it exists and has
272275
/// 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> {
274277
match self.get(key) {
275278
Some(&Bson::Array(ref v)) => Ok(v),
276279
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -280,7 +283,7 @@ impl Document {
280283

281284
/// Get a mutable reference to an array for this key if it exists and has
282285
/// 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> {
284287
match self.get_mut(key) {
285288
Some(&mut Bson::Array(ref mut v)) => Ok(v),
286289
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -290,7 +293,7 @@ impl Document {
290293

291294
/// Get a reference to a document for this key if it exists and has
292295
/// 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> {
294297
match self.get(key) {
295298
Some(&Bson::Document(ref v)) => Ok(v),
296299
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -300,7 +303,7 @@ impl Document {
300303

301304
/// Get a mutable reference to a document for this key if it exists and has
302305
/// 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> {
304307
match self.get_mut(key) {
305308
Some(&mut Bson::Document(ref mut v)) => Ok(v),
306309
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -309,7 +312,7 @@ impl Document {
309312
}
310313

311314
/// 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> {
313316
match self.get(key) {
314317
Some(&Bson::Boolean(v)) => Ok(v),
315318
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -318,7 +321,7 @@ impl Document {
318321
}
319322

320323
/// 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> {
322325
match self.get_mut(key) {
323326
Some(&mut Bson::Boolean(ref mut v)) => Ok(v),
324327
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -327,12 +330,12 @@ impl Document {
327330
}
328331

329332
/// 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 {
331334
self.get(key) == Some(&Bson::Null)
332335
}
333336

334337
/// 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> {
336339
match self.get(key) {
337340
Some(&Bson::Int32(v)) => Ok(v),
338341
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -341,7 +344,7 @@ impl Document {
341344
}
342345

343346
/// 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> {
345348
match self.get_mut(key) {
346349
Some(&mut Bson::Int32(ref mut v)) => Ok(v),
347350
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -350,7 +353,7 @@ impl Document {
350353
}
351354

352355
/// 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> {
354357
match self.get(key) {
355358
Some(&Bson::Int64(v)) => Ok(v),
356359
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -359,7 +362,7 @@ impl Document {
359362
}
360363

361364
/// 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> {
363366
match self.get_mut(key) {
364367
Some(&mut Bson::Int64(ref mut v)) => Ok(v),
365368
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -368,7 +371,7 @@ impl Document {
368371
}
369372

370373
/// 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> {
372375
match self.get(key) {
373376
Some(&Bson::Timestamp(timestamp)) => Ok(timestamp),
374377
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -378,7 +381,7 @@ impl Document {
378381

379382
/// Get a mutable reference to a time stamp value for this key if it exists and has the correct
380383
/// 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> {
382385
match self.get_mut(key) {
383386
Some(&mut Bson::Timestamp(ref mut timestamp)) => Ok(timestamp),
384387
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -388,7 +391,7 @@ impl Document {
388391

389392
/// Get a reference to a generic binary value for this key if it exists and has the correct
390393
/// 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>> {
392395
match self.get(key) {
393396
Some(&Bson::Binary(Binary {
394397
subtype: BinarySubtype::Generic,
@@ -401,7 +404,10 @@ impl Document {
401404

402405
/// Get a mutable reference generic binary value for this key if it exists and has the correct
403406
/// 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>> {
405411
match self.get_mut(key) {
406412
Some(&mut Bson::Binary(Binary {
407413
subtype: BinarySubtype::Generic,
@@ -413,7 +419,7 @@ impl Document {
413419
}
414420

415421
/// 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> {
417423
match self.get(key) {
418424
Some(&Bson::ObjectId(v)) => Ok(v),
419425
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -423,7 +429,7 @@ impl Document {
423429

424430
/// Get a mutable reference to an object id value for this key if it exists and has the correct
425431
/// 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> {
427433
match self.get_mut(key) {
428434
Some(&mut Bson::ObjectId(ref mut v)) => Ok(v),
429435
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -432,7 +438,7 @@ impl Document {
432438
}
433439

434440
/// 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>> {
436442
match self.get(key) {
437443
Some(&Bson::DateTime(ref v)) => Ok(v),
438444
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -442,7 +448,10 @@ impl Document {
442448

443449
/// Get a mutable reference to a UTC datetime value for this key if it exists and has the
444450
/// 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>> {
446455
match self.get_mut(key) {
447456
Some(&mut Bson::DateTime(ref mut v)) => Ok(v),
448457
Some(_) => Err(ValueAccessError::UnexpectedType),
@@ -451,8 +460,8 @@ impl Document {
451460
}
452461

453462
/// 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())
456465
}
457466

458467
/// Gets a collection of all keys in the document.
@@ -488,8 +497,8 @@ impl Document {
488497

489498
/// Takes the value of the entry out of the document, and returns it.
490499
/// 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())
493502
}
494503

495504
pub fn entry(&mut self, k: String) -> Entry {

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@
183183
//! that is also less error prone.
184184
185185
#![allow(clippy::cognitive_complexity)]
186-
187186
#![doc(html_root_url = "https://docs.rs/bson/1.2.2")]
188187

189188
pub use self::{

0 commit comments

Comments
 (0)