Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 9 additions & 7 deletions crates/trie/trie/src/forward_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ impl<'a, K, V> ForwardInMemoryCursor<'a, K, V> {
self.is_empty
}

/// Returns the current entry pointed to be the cursor, or `None` if no entries are left.
#[inline]
fn peek(&self) -> Option<&(K, V)> {
pub fn current(&self) -> Option<&(K, V)> {
self.entries.clone().next()
}

Expand Down Expand Up @@ -59,7 +60,7 @@ where
fn advance_while(&mut self, predicate: impl Fn(&K) -> bool) -> Option<(K, V)> {
let mut entry;
loop {
entry = self.peek();
entry = self.current();
if entry.is_some_and(|(k, _)| predicate(k)) {
self.next();
} else {
Expand All @@ -77,20 +78,21 @@ mod tests {
#[test]
fn test_cursor() {
let mut cursor = ForwardInMemoryCursor::new(&[(1, ()), (2, ()), (3, ()), (4, ()), (5, ())]);
assert_eq!(cursor.current(), Some(&(1, ())));

assert_eq!(cursor.seek(&0), Some((1, ())));
assert_eq!(cursor.peek(), Some(&(1, ())));
assert_eq!(cursor.current(), Some(&(1, ())));

assert_eq!(cursor.seek(&3), Some((3, ())));
assert_eq!(cursor.peek(), Some(&(3, ())));
assert_eq!(cursor.current(), Some(&(3, ())));

assert_eq!(cursor.seek(&3), Some((3, ())));
assert_eq!(cursor.peek(), Some(&(3, ())));
assert_eq!(cursor.current(), Some(&(3, ())));

assert_eq!(cursor.seek(&4), Some((4, ())));
assert_eq!(cursor.peek(), Some(&(4, ())));
assert_eq!(cursor.current(), Some(&(4, ())));

assert_eq!(cursor.seek(&6), None);
assert_eq!(cursor.peek(), None);
assert_eq!(cursor.current(), None);
}
}
Loading
Loading