Skip to main content

SharedKeyCache

Struct SharedKeyCache 

Source
pub struct SharedKeyCache { /* private fields */ }
Expand description

Lets callers find the same live shared value by key.

Clones share the same cache. Self::new creates a separate cache. Cache entries do not keep values or subscriptions alive. A reader or writable handle keeps its value alive. The last reference to the value drops the subscription.

Self::reader and Self::shared reuse a live value without loading or subscribing again. On a miss, each call evaluates its seed, asks the provider to load, then subscribes. Concurrent misses for a compatible key can both run these steps. If one call finds the other’s live value, it uses that value and drops its own subscription outside the cache lock. Self::require_reader and Self::require always check storage first, even on a cache hit.

The stored value can change between the load and the subscription. The cache does not replay missed updates. To close this gap, the provider must replay updates, register atomically with a snapshot, or reconcile revisions. A provider can also deliver a new value during subscription, before the caller receives the handle.

Implementations§

Source§

impl SharedKeyCache

Source

pub fn new() -> Self

Creates an empty independent namespace without provider work.

Source

pub async fn shared<Key: SharedKey>( &self, key: Key, seed: impl FnOnce() -> Key::Value, ) -> Result<Shared<Key::Value>, SharedKeyError<Key::Error>>

Returns a writable handle with its own binding to the shared value.

This method uses the same load and subscription rules as Self::reader. Rebinding this handle does not rebind a handle from another call for the same key. Clones of this handle do follow the new binding. See Shared::rebind.

§Errors

Returns collision, metadata poison, or an original load/subscription failure.

§Panics

Propagates the same callback and destructor panics as Self::reader.

Source

pub async fn reader<Key: SharedReaderKey>( &self, key: Key, seed: impl FnOnce() -> Key::Value, ) -> Result<SharedReader<Key::Value>, SharedKeyError<Key::Error>>

Returns a reader for a live value or asks the provider for one.

On a cache miss, the seed runs even if storage contains a value. If storage has no value, the cache uses the seed as the initial value before it subscribes. See SharedKeyCache for simultaneous misses and updates between load and subscription.

§Errors

Returns collision, metadata poison, or an original load/subscription failure.

§Panics

Propagates seed, provider and discarded value/resource destructor panics. These callbacks run without the cache lock; publication may already have occurred before cleanup panics.

Source

pub async fn shared_default<Key: SharedKey, Seed: Fn() -> Key::Value>( &self, default: SharedKeyDefault<Key, Seed>, ) -> Result<Shared<Key::Value>, SharedKeyError<Key::Error>>

Constructs with a default closure while preserving the base key identity.

§Errors

Returns the same failures as Self::shared.

§Panics

Propagates the same callback and destructor panics as Self::reader.

Source

pub async fn reader_default<Key: SharedReaderKey, Seed: Fn() -> Key::Value>( &self, default: SharedKeyDefault<Key, Seed>, ) -> Result<SharedReader<Key::Value>, SharedKeyError<Key::Error>>

Constructs a reader without adding the default wrapper to cache identity.

§Errors

Returns the same failures as Self::reader.

§Panics

Propagates the same callback and destructor panics as Self::reader.

Source

pub async fn require<Key: SharedKey>( &self, key: Key, ) -> Result<Shared<Key::Value>, SharedKeyError<Key::Error>>

Requires stored data before creating or reusing a writable reference.

Use SharedKeyDefault::into_key to require a default key without using its seed. A live cached value may win over the newly loaded value.

§Errors

Returns absence, collision, poison, or original load/subscription failure.

§Panics

Propagates the same provider and destructor panics as Self::require_reader.

Source

pub async fn require_reader<Key: SharedReaderKey>( &self, key: Key, ) -> Result<SharedReader<Key::Value>, SharedKeyError<Key::Error>>

Requires stored data before looking up a read-only reference.

§Errors

Returns absence even with a live cache hit, or collision/poison/provider failure.

§Panics

