Skip to content

Commit fa46ded

Browse files
RUST-2113 Add has_next to cursor API (#1485)
1 parent 4bdd26d commit fa46ded

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

src/cursor.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,24 +142,6 @@ impl<T> Cursor<T> {
142142
.and_then(|c| c.post_batch_resume_token())
143143
}
144144

145-
/// Whether this cursor has exhausted all of its getMore calls. The cursor may have more
146-
/// items remaining in the buffer.
147-
pub(crate) fn is_exhausted(&self) -> bool {
148-
self.wrapped_cursor.as_ref().unwrap().is_exhausted()
149-
}
150-
151-
/// Whether this cursor has any additional items to return.
152-
pub(crate) fn has_next(&self) -> bool {
153-
!self.is_exhausted()
154-
|| !self
155-
.wrapped_cursor
156-
.as_ref()
157-
.unwrap()
158-
.state()
159-
.buffer
160-
.is_empty()
161-
}
162-
163145
pub(crate) fn client(&self) -> &Client {
164146
&self.client
165147
}
@@ -240,6 +222,23 @@ impl<T> Cursor<T> {
240222
self.wrapped_cursor.as_ref().unwrap().current().unwrap()
241223
}
242224

225+
/// Whether this cursor has exhausted all of its getMore calls. The cursor may have more
226+
/// items remaining in the buffer.
227+
pub(crate) fn is_exhausted(&self) -> bool {
228+
self.wrapped_cursor.as_ref().unwrap().is_exhausted()
229+
}
230+
231+
/// Returns true if the cursor has any additional items to return and false otherwise.
232+
pub fn has_next(&self) -> bool {
233+
!self.is_exhausted()
234+
|| !self
235+
.wrapped_cursor
236+
.as_ref()
237+
.unwrap()
238+
.state()
239+
.buffer
240+
.is_empty()
241+
}
243242
/// Deserialize the current result to the generic type associated with this cursor.
244243
///
245244
/// # Panics

src/sync/cursor.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ impl<T> Cursor<T> {
130130
self.async_cursor.current()
131131
}
132132

133+
/// Returns true if the cursor has any additional items to return and false otherwise.
134+
pub fn has_next(&self) -> bool {
135+
self.async_cursor.has_next()
136+
}
137+
133138
/// Deserialize the current result to the generic type associated with this cursor.
134139
///
135140
/// # Panics

src/test/cursor.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,29 @@ async fn cursor_final_batch() {
263263
}
264264
assert_eq!(found, 5);
265265
}
266+
267+
#[tokio::test]
268+
async fn cursor_has_next() {
269+
let client = Client::for_test().await;
270+
let coll = client
271+
.create_fresh_collection("test_cursor_has_next", "test", None)
272+
.await;
273+
coll.insert_many(vec![
274+
doc! { "foo": 1 },
275+
doc! { "foo": 2 },
276+
doc! { "foo": 3 },
277+
doc! { "foo": 4 },
278+
doc! { "foo": 5 },
279+
])
280+
.await
281+
.unwrap();
282+
283+
let mut cursor = coll.find(doc! {}).batch_size(2).await.unwrap();
284+
let mut found = 0;
285+
while cursor.has_next() {
286+
if cursor.advance().await.unwrap() {
287+
found += 1;
288+
}
289+
}
290+
assert_eq!(found, 5);
291+
}

0 commit comments

Comments
 (0)