Skip to content

Commit

Permalink
avoid passing Table by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheemdev committed Nov 29, 2024
1 parent ef4f399 commit 2a5fc43
Show file tree
Hide file tree
Showing 5 changed files with 619 additions and 507 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ The `Guard` trait supports both local and owned guards. Note the `'guard` lifeti
*/

#![deny(missing_debug_implementations, missing_docs, dead_code)]
// We use some polyfills for unstable APIs related to strict-provenance.
// Polyfills for unstable APIs related to strict-provenance.
#![allow(unstable_name_collisions)]
// Stylistic preferences.
#![allow(clippy::multiple_bound_locations, clippy::single_match)]
Expand Down
134 changes: 96 additions & 38 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,10 @@ where
K: Borrow<Q> + 'g,
Q: Hash + Eq + ?Sized,
{
match self.raw.root(guard).get(key, guard) {
self.raw.check_guard(guard);

// Safety: Checked the guard above.
match unsafe { self.raw.get(key, guard) } {
Some((_, v)) => Some(v),
None => None,
}
Expand Down Expand Up @@ -470,7 +473,10 @@ where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.raw.root(guard).get(key, guard)
self.raw.check_guard(guard);

// Safety: Checked the guard above.
unsafe { self.raw.get(key, guard) }
}

/// Inserts a key-value pair into the map.
Expand Down Expand Up @@ -502,7 +508,10 @@ where
/// ```
#[inline]
pub fn insert<'g>(&self, key: K, value: V, guard: &'g impl Guard) -> Option<&'g V> {
match self.raw.root(guard).insert(key, value, true, guard) {
self.raw.check_guard(guard);

// Safety: Checked the guard above.
match unsafe { self.raw.insert(key, value, true, guard) } {
InsertResult::Inserted(_) => None,
InsertResult::Replaced(value) => Some(value),
InsertResult::Error { .. } => unreachable!(),
Expand Down Expand Up @@ -535,7 +544,10 @@ where
value: V,
guard: &'g impl Guard,
) -> Result<&'g V, OccupiedError<'g, V>> {
match self.raw.root(guard).insert(key, value, false, guard) {
self.raw.check_guard(guard);

// Safety: Checked the guard above.
match unsafe { self.raw.insert(key, value, false, guard) } {
InsertResult::Inserted(value) => Ok(value),
InsertResult::Error {
current,
Expand Down Expand Up @@ -564,6 +576,8 @@ where
/// ```
#[inline]
pub fn get_or_insert<'g>(&self, key: K, value: V, guard: &'g impl Guard) -> &'g V {
self.raw.check_guard(guard);

// Note that we use `try_insert` instead of `compute` or `get_or_insert_with` here, as it
// allows us to avoid the closure indirection.
match self.try_insert(key, value, guard) {
Expand Down Expand Up @@ -595,7 +609,10 @@ where
F: FnOnce() -> V,
K: 'g,
{
self.raw.root(guard).get_or_insert_with(key, f, guard)
self.raw.check_guard(guard);

// Safety: Checked the guard above.
unsafe { self.raw.get_or_insert_with(key, f, guard) }
}

/// Updates an existing entry atomically.
Expand Down Expand Up @@ -633,7 +650,10 @@ where
F: Fn(&V) -> V,
K: 'g,
{
self.raw.root(guard).update(key, update, guard)
self.raw.check_guard(guard);

// Safety: Checked the guard above.
unsafe { self.raw.update(key, update, guard) }
}

/// Updates an existing entry or inserts a default value.
Expand Down Expand Up @@ -665,6 +685,8 @@ where
F: Fn(&V) -> V,
K: 'g,
{
self.raw.check_guard(guard);

self.update_or_insert_with(key, update, || value, guard)
}

Expand Down Expand Up @@ -699,9 +721,10 @@ where
U: Fn(&V) -> V,
K: 'g,
{
self.raw
.root(guard)
.update_or_insert_with(key, update, f, guard)
self.raw.check_guard(guard);

// Safety: Checked the guard above.
unsafe { self.raw.update_or_insert_with(key, update, f, guard) }
}

/// Updates an entry with a compare-and-swap (CAS) function.
Expand Down Expand Up @@ -759,7 +782,10 @@ where
where
F: FnMut(Option<(&'g K, &'g V)>) -> Operation<V, T>,
{
self.raw.root(guard).compute(key, compute, guard)
self.raw.check_guard(guard);

// Safety: Checked the guard above.
unsafe { self.raw.compute(key, compute, guard) }
}

/// Removes a key from the map, returning the value at the key if the key
Expand All @@ -785,7 +811,10 @@ where
K: Borrow<Q> + 'g,
Q: Hash + Eq + ?Sized,
{
match self.raw.root(guard).remove(key, guard) {
self.raw.check_guard(guard);

// Safety: Checked the guard above.
match unsafe { self.raw.remove(key, guard) } {
Some((_, value)) => Some(value),
None => None,
}
Expand Down Expand Up @@ -815,7 +844,10 @@ where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.raw.root(guard).remove(key, guard)
self.raw.check_guard(guard);

// Safety: Checked the guard above.
unsafe { self.raw.remove(key, guard) }
}

/// Tries to reserve capacity for `additional` more elements to be inserted
Expand All @@ -840,7 +872,10 @@ where
/// ```
#[inline]
pub fn reserve(&self, additional: usize, guard: &impl Guard) {
self.raw.root(guard).reserve(additional, guard);
self.raw.check_guard(guard);

// Safety: Checked the guard above.
unsafe { self.raw.reserve(additional, guard) };
}

/// Clears the map, removing all key-value pairs.
Expand All @@ -862,7 +897,10 @@ where
/// ```
#[inline]
pub fn clear(&self, guard: &impl Guard) {
self.raw.root(guard).clear(guard)
self.raw.check_guard(guard);

// Safety: Checked the guard above.
unsafe { self.raw.clear(guard) }
}

/// Retains only the elements specified by the predicate.
Expand Down Expand Up @@ -891,7 +929,10 @@ where
where
F: FnMut(&K, &V) -> bool,
{
self.raw.root(guard).retain(f, guard)
self.raw.check_guard(guard);

// Safety: Checked the guard above.
unsafe { self.raw.retain(f, guard) }
}

/// An iterator visiting all key-value pairs in arbitrary order.
Expand Down Expand Up @@ -920,8 +961,11 @@ where
where
G: Guard,
{
self.raw.check_guard(guard);

Iter {
raw: self.raw.root(guard).iter(guard),
// Safety: Checked the guard above.
raw: unsafe { self.raw.iter(guard) },
}
}

Expand Down Expand Up @@ -952,6 +996,8 @@ where
where
G: Guard,
{
self.raw.check_guard(guard);

Keys {
iter: self.iter(guard),
}
Expand Down Expand Up @@ -984,6 +1030,8 @@ where
where
G: Guard,
{
self.raw.check_guard(guard);

Values {
iter: self.iter(guard),
}
Expand Down Expand Up @@ -1238,7 +1286,8 @@ where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
match self.root().get(key, &self.guard) {
// Safety: `self.guard` was created from our map.
match unsafe { self.map.raw.get(key, &self.guard) } {
Some((_, v)) => Some(v),
None => None,
}
Expand All @@ -1253,15 +1302,17 @@ where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.root().get(key, &self.guard)
// Safety: `self.guard` was created from our map.
unsafe { self.map.raw.get(key, &self.guard) }
}

/// Inserts a key-value pair into the map.
///
/// See [`HashMap::insert`] for details.
#[inline]
pub fn insert(&self, key: K, value: V) -> Option<&V> {
match self.root().insert(key, value, true, &self.guard) {
// Safety: `self.guard` was created from our map.
match unsafe { self.map.raw.insert(key, value, true, &self.guard) } {
InsertResult::Inserted(_) => None,
InsertResult::Replaced(value) => Some(value),
InsertResult::Error { .. } => unreachable!(),
Expand All @@ -1274,7 +1325,8 @@ where
/// See [`HashMap::try_insert`] for details.
#[inline]
pub fn try_insert(&self, key: K, value: V) -> Result<&V, OccupiedError<'_, V>> {
match self.root().insert(key, value, false, &self.guard) {
// Safety: `self.guard` was created from our map.
match unsafe { self.map.raw.insert(key, value, false, &self.guard) } {
InsertResult::Inserted(value) => Ok(value),
InsertResult::Error {
current,
Expand Down Expand Up @@ -1309,7 +1361,8 @@ where
where
F: FnOnce() -> V,
{
self.root().get_or_insert_with(key, f, &self.guard)
// Safety: `self.guard` was created from our map.
unsafe { self.map.raw.get_or_insert_with(key, f, &self.guard) }
}

/// Updates an existing entry atomically.
Expand All @@ -1320,7 +1373,8 @@ where
where
F: Fn(&V) -> V,
{
self.root().update(key, update, &self.guard)
// Safety: `self.guard` was created from our map.
unsafe { self.map.raw.update(key, update, &self.guard) }
}

/// Updates an existing entry or inserts a default value.
Expand All @@ -1343,8 +1397,12 @@ where
F: FnOnce() -> V,
U: Fn(&V) -> V,
{
self.root()
.update_or_insert_with(key, update, f, &self.guard)
// Safety: `self.guard` was created from our map.
unsafe {
self.map
.raw
.update_or_insert_with(key, update, f, &self.guard)
}
}

// Updates an entry with a compare-and-swap (CAS) function.
Expand All @@ -1355,7 +1413,8 @@ where
where
F: FnMut(Option<(&'g K, &'g V)>) -> Operation<V, T>,
{
self.root().compute(key, compute, &self.guard)
// Safety: `self.guard` was created from our map.
unsafe { self.map.raw.compute(key, compute, &self.guard) }
}

/// Removes a key from the map, returning the value at the key if the key
Expand All @@ -1368,7 +1427,8 @@ where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
match self.root().remove(key, &self.guard) {
// Safety: `self.guard` was created from our map.
match unsafe { self.map.raw.remove(key, &self.guard) } {
Some((_, value)) => Some(value),
None => None,
}
Expand All @@ -1384,15 +1444,17 @@ where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.root().remove(key, &self.guard)
// Safety: `self.guard` was created from our map.
unsafe { self.map.raw.remove(key, &self.guard) }
}

/// Clears the map, removing all key-value pairs.
///
/// See [`HashMap::clear`] for details.
#[inline]
pub fn clear(&self) {
self.root().clear(&self.guard)
// Safety: `self.guard` was created from our map.
unsafe { self.map.raw.clear(&self.guard) }
}

/// Retains only the elements specified by the predicate.
Expand All @@ -1403,7 +1465,8 @@ where
where
F: FnMut(&K, &V) -> bool,
{
self.root().retain(f, &self.guard)
// Safety: `self.guard` was created from our map.
unsafe { self.map.raw.retain(f, &self.guard) }
}

/// Tries to reserve capacity for `additional` more elements to be inserted
Expand All @@ -1412,7 +1475,8 @@ where
/// See [`HashMap::reserve`] for details.
#[inline]
pub fn reserve(&self, additional: usize) {
self.root().reserve(additional, &self.guard)
// Safety: `self.guard` was created from our map.
unsafe { self.map.raw.reserve(additional, &self.guard) }
}

/// An iterator visiting all key-value pairs in arbitrary order.
Expand All @@ -1422,7 +1486,8 @@ where
#[inline]
pub fn iter(&self) -> Iter<'_, K, V, G> {
Iter {
raw: self.root().iter(&self.guard),
// Safety: `self.guard` was created from our map.
raw: unsafe { self.map.raw.iter(&self.guard) },
}
}

Expand All @@ -1443,13 +1508,6 @@ where
pub fn values(&self) -> Values<'_, K, V, G> {
Values { iter: self.iter() }
}

#[inline]
fn root(&self) -> raw::HashMapRef<'_, K, V, S> {
// Safety: A `HashMapRef` can only be created through `HashMap::pin` or
// `HashMap::pin_owned`, so we know the guard belongs to our collector.
unsafe { self.map.raw.root_unchecked(&self.guard) }
}
}

impl<K, V, S, G> fmt::Debug for HashMapRef<'_, K, V, S, G>
Expand Down
Loading

0 comments on commit 2a5fc43

Please sign in to comment.