Skip to content
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
35 changes: 17 additions & 18 deletions src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,6 @@ impl<T> Cursor<T> {
.and_then(|c| c.post_batch_resume_token())
}

/// Whether this cursor has exhausted all of its getMore calls. The cursor may have more
/// items remaining in the buffer.
pub(crate) fn is_exhausted(&self) -> bool {
self.wrapped_cursor.as_ref().unwrap().is_exhausted()
}

/// Whether this cursor has any additional items to return.
pub(crate) fn has_next(&self) -> bool {
!self.is_exhausted()
|| !self
.wrapped_cursor
.as_ref()
.unwrap()
.state()
.buffer
.is_empty()
}

pub(crate) fn client(&self) -> &Client {
&self.client
}
Expand Down Expand Up @@ -240,6 +222,23 @@ impl<T> Cursor<T> {
self.wrapped_cursor.as_ref().unwrap().current().unwrap()
}

/// Whether this cursor has exhausted all of its getMore calls. The cursor may have more
/// items remaining in the buffer.
pub(crate) fn is_exhausted(&self) -> bool {
self.wrapped_cursor.as_ref().unwrap().is_exhausted()
}

/// Returns true if the cursor has any additional items to return and false otherwise.
pub fn has_next(&self) -> bool {
!self.is_exhausted()
|| !self
.wrapped_cursor
.as_ref()
.unwrap()
.state()
.buffer
.is_empty()
}
/// Deserialize the current result to the generic type associated with this cursor.
///
/// # Panics
Expand Down
5 changes: 5 additions & 0 deletions src/sync/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ impl<T> Cursor<T> {
self.async_cursor.current()
}

/// Returns true if the cursor has any additional items to return and false otherwise.
pub fn has_next(&self) -> bool {
self.async_cursor.has_next()
}

/// Deserialize the current result to the generic type associated with this cursor.
///
/// # Panics
Expand Down
26 changes: 26 additions & 0 deletions src/test/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,29 @@ async fn cursor_final_batch() {
}
assert_eq!(found, 5);
}

#[tokio::test]
async fn cursor_has_next() {
let client = Client::for_test().await;
let coll = client
.create_fresh_collection("test_cursor_has_next", "test", None)
.await;
coll.insert_many(vec![
doc! { "foo": 1 },
doc! { "foo": 2 },
doc! { "foo": 3 },
doc! { "foo": 4 },
doc! { "foo": 5 },
])
.await
.unwrap();

let mut cursor = coll.find(doc! {}).batch_size(2).await.unwrap();
let mut found = 0;
while cursor.has_next() {
if cursor.advance().await.unwrap() {
found += 1;
}
}
assert_eq!(found, 5);
}