pub struct Metadata(/* private fields */);Expand description
A cheaply cloneable map of key-value metadata, used by
Field and Schema.
Cloning a Metadata is always cheap, as the underlying map is
reference-counted. Mutating a shared Metadata (e.g. via
Metadata::insert) will clone the underlying map if (and only if)
it is shared (copy-on-write).
The entries are stored in a BTreeMap, so iteration order is
deterministic (sorted by key).
§Example
let mut metadata = Metadata::new();
metadata.insert("key", "value");
let clone = metadata.clone(); // cheap
assert_eq!(clone.get("key"), Some(&"value".to_string()));
// Mutating one does not affect the other:
metadata.insert("key2", "value2");
assert_eq!(metadata.len(), 2);
assert_eq!(clone.len(), 1);Implementations§
Source§impl Metadata
impl Metadata
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 key.
Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Returns true if the map contains a value for the specified key.
Sourcepub fn insert(
&mut self,
key: impl Into<String>,
value: impl Into<String>,
) -> Option<String>
pub fn insert( &mut self, key: impl Into<String>, value: impl Into<String>, ) -> Option<String>
Inserts a key-value pair, returning the old value of the key, if any.
If the map already contained this key, the value is replaced.
Clones the underlying map if (and only if) it is shared.
Sourcepub fn with(self, key: impl Into<String>, value: impl Into<String>) -> Metadata
pub fn with(self, key: impl Into<String>, value: impl Into<String>) -> Metadata
Inserts a key-value pair and returns self, for builder-style chaining.
If the map already contained this key, the value is replaced.
§Example
let metadata = Metadata::new().with("a", "1").with("b", "2");
assert_eq!(metadata.len(), 2);Sourcepub fn remove(&mut self, key: &str) -> Option<String>
pub fn remove(&mut self, key: &str) -> Option<String>
Removes a key from the map, returning the value at the key if the key was previously in the map.
Clones the underlying map if (and only if) it is shared and contains the key.
Sourcepub fn as_arc(&self) -> Option<&Arc<BTreeMap<String, String>>>
pub fn as_arc(&self) -> Option<&Arc<BTreeMap<String, String>>>
Returns a reference to the underlying Arc, or None if the map is empty.
Useful for pointer-based deduplication (e.g. memory accounting) where callers need to detect clones that share the same allocation.
Sourcepub fn into_arc(self) -> Option<Arc<BTreeMap<String, String>>>
pub fn into_arc(self) -> Option<Arc<BTreeMap<String, String>>>
Consumes self and returns the underlying Arc, or None if the map is empty.
Useful for zero-copy conversion into wrapper types that also store an
Arc<BTreeMap<String, String>> internally.
Sourcepub fn iter(&self) -> Flatten<IntoIter<Iter<'_, String, String>>> ⓘ
pub fn iter(&self) -> Flatten<IntoIter<Iter<'_, String, String>>> ⓘ
Returns an iterator over the entries, sorted by key.
Sourcepub fn keys(&self) -> impl Iterator<Item = &String>
pub fn keys(&self) -> impl Iterator<Item = &String>
Returns an iterator over the keys, in sorted order.
Sourcepub fn values(&self) -> impl Iterator<Item = &String>
pub fn values(&self) -> impl Iterator<Item = &String>
Returns an iterator over the values, sorted by key.
Trait Implementations§
impl Eq for Metadata
Source§impl<K, V> Extend<(K, V)> for Metadata
impl<K, V> Extend<(K, V)> for Metadata
Source§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = (K, V)>,
fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = (K, V)>,
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K, V> FromIterator<(K, V)> for Metadata
impl<K, V> FromIterator<(K, V)> for Metadata
Source§impl<'a> IntoIterator for &'a Metadata
impl<'a> IntoIterator for &'a Metadata
Source§impl IntoIterator for Metadata
impl IntoIterator for Metadata
Source§impl Ord for Metadata
impl Ord for Metadata
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Metadata
impl PartialOrd for Metadata
impl StructuralPartialEq for Metadata
Auto Trait Implementations§
impl Freeze for Metadata
impl RefUnwindSafe for Metadata
impl Send for Metadata
impl Sync for Metadata
impl Unpin for Metadata
impl UnsafeUnpin for Metadata
impl UnwindSafe for Metadata
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.