Skip to content

Commit

Permalink
cleanup method trait bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Oct 2, 2024
1 parent a04d3c0 commit 7df58b2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 79 deletions.
13 changes: 12 additions & 1 deletion examples/zero_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
})
});

Expand Down
16 changes: 8 additions & 8 deletions src/swmr/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ where
}
}

impl<K, Q, V> Equivalent<GenericPointer<K, V>> for Query<'_, K, Q>
impl<'a, 'b: 'a, K, Q, V> Equivalent<GenericPointer<K, V>> for Query<'a, K, Q>
where
K: Type + Ord + ?Sized,
V: ?Sized,
Q: ?Sized + Ord + for<'b> Equivalent<K::Ref<'b>>,
Q: ?Sized + Ord + Equivalent<K::Ref<'b>>,
{
#[inline]
fn equivalent(&self, p: &GenericPointer<K, V>) -> bool {
Expand All @@ -180,11 +180,11 @@ where
}
}

impl<K, Q, V> Comparable<GenericPointer<K, V>> for Query<'_, K, Q>
impl<'a, 'b: 'a, K, Q, V> Comparable<GenericPointer<K, V>> for Query<'a, K, Q>
where
K: Type + Ord + ?Sized,
V: ?Sized,
Q: ?Sized + Ord + for<'b> Comparable<K::Ref<'b>>,
Q: ?Sized + Ord + Comparable<K::Ref<'b>>,
{
#[inline]
fn compare(&self, p: &GenericPointer<K, V>) -> cmp::Ordering {
Expand Down Expand Up @@ -326,9 +326,9 @@ where
}

#[inline]
fn get<Q>(&self, key: &Q) -> Option<GenericEntryRef<'_, K, V>>
fn get<'a, 'b: 'a, Q>(&'a self, key: &'b Q) -> Option<GenericEntryRef<'a, K, V>>
where
Q: ?Sized + Ord + for<'b> Comparable<K::Ref<'b>>,
Q: ?Sized + Ord + Comparable<K::Ref<'b>>,
{
self
.map
Expand Down Expand Up @@ -511,9 +511,9 @@ where

/// Gets the value associated with the key.
#[inline]
pub fn get<Q>(&self, key: &Q) -> Option<GenericEntryRef<'_, K, V>>
pub fn get<'a, 'b: 'a, Q>(&'a self, key: &'b Q) -> Option<GenericEntryRef<'a, K, V>>
where
Q: ?Sized + Ord + for<'b> Comparable<K::Ref<'b>>,
Q: ?Sized + Ord + Comparable<K::Ref<'b>>,
{
self.core.get(key)
}
Expand Down
4 changes: 2 additions & 2 deletions src/swmr/generic/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ where

/// Gets the value associated with the key.
#[inline]
pub fn get<Q>(&self, key: &Q) -> Option<GenericEntryRef<'_, K, V>>
pub fn get<'a, 'b: 'a, Q>(&'a self, key: &'b Q) -> Option<GenericEntryRef<'a, K, V>>
where
Q: ?Sized + Ord + for<'b> Comparable<K::Ref<'b>>,
Q: ?Sized + Ord + Comparable<K::Ref<'b>>,
{
self.0.get(key)
}
Expand Down
2 changes: 1 addition & 1 deletion src/swmr/generic/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Comparable<PersonRef<'_>> for Person {
}
}

impl<'a> KeyRef<'a, Person> for PersonRef<'a> {
impl KeyRef<'_, Person> for PersonRef<'_> {
fn compare<Q>(&self, a: &Q) -> cmp::Ordering
where
Q: ?Sized + Ord + Comparable<Self>,
Expand Down
72 changes: 5 additions & 67 deletions src/swmr/generic/tests/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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)
Expand All @@ -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::<Person, String>::new(p1bytes.len(), 0, p1bytes.as_ptr());
let ptr2 = GenericPointer::<Person, String>::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::<Person, String>()
.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() {
Expand Down

0 comments on commit 7df58b2

Please sign in to comment.