Skip to content

Commit

Permalink
fix: access iterators from RawDocument without needing to convert to …
Browse files Browse the repository at this point in the history
…RawDocumentBuf (#462)
  • Loading branch information
tychoish authored Mar 1, 2024
1 parent 32f3dc3 commit c89e137
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
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<'_> {
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

0 comments on commit c89e137

Please sign in to comment.