Skip to content
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

Implement pop_back for kv #279

Merged
merged 1 commit into from
Jan 1, 2022
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
14 changes: 12 additions & 2 deletions sewup/src/kv/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,18 @@ impl<'a, K: Key + PartialEq, V: Clone + Value> Bucket<K, V> {
}

/// Pop the last item
pub fn pop_back(&self) -> Option<Item<K, V>> {
None
pub fn pop_back(&mut self) -> Option<Item<K, V>> {
let mut prev_pair: Option<(K, V)> = None;
let mut iter = self.iter();
while let Some(pair) = iter.next() {
prev_pair = Some(pair);
}
if let Some((key, value)) = prev_pair {
self.remove(key.clone());
Some((key, value))
} else {
None
}
}

/// Pop the first item
Expand Down
18 changes: 18 additions & 0 deletions sewup/src/kv/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,21 @@ fn test_pop_for_bucket() {
assert_eq!(bucket.get(2).unwrap(), None);
assert_eq!(bucket.get(3).unwrap(), Some(3));
}

#[cfg(feature = "default")]
#[test]
fn test_pop_back_for_bucket() {
let mut bucket = Bucket {
name: "test_bucket".into(),
raw_bucket: (vec![], vec![]),
phantom_k: PhantomData::<usize>,
phantom_v: PhantomData::<usize>,
};
bucket.set(1, 1);
bucket.set(2, 2);
assert_eq!(bucket.get(1).unwrap(), Some(1));
let last = bucket.pop_back();
assert_eq!(last, Some((2, 2)));
assert_eq!(bucket.get(1).unwrap(), Some(1));
assert_eq!(bucket.get(2).unwrap(), None);
}