Skip to main content

ShardedHashMap

Struct ShardedHashMap 

Source
pub struct ShardedHashMap<K, V, S = FxBuildHasher>
where K: Eq + Hash + Clone + Send + Sync, V: Clone + Send + Sync, S: BuildHasher + Clone + Send + Sync,
{ /* private fields */ }
Expand description

Sharded concurrent HashMap (synchronous).

Cloning the map is cheap (Arc handles + atomic length).

Implementations§

Source§

impl<K, V> ShardedHashMap<K, V, FxBuildHasher>
where K: Eq + Hash + Clone + Send + Sync, V: Clone + Send + Sync,

Source

pub fn new(shard_count: usize) -> Self

Create with default hasher (FxBuildHasher).

Source§

impl<K, V, S> ShardedHashMap<K, V, S>
where K: Eq + Hash + Clone + Send + Sync, V: Clone + Send + Sync, S: BuildHasher + Clone + Send + Sync,

Source

pub fn with_shards_and_hasher(shard_count: usize, hasher: S) -> Self

Create with explicit hasher (non-zero shard count fallback).

Source

pub fn shard_count(&self) -> usize

Current configured shard slots.

Source

pub fn initialized_shards(&self) -> usize

Number of shards actually initialized (allocated).

Source

pub fn insert(&self, key: K, value: V) -> Option<V>

Insert key/value. Returns previous value if existed.

Complexity: O(1) expected.

If the key was not present, increments length counter.

§Arguments
  • key: key to insert.
  • value: value to associate with the key
§Returns
  • Option<V>: previous value if the key was already present.
Source

pub fn get(&self, key: &K) -> Option<V>

Fetch cloned value.

§Arguments
  • key: key to look up.
§Returns
  • Option<V>: cloned value if the key exists.
Source

pub fn contains(&self, key: &K) -> bool

Check if a key exists (returns bool without cloning the value).

§Arguments
  • key: key to check.
§Returns
  • bool: true if the key exists in the map, false otherwise.
Source

pub fn remove(&self, key: &K) -> Option<V>

Remove key, returning previous value.

§Arguments
  • key: key to remove.
§Returns
  • Option<V>: previous value if the key existed.
Source

pub fn len(&self) -> usize

Length (cached atomic).

§Returns
  • usize: total number of key/value pairs in the map.
Source

pub fn is_empty(&self) -> bool

Whether empty.

§Returns
  • bool: true if the map contains no entries.
Source

pub fn clear(&self)

Clear all data (retains shard allocations).

§Notes
  • Resets length counter to zero.
Source

pub fn iter(&self) -> impl Iterator<Item = (K, V)>

Snapshot iteration over (K,V) clones.

Semantics:

  • Collects a list of initialized shard Arcs first (short critical section).
  • Each shard is read-locked independently; values cloned.
  • Not a live iterator: modifications after a shard snapshot are not reflected.
  • If rayon enabled, internal flattening per-shard happens in parallel for speed.

Cost:

  • O(N) cloning cost for visited entries.
  • Temporary Vec allocations proportional to initialized shard count (and item copies).
§Returns
  • impl Iterator<Item = (K, V)>: iterator over cloned key/value pairs.

Trait Implementations§

Source§

impl<K, V, S> Clone for ShardedHashMap<K, V, S>
where K: Eq + Hash + Clone + Send + Sync + Clone, V: Clone + Send + Sync + Clone, S: BuildHasher + Clone + Send + Sync + Clone,

Source§

fn clone(&self) -> ShardedHashMap<K, V, S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'de, K, V, S> Deserialize<'de> for ShardedHashMap<K, V, S>
where K: Eq + Hash + Clone + Send + Sync + Deserialize<'de>, V: Clone + Send + Sync + Deserialize<'de>, S: BuildHasher + Clone + Send + Sync + Default,

Available on crate feature serde only.
Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<K, V, S> Serialize for ShardedHashMap<K, V, S>
where K: Eq + Hash + Clone + Send + Sync + Serialize, V: Clone + Send + Sync + Serialize, S: BuildHasher + Clone + Send + Sync + Default,

Available on crate feature serde only.
Source§

fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
where Ser: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<K, V, S> Freeze for ShardedHashMap<K, V, S>
where S: Freeze,

§

impl<K, V, S> RefUnwindSafe for ShardedHashMap<K, V, S>
where S: RefUnwindSafe,

§

impl<K, V, S> Send for ShardedHashMap<K, V, S>

§

impl<K, V, S> Sync for ShardedHashMap<K, V, S>

§

impl<K, V, S> Unpin for ShardedHashMap<K, V, S>
where S: Unpin,

§

impl<K, V, S> UnwindSafe for ShardedHashMap<K, V, S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,