diff --git a/examples/zero_cost.rs b/examples/zero_cost.rs index f73d55e5..cdefc051 100644 --- a/examples/zero_cost.rs +++ b/examples/zero_cost.rs @@ -154,12 +154,23 @@ fn main() { let people = people.clone(); spawn(move || loop { let (person, hello) = &people[i]; - if let Some(p) = reader.get(&people[i].0) { + let person_ref = PersonRef { + id: person.id, + name: &person.name, + }; + if let Some(p) = reader.get(person) { assert_eq!(p.key().id, person.id); assert_eq!(p.key().name, person.name); assert_eq!(p.value(), hello); break; } + + if let Some(p) = reader.get(&person_ref) { + assert_eq!(p.key().id, person.id); + assert_eq!(p.key().name, person.name); + assert_eq!(p.value(), hello); + break; + }; }) }); diff --git a/src/swmr/generic.rs b/src/swmr/generic.rs index 3fd720f2..abe9ea20 100644 --- a/src/swmr/generic.rs +++ b/src/swmr/generic.rs @@ -167,11 +167,11 @@ where } } -impl Equivalent> for Query<'_, K, Q> +impl<'a, 'b: 'a, K, Q, V> Equivalent> for Query<'a, K, Q> where K: Type + Ord + ?Sized, V: ?Sized, - Q: ?Sized + Ord + for<'b> Equivalent>, + Q: ?Sized + Ord + Equivalent>, { #[inline] fn equivalent(&self, p: &GenericPointer) -> bool { @@ -180,11 +180,11 @@ where } } -impl Comparable> for Query<'_, K, Q> +impl<'a, 'b: 'a, K, Q, V> Comparable> for Query<'a, K, Q> where K: Type + Ord + ?Sized, V: ?Sized, - Q: ?Sized + Ord + for<'b> Comparable>, + Q: ?Sized + Ord + Comparable>, { #[inline] fn compare(&self, p: &GenericPointer) -> cmp::Ordering { @@ -326,9 +326,9 @@ where } #[inline] - fn get(&self, key: &Q) -> Option> + fn get<'a, 'b: 'a, Q>(&'a self, key: &'b Q) -> Option> where - Q: ?Sized + Ord + for<'b> Comparable>, + Q: ?Sized + Ord + Comparable>, { self .map @@ -511,9 +511,9 @@ where /// Gets the value associated with the key. #[inline] - pub fn get(&self, key: &Q) -> Option> + pub fn get<'a, 'b: 'a, Q>(&'a self, key: &'b Q) -> Option> where - Q: ?Sized + Ord + for<'b> Comparable>, + Q: ?Sized + Ord + Comparable>, { self.core.get(key) } diff --git a/src/swmr/generic/reader.rs b/src/swmr/generic/reader.rs index c39be705..5da0ac0e 100644 --- a/src/swmr/generic/reader.rs +++ b/src/swmr/generic/reader.rs @@ -125,9 +125,9 @@ where /// Gets the value associated with the key. #[inline] - pub fn get(&self, key: &Q) -> Option> + pub fn get<'a, 'b: 'a, Q>(&'a self, key: &'b Q) -> Option> where - Q: ?Sized + Ord + for<'b> Comparable>, + Q: ?Sized + Ord + Comparable>, { self.0.get(key) } diff --git a/src/swmr/generic/tests.rs b/src/swmr/generic/tests.rs index 11d08f39..76e812f9 100644 --- a/src/swmr/generic/tests.rs +++ b/src/swmr/generic/tests.rs @@ -113,7 +113,7 @@ impl Comparable> for Person { } } -impl<'a> KeyRef<'a, Person> for PersonRef<'a> { +impl KeyRef<'_, Person> for PersonRef<'_> { fn compare(&self, a: &Q) -> cmp::Ordering where Q: ?Sized + Ord + Comparable, diff --git a/src/swmr/generic/tests/constructor.rs b/src/swmr/generic/tests/constructor.rs index 40bab658..f51bc1c1 100644 --- a/src/swmr/generic/tests/constructor.rs +++ b/src/swmr/generic/tests/constructor.rs @@ -2,7 +2,7 @@ use super::*; #[test] #[allow(clippy::needless_borrows_for_generic_args)] -fn owned_comparable() { +fn query_comparable() { let p1 = Person { id: 3127022870678870148, name: "enthusiastic-magic".into(), @@ -22,11 +22,11 @@ fn owned_comparable() { map.insert(ptr1); map.insert(ptr2); - assert!(map.contains(&Owned::new(&p1))); - assert!(map.get(&Owned::new(&p1)).is_some()); + assert!(map.contains(&Query::new(&p1))); + assert!(map.get(&Query::new(&p1)).is_some()); - assert!(map.contains(&Owned::new(&p2))); - assert!(map.get(&Owned::new(&p2)).is_some()); + assert!(map.contains(&Query::new(&p2))); + assert!(map.get(&Query::new(&p2)).is_some()); let mut wal = GenericBuilder::new() .with_capacity(MB) @@ -42,68 +42,6 @@ fn owned_comparable() { assert_eq!(wal.get(&p2).unwrap().value(), "My name is Bob!"); } -#[test] -fn ref_comparable() { - let p1 = PersonRef { - id: 3127022870678870148, - name: "enthusiastic-magic", - }; - let p2 = PersonRef { - id: 9872687799307360216, - name: "damaged-friend", - }; - - let p1bytes = p1.encode_into_vec().unwrap(); - let p2bytes = p2.encode_into_vec().unwrap(); - - let ptr1 = GenericPointer::::new(p1bytes.len(), 0, p1bytes.as_ptr()); - let ptr2 = GenericPointer::::new(p2bytes.len(), 0, p2bytes.as_ptr()); - - let map = SkipSet::new(); - map.insert(ptr1); - map.insert(ptr2); - - assert!(map.contains(&Owned::new(&p1))); - assert!(map.get(&Owned::new(&p1)).is_some()); - - assert!(map.contains(&Owned::new(&p2))); - assert!(map.get(&Owned::new(&p2)).is_some()); - - let mut wal = GenericBuilder::new() - .with_capacity(MB) - .alloc::() - .unwrap(); - - unsafe { - wal - .insert( - Generic::from_slice(p1bytes.as_ref()), - &"My name is Alice!".to_string(), - ) - .unwrap(); - wal - .insert( - Generic::from_slice(p2bytes.as_ref()), - &"My name is Bob!".to_string(), - ) - .unwrap(); - } - - assert!(wal.contains_key(&p1)); - assert_eq!(wal.get(&p1).unwrap().value(), "My name is Alice!"); - - assert!(wal.contains_key(&p2)); - assert_eq!(wal.get(&p2).unwrap().value(), "My name is Bob!"); - - unsafe { - assert!(wal.contains_key_by_bytes(&p1bytes)); - assert_eq!(wal.get(&p1).unwrap().value(), "My name is Alice!"); - - assert!(wal.contains_key_by_bytes(&p2bytes)); - assert_eq!(wal.get(&p2).unwrap().value(), "My name is Bob!"); - } -} - #[test] #[allow(clippy::needless_borrows_for_generic_args)] fn construct_inmemory() {