Skip to content

Commit

Permalink
- Make unsafe blocks smaller and more refined.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bwallker committed Nov 12, 2023
1 parent 1a6bf25 commit 325cc43
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
9 changes: 4 additions & 5 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,12 @@ impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>> Iter
if let Some((k, v)) = current.1.next() {
let guard = current.0.clone();

unsafe {
let k = util::change_lifetime_const(k);
let k = k;

let v = v.get_mut();
let v = v.get_mut();

return Some(RefMutMulti::new(guard, k, v));
}
return Some(unsafe { RefMutMulti::new(guard, k, v) });

}
}

Expand Down
35 changes: 15 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
if let Some((kptr, vptr)) = shard.get_key_value_mut(key) {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.get_mut();
Some(unsafe {RefMut::new(shard, kptr, vptr)})
Some(unsafe { RefMut::new(shard, kptr, vptr) })
} else {
None
}
Expand Down Expand Up @@ -1075,11 +1075,9 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
};

if let Some((kptr, vptr)) = shard.get_key_value_mut(key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.get_mut();
TryResult::Present(RefMut::new(shard, kptr, vptr))
}
let kptr: *const K = kptr;
let vptr: *mut V = vptr.get_mut();
TryResult::Present(unsafe { RefMut::new(shard, kptr, vptr) })
} else {
TryResult::Absent
}
Expand Down Expand Up @@ -1140,11 +1138,9 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
let mut shard = unsafe { self._yield_write_shard(idx) };

if let Some((kptr, vptr)) = shard.get_key_value_mut(&key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.get_mut();
Entry::Occupied(OccupiedEntry::new(shard, key, (kptr, vptr)))
}
let kptr: *const K = kptr;
let vptr: *mut V = vptr.get_mut();
Entry::Occupied(unsafe {OccupiedEntry::new(shard, key, (kptr, vptr))})
} else {
unsafe { Entry::Vacant(VacantEntry::new(shard, key)) }
}
Expand All @@ -1161,16 +1157,15 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
};

if let Some((kptr, vptr)) = shard.get_key_value_mut(&key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.get_mut();
let kptr: *const K = kptr;
let vptr: *mut V = vptr.get_mut();

Some(Entry::Occupied(OccupiedEntry::new(
shard,
key,
(kptr, vptr),
)))
}
Some(Entry::Occupied(unsafe {OccupiedEntry::new(
shard,
key,
(kptr, vptr),
)}))

} else {
unsafe { Some(Entry::Vacant(VacantEntry::new(shard, key))) }
}
Expand Down

0 comments on commit 325cc43

Please sign in to comment.