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
impl MetadataStore
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty MetadataStore.
§Examples
let metadata = MetadataStore::new();
assert_eq!(metadata.iter().count(), 0);Sourcepub fn insert<K, V>(&mut self, key: K, value: V) -> Result<(), MetadataError>
pub fn insert<K, V>(&mut self, key: K, value: V) -> Result<(), MetadataError>
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())
);
Sourcepub fn get(&self, key: &str) -> Option<&String>
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);
Sourcepub fn remove(&mut self, key: &str) -> Option<String>
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"));
Sourcepub fn contains_key(&self, key: &str) -> bool
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"));
Sourcepub fn iter(&self) -> impl Iterator<Item = (&String, &String)>
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);
Sourcepub fn into_inner(self) -> HashMap<String, String>
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())
);
Sourcepub fn extract_as<M: Metadata>(&self) -> Result<M, M::Error>
pub fn extract_as<M: Metadata>(&self) -> Result<M, M::Error>
Get a typed metadata entry.
Trait Implementations§
Source§impl Clone for MetadataStore
impl Clone for MetadataStore
Source§fn clone(&self) -> MetadataStore
fn clone(&self) -> MetadataStore
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for MetadataStore
impl Debug for MetadataStore
Source§impl Default for MetadataStore
impl Default for MetadataStore
Source§fn default() -> MetadataStore
fn default() -> MetadataStore
Source§impl<'de> Deserialize<'de> for MetadataStore
Available on crate feature serde only.
impl<'de> Deserialize<'de> for MetadataStore
serde only.Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for MetadataStore
Source§impl<Args: Send + Sync> FromRequest<Task<Args>> for MetadataStore
impl<Args: Send + Sync> FromRequest<Task<Args>> for MetadataStore
Source§impl PartialEq for MetadataStore
impl PartialEq for MetadataStore
Source§impl Serialize for MetadataStore
Available on crate feature serde only.
impl Serialize for MetadataStore
serde only.