-
Notifications
You must be signed in to change notification settings - Fork 135
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
provide lazy iterator implementation #449
Changes from 11 commits
06bcd4f
7f5ca0c
89c41c8
9d5c717
0abd2d0
f79c4c0
e5ff849
66555e5
4d5bf47
ba58151
364131f
1056729
e39ee2e
ee56e06
5f39853
b822da5
093de42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ use crate::{ | |
use super::{ | ||
error::{ValueAccessError, ValueAccessErrorKind, ValueAccessResult}, | ||
i32_from_slice, | ||
try_to_str, | ||
Error, | ||
Iter, | ||
RawArray, | ||
|
@@ -170,10 +171,10 @@ impl RawDocument { | |
/// # Ok::<(), Error>(()) | ||
/// ``` | ||
pub fn get(&self, key: impl AsRef<str>) -> Result<Option<RawBsonRef<'_>>> { | ||
for result in self.into_iter() { | ||
let (k, v) = result?; | ||
if key.as_ref() == k { | ||
return Ok(Some(v)); | ||
for elem in Iter::new(self) { | ||
let elem = elem?; | ||
if key.as_ref() == elem.key() { | ||
return Ok(Some(elem.try_into()?)); | ||
} | ||
} | ||
Ok(None) | ||
|
@@ -492,6 +493,22 @@ impl RawDocument { | |
pub fn is_empty(&self) -> bool { | ||
self.as_bytes().len() == MIN_BSON_DOCUMENT_SIZE as usize | ||
} | ||
|
||
pub(crate) fn read_cstring_at(&self, start_at: usize) -> Result<&str> { | ||
let buf = &self.as_bytes()[start_at..]; | ||
|
||
let mut splits = buf.splitn(2, |x| *x == 0); | ||
let value = splits | ||
.next() | ||
.ok_or_else(|| Error::new_without_key(ErrorKind::new_malformed("no value")))?; | ||
if splits.next().is_some() { | ||
Ok(try_to_str(value)?) | ||
} else { | ||
Err(Error::new_without_key(ErrorKind::new_malformed( | ||
"expected null terminator", | ||
))) | ||
} | ||
} | ||
} | ||
|
||
impl<'de: 'a, 'a> Deserialize<'de> for &'a RawDocument { | ||
|
@@ -577,10 +594,10 @@ impl TryFrom<&RawDocument> for crate::Document { | |
} | ||
|
||
impl<'a> IntoIterator for &'a RawDocument { | ||
type IntoIter = Iter<'a>; | ||
type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, this is a breaking change. I think this can be solved by what you suggested in the conversation - keep the existing functionality named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alas, easy enough done |
||
type Item = Result<(&'a str, RawBsonRef<'a>)>; | ||
|
||
fn into_iter(self) -> Iter<'a> { | ||
Iter::new(self) | ||
fn into_iter(self) -> Box<dyn Iterator<Item = Result<(&'a str, RawBsonRef<'a>)>> + 'a> { | ||
Box::new(Iter::new(self).into_eager()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this just be
Some(Ok(elem)) => Some(elem.value())
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup!