Skip to content
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

fix: access iterators from RawDocument without needing to convert to RawDocumentBuf #462

Merged
merged 2 commits into from
Mar 1, 2024
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
22 changes: 21 additions & 1 deletion src/raw/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,26 @@ impl RawDocument {
Ok(None)
}

/// Gets an iterator over the elements in the [`RawDocument`] that yields
/// `Result<(&str, RawBson<'_>)>`.
pub fn iter(&self) -> Iter<'_> {
Iter::new(self)
}

/// Gets an iterator over the elements in the [`RawDocument`],
/// which yields `Result<RawElement<'_>>` values. These hold a
/// reference to the underlying document but do not explicitly
/// resolve the values.
///
/// This iterator, which underpins the implementation of the
/// default iterator, produces `RawElement` objects that hold a
/// view onto the document but do not parse out or construct
/// values until the `.value()` or `.try_into()` methods are
/// called.
pub fn iter_elements(&self) -> RawIter<'_> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a doc comment for this method as well? It can probably copy most of the content of the RawDocumentBuf::iter_elements comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure thing!

RawIter::new(self)
}

fn get_with<'a, T>(
&'a self,
key: impl AsRef<str>,
Expand Down Expand Up @@ -599,6 +619,6 @@ impl<'a> IntoIterator for &'a RawDocument {
type Item = Result<(&'a str, RawBsonRef<'a>)>;

fn into_iter(self) -> Iter<'a> {
Iter::new(self)
self.iter()
}
}
10 changes: 5 additions & 5 deletions src/raw/document_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ impl RawDocumentBuf {
/// resolve the values.
///
/// This iterator, which underpins the implementation of the
/// default iterator, produces `RawElement` objects, which
/// hold a view onto the document but do not parse out or
/// construct values until the `.value()` or `.try_into()` methods
/// are called.
/// default iterator, produces `RawElement` objects that hold a
/// view onto the document but do not parse out or construct
/// values until the `.value()` or `.try_into()` methods are
/// called.
///
/// # Note:
///
Expand Down Expand Up @@ -394,7 +394,7 @@ impl<'a> IntoIterator for &'a RawDocumentBuf {
type Item = Result<(&'a str, RawBsonRef<'a>)>;

fn into_iter(self) -> Iter<'a> {
Iter::new(self)
self.iter()
}
}

Expand Down