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

impl Deref for set::Entry #657

Merged
merged 1 commit into from Feb 13, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions crossbeam-skiplist/src/set.rs
Expand Up @@ -3,6 +3,7 @@
use std::borrow::Borrow;
use std::fmt;
use std::iter::FromIterator;
use std::ops::Deref;
use std::ops::{Bound, RangeBounds};

use crate::map;
Expand Down Expand Up @@ -281,6 +282,14 @@ where
}
}

impl<T> Deref for Entry<'_, T> {
type Target = T;

fn deref(&self) -> &Self::Target {
self.value()
}
}

/// An owning iterator over the entries of a `SkipSet`.
pub struct IntoIter<T> {
inner: map::IntoIter<T, ()>,
Expand Down
24 changes: 24 additions & 0 deletions crossbeam-skiplist/tests/set.rs
Expand Up @@ -7,3 +7,27 @@ fn smoke() {
m.insert(5);
m.insert(7);
}

#[test]
fn iter() {
let s = SkipSet::new();
for &x in &[4, 2, 12, 8, 7, 11, 5] {
s.insert(x);
}

assert_eq!(
s.iter().map(|e| *e).collect::<Vec<_>>(),
&[2, 4, 5, 7, 8, 11, 12]
);

let mut it = s.iter();
s.remove(&2);
assert_eq!(*it.next().unwrap(), 4);
s.remove(&7);
assert_eq!(*it.next().unwrap(), 5);
s.remove(&5);
assert_eq!(*it.next().unwrap(), 8);
s.remove(&12);
assert_eq!(*it.next().unwrap(), 11);
assert!(it.next().is_none());
}