Propagates provider and discarded value/resource destructor panics. Cleanup completes before a selected reader or original provider cause is returned.

Source

pub async fn reload<Key: SharedReaderKey>( &self, shared: &Shared<Key::Value>, key: &Key, ) -> Result<(), SharedKeyError<Key::Error>>

Reloads a captured writable reference, retaining its current value on absence.

Concurrent rebinding does not retarget this operation after capture. Only the latest started reload on that reference may replace its value; completion, failure or cancellation never revives an older request.

§Errors

Returns incompatible key, access failure, or original provider failure.

§Panics

Propagates provider and displaced-value cleanup panics. Releasing the captured reference can also run a panicking destructor before return.

Source

pub async fn reload_reader<Key: SharedReaderKey>( &self, reader: &SharedReader<Key::Value>, key: &Key, ) -> Result<(), SharedKeyError<Key::Error>>

Reloads a read-only captured reference without granting mutation access.

§Errors

Returns incompatible key, value-access failure, or original provider failure.

§Panics

Propagates provider and displaced or rejected value destructor panics.

Source

pub async fn load<Key: SharedKey>( &self, shared: &Shared<Key::Value>, key: Key, seed: impl FnOnce(&Key::Value) -> Key::Value, ) -> Result<(), SharedKeyError<Key::Error>>

Rebinds to a key before reloading, seeding fresh owners from current data.

seed runs synchronously only on a target-cache miss and supplies an owned current-value snapshot without requiring Clone. The new owner skips initial load and subscribes with user intent. Reload failure does not roll the binding back. Unwrap default keys with into_key.

§Errors

Returns collision, cache/value access, subscription or explicit-load failure.

§Panics

A seed callback panic poisons the captured source value. Provider or displaced value/resource destructor panics also propagate. The receiving handle and its clones can already refer to the replacement value.

Source

pub async fn load_reader<Key: SharedReaderKey>( &self, reader: &mut SharedReader<Key::Value>, key: Key, seed: impl FnOnce(&Key::Value) -> Key::Value, ) -> Result<(), SharedKeyError<Key::Error>>

Replaces this reader’s reference before explicitly reloading the selected key.

Other reader clones retain their previously captured reference.

§Errors

Returns collision, cache/value access, subscription or explicit-load failure.

§Panics

A seed callback panic poisons the captured source value. Provider or displaced value/resource destructor panics also propagate. This reader may already be replaced and is not rolled back.

Source

pub fn reset(&self) -> Result<(), SharedKeyError<Infallible>>

Clears weak lookup entries without revoking owners or touching storage.

In-flight constructors may publish again after this reset.

§Errors

Returns metadata poison if another operation panicked while holding its lock.

Trait Implementations§

Source§

impl Clone for SharedKeyCache

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 Debug for SharedKeyCache

Source§

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

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

impl Default for SharedKeyCache

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Forget for SharedKeyCache

§

impl Freeze for SharedKeyCache
where *const ArcInner<Mutex<HashMap<Box<str>, Entry>>> is !null: Freeze, PhantomData<ArcInner<Mutex<HashMap<Box<str>, Entry>>>>: Freeze,

§

impl Move for SharedKeyCache

§

impl RefUnwindSafe for SharedKeyCache

§

impl Send for SharedKeyCache
where Arc<Mutex<HashMap<Box<str>, Entry>>>: Send,

§

impl Sync for SharedKeyCache
where Arc<Mutex<HashMap<Box<str>, Entry>>>: Sync,

§

impl Unpin for SharedKeyCache
where Arc<Mutex<HashMap<Box<str>, Entry>>>: Unpin,

§

impl UnsafeUnpin for SharedKeyCache
where *const ArcInner<Mutex<HashMap<Box<str>, Entry>>> is !null: UnsafeUnpin, PhantomData<ArcInner<Mutex<HashMap<Box<str>, Entry>>>>: UnsafeUnpin,

§

impl UnwindSafe for SharedKeyCache
where Arc<Mutex<HashMap<Box<str>, Entry>>>: 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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.