diff --git a/src/document.rs b/src/document.rs index 54b22dcb..fedc38a1 100644 --- a/src/document.rs +++ b/src/document.rs @@ -117,6 +117,11 @@ pub struct Values<'a> { inner: indexmap::map::Values<'a, String, Bson>, } +/// An iterator over a `Document`'s keys and mutable values. +pub struct IterMut<'a> { + inner: indexmap::map::IterMut<'a, String, Bson>, +} + impl<'a> Iterator for Keys<'a> { type Item = &'a String; @@ -181,6 +186,14 @@ impl<'a> Iterator for Iter<'a> { } } +impl<'a> Iterator for IterMut<'a> { + type Item = (&'a String, &'a mut Bson); + + fn next(&mut self) -> Option<(&'a String, &'a mut Bson)> { + self.inner.next() + } +} + impl Document { /// Creates a new empty Document. pub fn new() -> Document { @@ -194,6 +207,13 @@ impl Document { self.into_iter() } + /// Gets an iterator over pairs of keys and mutable values. + pub fn iter_mut(&mut self) -> IterMut { + IterMut { + inner: self.inner.iter_mut(), + } + } + /// Clears the document, removing all values. pub fn clear(&mut self) { self.inner.clear(); diff --git a/src/raw/document.rs b/src/raw/document.rs index fb5f86dc..b5193067 100644 --- a/src/raw/document.rs +++ b/src/raw/document.rs @@ -28,7 +28,7 @@ use crate::{oid::ObjectId, spec::ElementType, Document}; /// A slice of a BSON document (akin to [`std::str`]). This can be created from a /// [`RawDocumentBuf`] or any type that contains valid BSON data, including static binary literals, -/// [Vec](std::vec::Vec), or arrays. +/// [`Vec`](std::vec::Vec), or arrays. /// /// This is an _unsized_ type, meaning that it must always be used behind a pointer like `&`. For an /// owned version of this type, see [`RawDocumentBuf`].