pub struct StaticPartitionMap<K, V> { /* private fields */ }
Expand description

A data structure for persisting and sharing state between multiple clients.

Some state should be shared between multiple clients. For example, when creating multiple clients for the same service, it’s desirable to share a client rate limiter. This way, when one client receives a throttling response, the other clients will be aware of it as well.

Whether clients share state is dependent on their partition key K. Going back to the client rate limiter example, K would be a struct containing the name of the service as well as the client’s configured region, since receiving throttling responses in us-east-1 shouldn’t throttle requests to the same service made in other regions.

Values stored in a StaticPartitionMap will be cloned whenever they are requested. Values must be initialized before they can be retrieved, and the StaticPartitionMap::get_or_init method is how you can ensure this.

§Example

use std::sync::{Arc, Mutex};
use aws_smithy_runtime::static_partition_map::StaticPartitionMap;

// The shared state must be `Clone` and will be internally mutable. Deriving `Default` isn't
// necessary, but allows us to use the `StaticPartitionMap::get_or_init_default` method.
#[derive(Clone, Default)]
pub struct SomeSharedState {
    inner: Arc<Mutex<Inner>>
}

#[derive(Default)]
struct Inner {
    // Some shared state...
}

// `Clone`, `Hash`, and `Eq` are all required trait impls for partition keys
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct SharedStatePartition {
    region: String,
    service_name: String,
}

impl SharedStatePartition {
    pub fn new(region: impl Into<String>, service_name: impl Into<String>) -> Self {
        Self { region: region.into(), service_name: service_name.into() }
    }
}

static SOME_SHARED_STATE: StaticPartitionMap<SharedStatePartition, SomeSharedState> = StaticPartitionMap::new();

struct Client {
    shared_state: SomeSharedState,
}

impl Client {
    pub fn new() -> Self {
        let key = SharedStatePartition::new("us-east-1", "example_service_20230628");
        Self {
            // If the stored value implements `Default`, you can call the
            // `StaticPartitionMap::get_or_init_default` convenience method.
            shared_state: SOME_SHARED_STATE.get_or_init_default(key),
        }
    }
}

Implementations§

source§

impl<K, V> StaticPartitionMap<K, V>

source

pub const fn new() -> Self

Creates a new StaticPartitionMap.

source§

impl<K, V> StaticPartitionMap<K, V>
where K: Eq + Hash, V: Clone,

source

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

Gets the value for the given partition key.

source

pub fn get_or_init<F>(&self, partition_key: K, init: F) -> V
where F: FnOnce() -> V,

Gets the value for the given partition key, initializing it with init if it doesn’t exist.

source§

impl<K, V> StaticPartitionMap<K, V>
where K: Eq + Hash, V: Clone + Default,

source

pub fn get_or_init_default(&self, partition_key: K) -> V

Gets the value for the given partition key, initializing it if it doesn’t exist.

Trait Implementations§

source§

impl<K: Debug, V: Debug> Debug for StaticPartitionMap<K, V>

source§

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

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

impl<K: Default, V: Default> Default for StaticPartitionMap<K, V>

source§

fn default() -> StaticPartitionMap<K, V>

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

Auto Trait Implementations§

§

impl<K, V> !Freeze for StaticPartitionMap<K, V>

§

impl<K, V> RefUnwindSafe for StaticPartitionMap<K, V>

§

impl<K, V> Send for StaticPartitionMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for StaticPartitionMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Unpin for StaticPartitionMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for StaticPartitionMap<K, V>

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> 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<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
source§

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

§

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>,

§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

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