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")
);

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")
);

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<&str>

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")
);

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

Source

pub fn len(&self) -> usize

Returns the number of entries in the store.

Source

pub fn is_empty(&self) -> bool

Returns true if the store contains no entries.

Source

pub fn clear(&mut self)

Removes all entries from the store.

Source

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

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

Returns None if the key does not exist.

Source

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

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

Source

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

Returns an iterator over all keys in the store.

Source

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

Returns an iterator over all values in the store.

Source

pub fn values_mut(&mut self) -> impl Iterator<Item = &mut String>

Returns a mutable iterator over all values in the store.

Source

pub fn extend<I, K, V>(&mut self, iter: I)
where I: IntoIterator<Item = (K, V)>, K: Into<String>, V: Into<String>,

Extends the store with key-value pairs.

Existing keys are overwritten.

Use MetadataStore::insert when duplicate keys should be rejected.

Source

pub fn as_map(&self) -> &HashMap<String, String>

Returns a reference to the underlying map.

Source

pub fn replace<K, V>(&mut self, key: K, value: V) -> Option<String>
where K: AsRef<str>, V: Into<String>,

Updates an existing value.

Returns None if the key does not exist.

Source

pub fn insert_if_absent<K, V>(&mut self, key: K, value: V) -> bool
where K: Into<String>, V: Into<String>,

Inserts a value only if the key does not already exist.

Trait Implementations§

Source§

impl AsMut<HashMap<String, String>> for MetadataStore

Source§

fn as_mut(&mut self) -> &mut HashMap<String, String>

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<HashMap<String, String>> for MetadataStore

Source§

fn as_ref(&self) -> &HashMap<String, String>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for MetadataStore

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 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() -> Self

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<K, V> FromIterator<(K, V)> for MetadataStore
where K: Into<String>, V: Into<String>,

Source§

fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self

Creates a value from an iterator. Read more
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 Index<&str> for MetadataStore

Source§

type Output = String

The returned type after indexing.
Source§

fn index(&self, key: &str) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<&str> for MetadataStore

Source§

fn index_mut(&mut self, key: &str) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IntoIterator for MetadataStore

Source§

type Item = (String, String)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<String, String>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a MetadataStore

Source§

type Item = (&'a String, &'a String)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, String, String>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a mut MetadataStore

Source§

type Item = (&'a String, &'a mut String)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, String, String>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for MetadataStore

Source§

fn eq(&self, other: &Self) -> 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