Skip to main content

MetadataStore

Struct MetadataStore 

Source
pub struct MetadataStore(/* private fields */);
Expand description

A lightweight key-value metadata store backed by a HashMap.

MetadataStore is designed for storing arbitrary string metadata such as task attributes, labels, annotations, headers, or contextual information.

Keys are unique within the store. Attempting to insert a duplicate key returns a MetadataError::DuplicateKey error.

§Examples

let mut metadata = MetadataStore::new();

metadata.insert("request_id", "abc-123")?;
metadata.insert("environment", "production")?;

assert_eq!(
    metadata.get("request_id"),
    Some(&"abc-123".to_string())
);

assert!(metadata.contains_key("environment"));

Implementations§

Source§

impl MetadataStore

Source

pub fn new() -> Self

Creates an empty MetadataStore.

§Examples
let metadata = MetadataStore::new();

assert_eq!(metadata.iter().count(), 0);
Source

pub fn insert<K, V>(&mut self, key: K, value: V) -> Result<(), MetadataError>
where K: Into<String>, V: Into<String>,

Inserts a key-value pair into the store.

Returns an error if the key already exists.

§Errors

Returns MetadataError::DuplicateKey if the provided key is already present in the store.

§Examples
let mut metadata = MetadataStore::new();

metadata.insert("region", "us-east-1")?;

assert_eq!(
    metadata.get("region"),
    Some(&"us-east-1".to_string())
);

Duplicate keys are rejected:

let mut metadata = MetadataStore::new();

metadata.insert("service", "api")?;

let err = metadata.insert("service", "worker").unwrap_err();

assert_eq!(
    err,
    MetadataError::DuplicateKey("service".to_string())
);
Source

pub fn get(&self, key: &str) -> Option<&String>

Returns a reference to the value corresponding to the given key.

Returns None if the key does not exist.

§Examples
let mut metadata = MetadataStore::new();

metadata.insert("version", "1.0")?;

assert_eq!(
    metadata.get("version"),
    Some(&"1.0".to_string())
);

assert_eq!(metadata.get("missing"), None);
Source

pub fn remove(&mut self, key: &str) -> Option<String>

Removes a key from the store, returning the stored value if it existed.

§Examples
let mut metadata = MetadataStore::new();

metadata.insert("token", "secret")?;

assert_eq!(
    metadata.remove("token"),
    Some("secret".to_string())
);

assert!(!metadata.contains_key("token"));
Source

pub fn contains_key(&self, key: &str) -> bool

Returns true if the store contains the specified key.

§Examples
let mut metadata = MetadataStore::new();

metadata.insert("owner", "alice")?;

assert!(metadata.contains_key("owner"));
assert!(!metadata.contains_key("missing"));
Source

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

Returns an iterator over all key-value pairs in the store.

The iterator yields (&String, &String) pairs.

§Examples
let mut metadata = MetadataStore::new();

metadata.insert("a", "1")?;
metadata.insert("b", "2")?;

let items: Vec<_> = metadata.iter().collect();

assert_eq!(items.len(), 2);
Source

pub fn into_inner(self) -> HashMap<String, String>

Consumes the store and returns the underlying HashMap.

§Examples
let mut metadata = MetadataStore::new();

metadata.insert("key", "value")?;

let inner = metadata.into_inner();

assert_eq!(
    inner.get("key"),
    Some(&"value".to_string())
);
Source

pub fn extract_as<M: Metadata>(&self) -> Result<M, M::Error>

Get a typed metadata entry.

Source

pub fn from_map(map: HashMap<String, String>) -> Self

Create a MetadataStore from a HashMap<String, String>.

Trait Implementations§

Source§

impl Clone for MetadataStore

Source§

fn clone(&self) -> MetadataStore

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 MetadataStore

Source§

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

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

impl Default for MetadataStore

Source§

fn default() -> MetadataStore

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

impl<'de> Deserialize<'de> for MetadataStore

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 Eq for MetadataStore

Source§

impl<Args: Send + Sync> FromRequest<Task<Args>> for MetadataStore

Source§

type Error = !

The error type that can occur during extraction.
Source§

async fn from_request(task: &Task<Args>) -> Result<Self, Self::Error>

Perform the extraction.
Source§

impl PartialEq for MetadataStore

Source§

fn eq(&self, other: &MetadataStore) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for MetadataStore

Available on crate feature serde only.
Source§

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

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for MetadataStore

Auto Trait Implementations§

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

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

Checks if this value is equivalent to the given key. 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 = !

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