Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strengthen bounds on keys and values for HashMap: Sync #43

Merged
merged 1 commit into from
Dec 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@ pub struct HashMap<K, V, S = RandomState> {
raw: raw::HashMap<K, V, S>,
}

// Safety: We only ever hand out &K/V through shared references to the map,
// so normal Send/Sync rules apply. We never expose owned or mutable references
// to keys or values.
// Safety: `HashMap` acts as a single-threaded collection on a single thread.
// References to keys and values cannot outlive the map's lifetime on a given
// thread.
unsafe impl<K: Send, V: Send, S: Send> Send for HashMap<K, V, S> {}
unsafe impl<K: Sync, V: Sync, S: Sync> Sync for HashMap<K, V, S> {}

// Safety: We only ever hand out `&{K, V}` through shared references to the map,
// and never `&mut {K, V}` except through synchronized memory reclamation.
//
// However, we require `{K, V}: Send` as keys and values may be removed and dropped
// on a different thread than they were created on through shared access to the
// `HashMap`.
//
// Additionally, `HashMap` owns its `seize::Collector` and never exposes it,
// so multiple threads cannot be involved in reclamation without sharing the
// `HashMap` itself. If this was not true, we would require stricter bounds
// on `HashMap` operations themselves.
unsafe impl<K: Send + Sync, V: Send + Sync, S: Sync> Sync for HashMap<K, V, S> {}

/// A builder for a [`HashMap`].
///
Expand Down
Loading