Skip to main content

ShardMap

Struct ShardMap 

Source
pub struct ShardMap<K, V, const SHARDS: usize = DEFAULT_CACHE_SHARDS> { /* private fields */ }
Expand description

Cloneable native Rust sharded map.

ShardMap<K, V> stores K and V directly. Keys must implement Hash and Eq, matching the ordinary Rust HashMap contract.

Implementations§

Source§

impl<K, V, const SHARDS: usize> ShardMap<K, V, SHARDS>

Source

pub fn new() -> Self

Creates an empty native typed map.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty native typed map with an approximate total capacity.

Source

pub fn with_options(options: ShardMapOptions) -> Self

Creates an empty native typed map with explicit options.

Source

pub const fn shard_count(&self) -> usize

Returns the configured number of shards.

Source

pub fn route_key<Q>(&self, key: &Q) -> EmbeddedKeyRoute
where Q: Hash + ?Sized,

Computes the route for a key.

Source

pub const fn default_ttl_ms(&self) -> Option<u64>

Returns the default TTL applied by write APIs that do not pass an explicit TTL.

Source

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

Inserts or replaces a value, returning the old value when present.

Source

pub fn insert_with_ttl( &self, key: K, value: V, ttl_ms: Option<u64>, ) -> Option<V>
where K: Eq + Hash,

Inserts or replaces a value with an optional relative TTL, returning the old live value when present.

Source

pub fn try_insert(&self, key: K, value: V) -> bool
where K: Eq + Hash,

Inserts only when the key is absent.

Source

pub fn try_insert_with_ttl(&self, key: K, value: V, ttl_ms: Option<u64>) -> bool
where K: Eq + Hash,

Inserts only when the key is absent or expired, with an optional relative TTL.

Source

pub fn get<Q>(&self, key: &Q) -> Option<V>
where K: Borrow<Q> + Eq + Hash, Q: Eq + Hash + ?Sized, V: Clone,

Returns an owned clone of the value.

Source

pub fn get_ref<Q>(&self, key: &Q) -> Option<ShardMapRef<'_, K, V>>
where K: Borrow<Q> + Eq + Hash, Q: Eq + Hash + ?Sized,

Returns a borrowed value guard.

Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q> + Eq + Hash, Q: Eq + Hash + ?Sized,

Returns true when the key is present.

Source

pub fn exists<Q>(&self, key: &Q) -> bool
where K: Borrow<Q> + Eq + Hash, Q: Eq + Hash + ?Sized,

Returns true when the key is present.

Source

pub fn remove<Q>(&self, key: &Q) -> Option<V>
where K: Borrow<Q> + Eq + Hash, Q: Eq + Hash + ?Sized,

Removes a key and returns its value when present.

Source

pub fn delete<Q>(&self, key: &Q) -> bool
where K: Borrow<Q> + Eq + Hash, Q: Eq + Hash + ?Sized,

Removes a key without returning the value.

Source

pub fn len(&self) -> usize

Returns the number of entries currently stored.

Source

pub fn is_empty(&self) -> bool

Returns true when the map contains no entries.

Source

pub fn visit_keys(&self, visitor: impl FnMut(&K) -> bool)

Visits keys without allocating a snapshot.

The visitor runs while each shard read lock is held. Keep callbacks lightweight, and return false to stop early.

Source

pub fn visit_entries(&self, visitor: impl FnMut(&K, &V) -> bool)

Visits entries without allocating a snapshot.

The visitor runs while each shard read lock is held. Keep callbacks lightweight, and return false to stop early.

Source

pub fn keys(&self) -> Vec<K>
where K: Clone,

Returns a snapshot of keys.

Source

pub fn entries(&self) -> Vec<(K, V)>
where K: Clone, V: Clone,

Returns a snapshot of entries.

Trait Implementations§

Source§

impl<K, V, const SHARDS: usize> Clone for ShardMap<K, V, SHARDS>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K, V, const SHARDS: usize> Debug for ShardMap<K, V, SHARDS>

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V, const SHARDS: usize> Default for ShardMap<K, V, SHARDS>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K, V, const SHARDS: usize = DEFAULT_CACHE_SHARDS> !RefUnwindSafe for ShardMap<K, V, SHARDS>

§

impl<K, V, const SHARDS: usize = DEFAULT_CACHE_SHARDS> !UnwindSafe for ShardMap<K, V, SHARDS>

§

impl<K, V, const SHARDS: usize> Freeze for ShardMap<K, V, SHARDS>

§

impl<K, V, const SHARDS: usize> Send for ShardMap<K, V, SHARDS>
where K: Send + Sync, V: Send + Sync,

§

impl<K, V, const SHARDS: usize> Sync for ShardMap<K, V, SHARDS>
where K: Send + Sync, V: Send + Sync,

§

impl<K, V, const SHARDS: usize> Unpin for ShardMap<K, V, SHARDS>

§

impl<K, V, const SHARDS: usize> UnsafeUnpin for ShardMap<K, V, SHARDS>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> 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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more