diff --git a/crossbeam-skiplist/tests/base.rs b/crossbeam-skiplist/tests/base.rs index 38f3122df..e7b93c83b 100644 --- a/crossbeam-skiplist/tests/base.rs +++ b/crossbeam-skiplist/tests/base.rs @@ -910,3 +910,49 @@ fn drops() { assert_eq!(KEYS.load(Ordering::SeqCst), 8); assert_eq!(VALUES.load(Ordering::SeqCst), 7); } + +#[test] +fn comparable_get() { + use equivalent::{Comparable, Equivalent}; + + #[derive(PartialEq, Eq, PartialOrd, Ord)] + struct Foo { + a: u64, + b: u32, + } + + #[derive(PartialEq, Eq, PartialOrd, Ord)] + struct FooRef<'a> { + data: &'a [u8] + } + + impl Equivalent for FooRef<'_> { + fn equivalent(&self, key: &Foo) -> bool { + let a = u64::from_be_bytes(self.data[..8].try_into().unwrap()); + let b = u32::from_be_bytes(self.data[8..].try_into().unwrap()); + a == key.a && b == key.b + } + } + + impl Comparable for FooRef<'_> { + fn compare(&self, key: &Foo) -> std::cmp::Ordering { + let a = u64::from_be_bytes(self.data[..8].try_into().unwrap()); + let b = u32::from_be_bytes(self.data[8..].try_into().unwrap()); + Foo { a, b }.cmp(key) + } + } + + let s = SkipList::new(epoch::default_collector().clone()); + let foo = Foo { a: 1, b: 2 }; + + let g = &epoch::pin(); + s.insert(foo, 12, g); + + let buf = 1u64.to_be_bytes().iter().chain(2u32.to_be_bytes().iter()).copied().collect::>(); + let foo_ref = FooRef { data: &buf }; + + let ent = s.get(&foo_ref, g).unwrap(); + assert_eq!(ent.key().a, 1); + assert_eq!(ent.key().b, 2); + assert_eq!(*ent.value(), 12); +} \ No newline at end of file