Struct tonic::metadata::MetadataMap
source · [−]pub struct MetadataMap { /* private fields */ }
Expand description
A set of gRPC custom metadata entries.
Examples
Basic usage
let mut map = MetadataMap::new();
map.insert("x-host", "example.com".parse().unwrap());
map.insert("x-number", "123".parse().unwrap());
map.insert_bin("trace-proto-bin", MetadataValue::from_bytes(b"[binary data]"));
assert!(map.contains_key("x-host"));
assert!(!map.contains_key("x-location"));
assert_eq!(map.get("x-host").unwrap(), "example.com");
map.remove("x-host");
assert!(!map.contains_key("x-host"));
Implementations
sourceimpl MetadataMap
impl MetadataMap
sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty MetadataMap
.
The map will be created without any capacity. This function will not allocate.
Examples
let map = MetadataMap::new();
assert!(map.is_empty());
assert_eq!(0, map.capacity());
sourcepub fn from_headers(headers: HeaderMap) -> Self
pub fn from_headers(headers: HeaderMap) -> Self
Convert an HTTP HeaderMap to a MetadataMap
sourcepub fn into_headers(self) -> HeaderMap
pub fn into_headers(self) -> HeaderMap
Convert a MetadataMap into a HTTP HeaderMap
Examples
let mut map = MetadataMap::new();
map.insert("x-host", "example.com".parse().unwrap());
let http_map = map.into_headers();
assert_eq!(http_map.get("x-host").unwrap(), "example.com");
sourcepub fn with_capacity(capacity: usize) -> MetadataMap
pub fn with_capacity(capacity: usize) -> MetadataMap
Create an empty MetadataMap
with the specified capacity.
The returned map will allocate internal storage in order to hold about
capacity
elements without reallocating. However, this is a “best
effort” as there are usage patterns that could cause additional
allocations before capacity
metadata entries are stored in the map.
More capacity than requested may be allocated.
Examples
let map: MetadataMap = MetadataMap::with_capacity(10);
assert!(map.is_empty());
assert!(map.capacity() >= 10);
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of metadata entries (ascii and binary) stored in the map.
This number represents the total number of values stored in the map. This number can be greater than or equal to the number of keys stored given that a single key may have more than one associated value.
Examples
let mut map = MetadataMap::new();
assert_eq!(0, map.len());
map.insert("x-host-ip", "127.0.0.1".parse().unwrap());
map.insert_bin("x-host-name-bin", MetadataValue::from_bytes(b"localhost"));
assert_eq!(2, map.len());
map.append("x-host-ip", "text/html".parse().unwrap());
assert_eq!(3, map.len());
sourcepub fn keys_len(&self) -> usize
pub fn keys_len(&self) -> usize
Returns the number of keys (ascii and binary) stored in the map.
This number will be less than or equal to len()
as each key may have
more than one associated value.
Examples
let mut map = MetadataMap::new();
assert_eq!(0, map.keys_len());
map.insert("x-host-ip", "127.0.0.1".parse().unwrap());
map.insert_bin("x-host-name-bin", MetadataValue::from_bytes(b"localhost"));
assert_eq!(2, map.keys_len());
map.append("x-host-ip", "text/html".parse().unwrap());
assert_eq!(2, map.keys_len());
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map contains no elements.
Examples
let mut map = MetadataMap::new();
assert!(map.is_empty());
map.insert("x-host", "hello.world".parse().unwrap());
assert!(!map.is_empty());
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
Examples
let mut map = MetadataMap::new();
map.insert("x-host", "hello.world".parse().unwrap());
map.clear();
assert!(map.is_empty());
assert!(map.capacity() > 0);
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of custom metadata entries the map can hold without reallocating.
This number is an approximation as certain usage patterns could cause additional allocations before the returned capacity is filled.
Examples
let mut map = MetadataMap::new();
assert_eq!(0, map.capacity());
map.insert("x-host", "hello.world".parse().unwrap());
assert_eq!(6, map.capacity());
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more custom metadata to be
inserted into the MetadataMap
.
The metadata map may reserve more space to avoid frequent reallocations.
Like with with_capacity
, this will be a “best effort” to avoid
allocations until additional
more custom metadata is inserted. Certain
usage patterns could cause additional allocations before the number is
reached.
Panics
Panics if the new allocation size overflows usize
.
Examples
let mut map = MetadataMap::new();
map.reserve(10);
sourcepub fn get<K>(&self, key: K) -> Option<&MetadataValue<Ascii>>where
K: AsMetadataKey<Ascii>,
pub fn get<K>(&self, key: K) -> Option<&MetadataValue<Ascii>>where
K: AsMetadataKey<Ascii>,
Returns a reference to the value associated with the key. This method is for ascii metadata entries (those whose names don’t end with “-bin”). For binary entries, use get_bin.
If there are multiple values associated with the key, then the first one
is returned. Use get_all
to get all values associated with a given
key. Returns None
if there are no values associated with the key.
Examples
let mut map = MetadataMap::new();
assert!(map.get("x-host").is_none());
map.insert("x-host", "hello".parse().unwrap());
assert_eq!(map.get("x-host").unwrap(), &"hello");
assert_eq!(map.get("x-host").unwrap(), &"hello");
map.append("x-host", "world".parse().unwrap());
assert_eq!(map.get("x-host").unwrap(), &"hello");
// Attempting to read a key of the wrong type fails by not
// finding anything.
map.append_bin("host-bin", MetadataValue::from_bytes(b"world"));
assert!(map.get("host-bin").is_none());
assert!(map.get("host-bin".to_string()).is_none());
assert!(map.get(&("host-bin".to_string())).is_none());
// Attempting to read an invalid key string fails by not
// finding anything.
assert!(map.get("host{}bin").is_none());
assert!(map.get("host{}bin".to_string()).is_none());
assert!(map.get(&("host{}bin".to_string())).is_none());
sourcepub fn get_bin<K>(&self, key: K) -> Option<&MetadataValue<Binary>>where
K: AsMetadataKey<Binary>,
pub fn get_bin<K>(&self, key: K) -> Option<&MetadataValue<Binary>>where
K: AsMetadataKey<Binary>,
Like get, but for Binary keys (for example “trace-proto-bin”).
Examples
let mut map = MetadataMap::new();
assert!(map.get_bin("trace-proto-bin").is_none());
map.insert_bin("trace-proto-bin", MetadataValue::from_bytes(b"hello"));
assert_eq!(map.get_bin("trace-proto-bin").unwrap(), &"hello");
assert_eq!(map.get_bin("trace-proto-bin").unwrap(), &"hello");
map.append_bin("trace-proto-bin", MetadataValue::from_bytes(b"world"));
assert_eq!(map.get_bin("trace-proto-bin").unwrap(), &"hello");
// Attempting to read a key of the wrong type fails by not
// finding anything.
map.append("host", "world".parse().unwrap());
assert!(map.get_bin("host").is_none());
assert!(map.get_bin("host".to_string()).is_none());
assert!(map.get_bin(&("host".to_string())).is_none());
// Attempting to read an invalid key string fails by not
// finding anything.
assert!(map.get_bin("host{}-bin").is_none());
assert!(map.get_bin("host{}-bin".to_string()).is_none());
assert!(map.get_bin(&("host{}-bin".to_string())).is_none());
sourcepub fn get_mut<K>(&mut self, key: K) -> Option<&mut MetadataValue<Ascii>>where
K: AsMetadataKey<Ascii>,
pub fn get_mut<K>(&mut self, key: K) -> Option<&mut MetadataValue<Ascii>>where
K: AsMetadataKey<Ascii>,
Returns a mutable reference to the value associated with the key. This method is for ascii metadata entries (those whose names don’t end with “-bin”). For binary entries, use get_mut_bin.
If there are multiple values associated with the key, then the first one
is returned. Use entry
to get all values associated with a given
key. Returns None
if there are no values associated with the key.
Examples
let mut map = MetadataMap::default();
map.insert("x-host", "hello".parse().unwrap());
map.get_mut("x-host").unwrap().set_sensitive(true);
assert!(map.get("x-host").unwrap().is_sensitive());
// Attempting to read a key of the wrong type fails by not
// finding anything.
map.append_bin("host-bin", MetadataValue::from_bytes(b"world"));
assert!(map.get_mut("host-bin").is_none());
assert!(map.get_mut("host-bin".to_string()).is_none());
assert!(map.get_mut(&("host-bin".to_string())).is_none());
// Attempting to read an invalid key string fails by not
// finding anything.
assert!(map.get_mut("host{}").is_none());
assert!(map.get_mut("host{}".to_string()).is_none());
assert!(map.get_mut(&("host{}".to_string())).is_none());
sourcepub fn get_bin_mut<K>(&mut self, key: K) -> Option<&mut MetadataValue<Binary>>where
K: AsMetadataKey<Binary>,
pub fn get_bin_mut<K>(&mut self, key: K) -> Option<&mut MetadataValue<Binary>>where
K: AsMetadataKey<Binary>,
Like get_mut, but for Binary keys (for example “trace-proto-bin”).
Examples
let mut map = MetadataMap::default();
map.insert_bin("trace-proto-bin", MetadataValue::from_bytes(b"hello"));
map.get_bin_mut("trace-proto-bin").unwrap().set_sensitive(true);
assert!(map.get_bin("trace-proto-bin").unwrap().is_sensitive());
// Attempting to read a key of the wrong type fails by not
// finding anything.
map.append("host", "world".parse().unwrap());
assert!(map.get_bin_mut("host").is_none());
assert!(map.get_bin_mut("host".to_string()).is_none());
assert!(map.get_bin_mut(&("host".to_string())).is_none());
// Attempting to read an invalid key string fails by not
// finding anything.
assert!(map.get_bin_mut("host{}-bin").is_none());
assert!(map.get_bin_mut("host{}-bin".to_string()).is_none());
assert!(map.get_bin_mut(&("host{}-bin".to_string())).is_none());
sourcepub fn get_all<K>(&self, key: K) -> GetAll<'_, Ascii>where
K: AsMetadataKey<Ascii>,
pub fn get_all<K>(&self, key: K) -> GetAll<'_, Ascii>where
K: AsMetadataKey<Ascii>,
Returns a view of all values associated with a key. This method is for ascii metadata entries (those whose names don’t end with “-bin”). For binary entries, use get_all_bin.
The returned view does not incur any allocations and allows iterating
the values associated with the key. See GetAll
for more details.
Returns None
if there are no values associated with the key.
Examples
let mut map = MetadataMap::new();
map.insert("x-host", "hello".parse().unwrap());
map.append("x-host", "goodbye".parse().unwrap());
{
let view = map.get_all("x-host");
let mut iter = view.iter();
assert_eq!(&"hello", iter.next().unwrap());
assert_eq!(&"goodbye", iter.next().unwrap());
assert!(iter.next().is_none());
}
// Attempting to read a key of the wrong type fails by not
// finding anything.
map.append_bin("host-bin", MetadataValue::from_bytes(b"world"));
assert!(map.get_all("host-bin").iter().next().is_none());
assert!(map.get_all("host-bin".to_string()).iter().next().is_none());
assert!(map.get_all(&("host-bin".to_string())).iter().next().is_none());
// Attempting to read an invalid key string fails by not
// finding anything.
assert!(map.get_all("host{}").iter().next().is_none());
assert!(map.get_all("host{}".to_string()).iter().next().is_none());
assert!(map.get_all(&("host{}".to_string())).iter().next().is_none());
sourcepub fn get_all_bin<K>(&self, key: K) -> GetAll<'_, Binary>where
K: AsMetadataKey<Binary>,
pub fn get_all_bin<K>(&self, key: K) -> GetAll<'_, Binary>where
K: AsMetadataKey<Binary>,
Like get_all, but for Binary keys (for example “trace-proto-bin”).
Examples
let mut map = MetadataMap::new();
map.insert_bin("trace-proto-bin", MetadataValue::from_bytes(b"hello"));
map.append_bin("trace-proto-bin", MetadataValue::from_bytes(b"goodbye"));
{
let view = map.get_all_bin("trace-proto-bin");
let mut iter = view.iter();
assert_eq!(&"hello", iter.next().unwrap());
assert_eq!(&"goodbye", iter.next().unwrap());
assert!(iter.next().is_none());
}
// Attempting to read a key of the wrong type fails by not
// finding anything.
map.append("host", "world".parse().unwrap());
assert!(map.get_all_bin("host").iter().next().is_none());
assert!(map.get_all_bin("host".to_string()).iter().next().is_none());
assert!(map.get_all_bin(&("host".to_string())).iter().next().is_none());
// Attempting to read an invalid key string fails by not
// finding anything.
assert!(map.get_all_bin("host{}-bin").iter().next().is_none());
assert!(map.get_all_bin("host{}-bin".to_string()).iter().next().is_none());
assert!(map.get_all_bin(&("host{}-bin".to_string())).iter().next().is_none());
sourcepub fn contains_key<K>(&self, key: K) -> boolwhere
K: AsEncodingAgnosticMetadataKey,
pub fn contains_key<K>(&self, key: K) -> boolwhere
K: AsEncodingAgnosticMetadataKey,
Returns true if the map contains a value for the specified key. This method works for both ascii and binary entries.
Examples
let mut map = MetadataMap::new();
assert!(!map.contains_key("x-host"));
map.append_bin("host-bin", MetadataValue::from_bytes(b"world"));
map.insert("x-host", "world".parse().unwrap());
// contains_key works for both Binary and Ascii keys:
assert!(map.contains_key("x-host"));
assert!(map.contains_key("host-bin"));
// contains_key returns false for invalid keys:
assert!(!map.contains_key("x{}host"));
sourcepub fn iter(&self) -> Iter<'_>ⓘNotable traits for Iter<'a>impl<'a> Iterator for Iter<'a> type Item = KeyAndValueRef<'a>;
pub fn iter(&self) -> Iter<'_>ⓘNotable traits for Iter<'a>impl<'a> Iterator for Iter<'a> type Item = KeyAndValueRef<'a>;
An iterator visiting all key-value pairs (both ascii and binary).
The iteration order is arbitrary, but consistent across platforms for the same crate version. Each key will be yielded once per associated value. So, if a key has 3 associated values, it will be yielded 3 times.
Examples
let mut map = MetadataMap::new();
map.insert("x-word", "hello".parse().unwrap());
map.append("x-word", "goodbye".parse().unwrap());
map.insert("x-number", "123".parse().unwrap());
for key_and_value in map.iter() {
match key_and_value {
KeyAndValueRef::Ascii(ref key, ref value) =>
println!("Ascii: {:?}: {:?}", key, value),
KeyAndValueRef::Binary(ref key, ref value) =>
println!("Binary: {:?}: {:?}", key, value),
}
}
sourcepub fn iter_mut(&mut self) -> IterMut<'_>ⓘNotable traits for IterMut<'a>impl<'a> Iterator for IterMut<'a> type Item = KeyAndMutValueRef<'a>;
pub fn iter_mut(&mut self) -> IterMut<'_>ⓘNotable traits for IterMut<'a>impl<'a> Iterator for IterMut<'a> type Item = KeyAndMutValueRef<'a>;
An iterator visiting all key-value pairs, with mutable value references.
The iterator order is arbitrary, but consistent across platforms for the same crate version. Each key will be yielded once per associated value, so if a key has 3 associated values, it will be yielded 3 times.
Examples
let mut map = MetadataMap::new();
map.insert("x-word", "hello".parse().unwrap());
map.append("x-word", "goodbye".parse().unwrap());
map.insert("x-number", "123".parse().unwrap());
for key_and_value in map.iter_mut() {
match key_and_value {
KeyAndMutValueRef::Ascii(key, mut value) =>
value.set_sensitive(true),
KeyAndMutValueRef::Binary(key, mut value) =>
value.set_sensitive(false),
}
}
sourcepub fn keys(&self) -> Keys<'_>ⓘNotable traits for Keys<'a>impl<'a> Iterator for Keys<'a> type Item = KeyRef<'a>;
pub fn keys(&self) -> Keys<'_>ⓘNotable traits for Keys<'a>impl<'a> Iterator for Keys<'a> type Item = KeyRef<'a>;
An iterator visiting all keys.
The iteration order is arbitrary, but consistent across platforms for the same crate version. Each key will be yielded only once even if it has multiple associated values.
Examples
let mut map = MetadataMap::new();
map.insert("x-word", "hello".parse().unwrap());
map.append("x-word", "goodbye".parse().unwrap());
map.insert_bin("x-number-bin", MetadataValue::from_bytes(b"123"));
for key in map.keys() {
match key {
KeyRef::Ascii(ref key) =>
println!("Ascii key: {:?}", key),
KeyRef::Binary(ref key) =>
println!("Binary key: {:?}", key),
}
println!("{:?}", key);
}
sourcepub fn values(&self) -> Values<'_>ⓘNotable traits for Values<'a>impl<'a> Iterator for Values<'a> type Item = ValueRef<'a>;
pub fn values(&self) -> Values<'_>ⓘNotable traits for Values<'a>impl<'a> Iterator for Values<'a> type Item = ValueRef<'a>;
An iterator visiting all values (both ascii and binary).
The iteration order is arbitrary, but consistent across platforms for the same crate version.
Examples
let mut map = MetadataMap::new();
map.insert("x-word", "hello".parse().unwrap());
map.append("x-word", "goodbye".parse().unwrap());
map.insert_bin("x-number-bin", MetadataValue::from_bytes(b"123"));
for value in map.values() {
match value {
ValueRef::Ascii(ref value) =>
println!("Ascii value: {:?}", value),
ValueRef::Binary(ref value) =>
println!("Binary value: {:?}", value),
}
println!("{:?}", value);
}
sourcepub fn values_mut(&mut self) -> ValuesMut<'_>ⓘNotable traits for ValuesMut<'a>impl<'a> Iterator for ValuesMut<'a> type Item = ValueRefMut<'a>;
pub fn values_mut(&mut self) -> ValuesMut<'_>ⓘNotable traits for ValuesMut<'a>impl<'a> Iterator for ValuesMut<'a> type Item = ValueRefMut<'a>;
An iterator visiting all values mutably.
The iteration order is arbitrary, but consistent across platforms for the same crate version.
Examples
let mut map = MetadataMap::default();
map.insert("x-word", "hello".parse().unwrap());
map.append("x-word", "goodbye".parse().unwrap());
map.insert("x-number", "123".parse().unwrap());
for value in map.values_mut() {
match value {
ValueRefMut::Ascii(mut value) =>
value.set_sensitive(true),
ValueRefMut::Binary(mut value) =>
value.set_sensitive(false),
}
}
sourcepub fn entry<K>(
&mut self,
key: K
) -> Result<Entry<'_, Ascii>, InvalidMetadataKey>where
K: AsMetadataKey<Ascii>,
pub fn entry<K>(
&mut self,
key: K
) -> Result<Entry<'_, Ascii>, InvalidMetadataKey>where
K: AsMetadataKey<Ascii>,
Gets the given ascii key’s corresponding entry in the map for in-place
manipulation. For binary keys, use entry_bin
.
Examples
let mut map = MetadataMap::default();
let headers = &[
"content-length",
"x-hello",
"Content-Length",
"x-world",
];
for &header in headers {
let counter = map.entry(header).unwrap().or_insert("".parse().unwrap());
*counter = format!("{}{}", counter.to_str().unwrap(), "1").parse().unwrap();
}
assert_eq!(map.get("content-length").unwrap(), "11");
assert_eq!(map.get("x-hello").unwrap(), "1");
// Gracefully handles parting invalid key strings
assert!(!map.entry("a{}b").is_ok());
// Attempting to read a key of the wrong type fails by not
// finding anything.
map.append_bin("host-bin", MetadataValue::from_bytes(b"world"));
assert!(!map.entry("host-bin").is_ok());
assert!(!map.entry("host-bin".to_string()).is_ok());
assert!(!map.entry(&("host-bin".to_string())).is_ok());
// Attempting to read an invalid key string fails by not
// finding anything.
assert!(!map.entry("host{}").is_ok());
assert!(!map.entry("host{}".to_string()).is_ok());
assert!(!map.entry(&("host{}".to_string())).is_ok());
sourcepub fn entry_bin<K>(
&mut self,
key: K
) -> Result<Entry<'_, Binary>, InvalidMetadataKey>where
K: AsMetadataKey<Binary>,
pub fn entry_bin<K>(
&mut self,
key: K
) -> Result<Entry<'_, Binary>, InvalidMetadataKey>where
K: AsMetadataKey<Binary>,
Gets the given Binary key’s corresponding entry in the map for in-place manipulation.
Examples
let mut map = MetadataMap::default();
let headers = &[
"content-length-bin",
"x-hello-bin",
"Content-Length-bin",
"x-world-bin",
];
for &header in headers {
let counter = map.entry_bin(header).unwrap().or_insert(MetadataValue::from_bytes(b""));
*counter = MetadataValue::from_bytes(format!("{}{}", str::from_utf8(counter.to_bytes().unwrap().as_ref()).unwrap(), "1").as_bytes());
}
assert_eq!(map.get_bin("content-length-bin").unwrap(), "11");
assert_eq!(map.get_bin("x-hello-bin").unwrap(), "1");
// Attempting to read a key of the wrong type fails by not
// finding anything.
map.append("host", "world".parse().unwrap());
assert!(!map.entry_bin("host").is_ok());
assert!(!map.entry_bin("host".to_string()).is_ok());
assert!(!map.entry_bin(&("host".to_string())).is_ok());
// Attempting to read an invalid key string fails by not
// finding anything.
assert!(!map.entry_bin("host{}-bin").is_ok());
assert!(!map.entry_bin("host{}-bin".to_string()).is_ok());
assert!(!map.entry_bin(&("host{}-bin".to_string())).is_ok());
sourcepub fn insert<K>(
&mut self,
key: K,
val: MetadataValue<Ascii>
) -> Option<MetadataValue<Ascii>>where
K: IntoMetadataKey<Ascii>,
pub fn insert<K>(
&mut self,
key: K,
val: MetadataValue<Ascii>
) -> Option<MetadataValue<Ascii>>where
K: IntoMetadataKey<Ascii>,
Inserts an ascii key-value pair into the map. To insert a binary entry,
use insert_bin
.
This method panics when the given key is a string and it cannot be
converted to a MetadataKey
If the map did not previously have this key present, then None
is
returned.
If the map did have this key present, the new value is associated with
the key and all previous values are removed. Note that only a single
one of the previous values is returned. If there are multiple values
that have been previously associated with the key, then the first one is
returned. See insert_mult
on OccupiedEntry
for an API that returns
all values.
The key is not updated, though; this matters for types that can be ==
without being identical.
Examples
let mut map = MetadataMap::new();
assert!(map.insert("x-host", "world".parse().unwrap()).is_none());
assert!(!map.is_empty());
let mut prev = map.insert("x-host", "earth".parse().unwrap()).unwrap();
assert_eq!("world", prev);
let mut map = MetadataMap::new();
// Trying to insert a key that is not valid panics.
map.insert("x{}host", "world".parse().unwrap());
let mut map = MetadataMap::new();
// Trying to insert a key that is binary panics (use insert_bin).
map.insert("x-host-bin", "world".parse().unwrap());
sourcepub fn insert_bin<K>(
&mut self,
key: K,
val: MetadataValue<Binary>
) -> Option<MetadataValue<Binary>>where
K: IntoMetadataKey<Binary>,
pub fn insert_bin<K>(
&mut self,
key: K,
val: MetadataValue<Binary>
) -> Option<MetadataValue<Binary>>where
K: IntoMetadataKey<Binary>,
Like insert, but for Binary keys (for example “trace-proto-bin”).
This method panics when the given key is a string and it cannot be
converted to a MetadataKey
Examples
let mut map = MetadataMap::new();
assert!(map.insert_bin("trace-proto-bin", MetadataValue::from_bytes(b"world")).is_none());
assert!(!map.is_empty());
let mut prev = map.insert_bin("trace-proto-bin", MetadataValue::from_bytes(b"earth")).unwrap();
assert_eq!("world", prev);
let mut map = MetadataMap::default();
// Attempting to add a binary metadata entry with an invalid name
map.insert_bin("trace-proto", MetadataValue::from_bytes(b"hello")); // This line panics!
let mut map = MetadataMap::new();
// Trying to insert a key that is not valid panics.
map.insert_bin("x{}host-bin", MetadataValue::from_bytes(b"world")); // This line panics!
sourcepub fn append<K>(&mut self, key: K, value: MetadataValue<Ascii>) -> boolwhere
K: IntoMetadataKey<Ascii>,
pub fn append<K>(&mut self, key: K, value: MetadataValue<Ascii>) -> boolwhere
K: IntoMetadataKey<Ascii>,
Inserts an ascii key-value pair into the map. To insert a binary entry,
use append_bin
.
This method panics when the given key is a string and it cannot be
converted to a MetadataKey
If the map did not previously have this key present, then false
is
returned.
If the map did have this key present, the new value is pushed to the end
of the list of values currently associated with the key. The key is not
updated, though; this matters for types that can be ==
without being
identical.
Examples
let mut map = MetadataMap::new();
assert!(map.insert("x-host", "world".parse().unwrap()).is_none());
assert!(!map.is_empty());
map.append("x-host", "earth".parse().unwrap());
let values = map.get_all("x-host");
let mut i = values.iter();
assert_eq!("world", *i.next().unwrap());
assert_eq!("earth", *i.next().unwrap());
let mut map = MetadataMap::new();
// Trying to append a key that is not valid panics.
map.append("x{}host", "world".parse().unwrap()); // This line panics!
let mut map = MetadataMap::new();
// Trying to append a key that is binary panics (use append_bin).
map.append("x-host-bin", "world".parse().unwrap()); // This line panics!
sourcepub fn append_bin<K>(&mut self, key: K, value: MetadataValue<Binary>) -> boolwhere
K: IntoMetadataKey<Binary>,
pub fn append_bin<K>(&mut self, key: K, value: MetadataValue<Binary>) -> boolwhere
K: IntoMetadataKey<Binary>,
Like append, but for binary keys (for example “trace-proto-bin”).
This method panics when the given key is a string and it cannot be
converted to a MetadataKey
Examples
let mut map = MetadataMap::new();
assert!(map.insert_bin("trace-proto-bin", MetadataValue::from_bytes(b"world")).is_none());
assert!(!map.is_empty());
map.append_bin("trace-proto-bin", MetadataValue::from_bytes(b"earth"));
let values = map.get_all_bin("trace-proto-bin");
let mut i = values.iter();
assert_eq!("world", *i.next().unwrap());
assert_eq!("earth", *i.next().unwrap());
let mut map = MetadataMap::new();
// Trying to append a key that is not valid panics.
map.append_bin("x{}host-bin", MetadataValue::from_bytes(b"world")); // This line panics!
let mut map = MetadataMap::new();
// Trying to append a key that is ascii panics (use append).
map.append_bin("x-host", MetadataValue::from_bytes(b"world")); // This line panics!
sourcepub fn remove<K>(&mut self, key: K) -> Option<MetadataValue<Ascii>>where
K: AsMetadataKey<Ascii>,
pub fn remove<K>(&mut self, key: K) -> Option<MetadataValue<Ascii>>where
K: AsMetadataKey<Ascii>,
Removes an ascii key from the map, returning the value associated with
the key. To remove a binary key, use remove_bin
.
Returns None
if the map does not contain the key. If there are
multiple values associated with the key, then the first one is returned.
See remove_entry_mult
on OccupiedEntry
for an API that yields all
values.
Examples
let mut map = MetadataMap::new();
map.insert("x-host", "hello.world".parse().unwrap());
let prev = map.remove("x-host").unwrap();
assert_eq!("hello.world", prev);
assert!(map.remove("x-host").is_none());
// Attempting to remove a key of the wrong type fails by not
// finding anything.
map.append_bin("host-bin", MetadataValue::from_bytes(b"world"));
assert!(map.remove("host-bin").is_none());
assert!(map.remove("host-bin".to_string()).is_none());
assert!(map.remove(&("host-bin".to_string())).is_none());
// Attempting to remove an invalid key string fails by not
// finding anything.
assert!(map.remove("host{}").is_none());
assert!(map.remove("host{}".to_string()).is_none());
assert!(map.remove(&("host{}".to_string())).is_none());
sourcepub fn remove_bin<K>(&mut self, key: K) -> Option<MetadataValue<Binary>>where
K: AsMetadataKey<Binary>,
pub fn remove_bin<K>(&mut self, key: K) -> Option<MetadataValue<Binary>>where
K: AsMetadataKey<Binary>,
Like remove, but for Binary keys (for example “trace-proto-bin”).
Examples
let mut map = MetadataMap::new();
map.insert_bin("trace-proto-bin", MetadataValue::from_bytes(b"hello.world"));
let prev = map.remove_bin("trace-proto-bin").unwrap();
assert_eq!("hello.world", prev);
assert!(map.remove_bin("trace-proto-bin").is_none());
// Attempting to remove a key of the wrong type fails by not
// finding anything.
map.append("host", "world".parse().unwrap());
assert!(map.remove_bin("host").is_none());
assert!(map.remove_bin("host".to_string()).is_none());
assert!(map.remove_bin(&("host".to_string())).is_none());
// Attempting to remove an invalid key string fails by not
// finding anything.
assert!(map.remove_bin("host{}-bin").is_none());
assert!(map.remove_bin("host{}-bin".to_string()).is_none());
assert!(map.remove_bin(&("host{}-bin".to_string())).is_none());
Trait Implementations
sourceimpl Clone for MetadataMap
impl Clone for MetadataMap
sourcefn clone(&self) -> MetadataMap
fn clone(&self) -> MetadataMap
1.0.0const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read moresourceimpl Debug for MetadataMap
impl Debug for MetadataMap
sourceimpl Default for MetadataMap
impl Default for MetadataMap
sourcefn default() -> MetadataMap
fn default() -> MetadataMap
Auto Trait Implementations
impl RefUnwindSafe for MetadataMap
impl Send for MetadataMap
impl Sync for MetadataMap
impl Unpin for MetadataMap
impl UnwindSafe for MetadataMap
Blanket Implementations
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstablefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
fn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourcefn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
fn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
fn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourcefn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
fn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
impl<T> ToOwned for Twhere
T: Clone,
impl<T> ToOwned for Twhere
T: Clone,
type Owned = T
type Owned = T
fn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
T: Future, type Output = <T as Future>::Output;
sourcefn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
fn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
T: Future, type Output = <T as Future>::Output;
sourcefn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
fn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;