Skip to content

RUST-1534: implement iter_mut on Document #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/raw/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>](std::vec::Vec), or arrays.
/// [`Vec<u8>`](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`].
Expand Down