pub struct FieldMap { /* private fields */ }Expand description
A wrapper around [http::HeaderMap] which implements wasi:http semantics.
The main differences from [http::HeaderMap] and this type are:
- A slimmed down mutability API to just what
wasi:httpneeds. FieldMapis cheaply clone-able with the internalHeaderMapbeing behind anArc.FieldMapis either immutable or mutable. Mutations on immutable values are rejected with an error. Mutations on mutable values will never panic unlikeHeaderMapand additionally require a limit to be set on the size of the map.
Overall the intention is that this is a slim wrapper around
[http::HeaderMap] with slightly different ownership, panic, and error
semantics.
Implementations§
Source§impl FieldMap
impl FieldMap
Sourcepub fn new_immutable(map: HeaderMap) -> Self
pub fn new_immutable(map: HeaderMap) -> Self
Creates a new immutable FieldMap from the provided
[http::HeaderMap].
The returned value cannot be mutated and attempting to mutate it will return an error.
Sourcepub fn new_mutable(limit: usize) -> Self
pub fn new_mutable(limit: usize) -> Self
Creates a new, empty, mutable FieldMap.
Mutations are allowed on the returned value and up to limit bytes of
memory (roughly) may be consumed by this map.
Sourcepub fn set(
&mut self,
key: HeaderName,
values: Vec<HeaderValue>,
) -> Result<(), FieldMapError>
pub fn set( &mut self, key: HeaderName, values: Vec<HeaderValue>, ) -> Result<(), FieldMapError>
Sets the header key to the values list provided.
Removes the previous value, if any.
If values is empty then this removes the header key.
Sourcepub fn remove_all(
&mut self,
key: HeaderName,
) -> Result<Vec<HeaderValue>, FieldMapError>
pub fn remove_all( &mut self, key: HeaderName, ) -> Result<Vec<HeaderValue>, FieldMapError>
Remove all values associated with a key in a map.
Returns an empty list if the key is not already present within the map.
Sourcepub fn append(
&mut self,
key: HeaderName,
value: HeaderValue,
) -> Result<bool, FieldMapError>
pub fn append( &mut self, key: HeaderName, value: HeaderValue, ) -> Result<bool, FieldMapError>
Add a value associated with a key to the map.
If key is already present within the map then value is appended to
the list of values it already has.
Sourcepub fn set_mutable(&mut self, limit: usize)
pub fn set_mutable(&mut self, limit: usize)
Flags this map as mutable, allowing mutations which can allocate as much
as limit memory, in bytes, for this entire map (roughly).
Sourcepub fn set_immutable(&mut self)
pub fn set_immutable(&mut self)
Flags this map as immutable, forbidding all further mutations.
Methods from Deref<Target = HeaderMap>§
pub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of headers 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 = HeaderMap::new();
assert_eq!(0, map.len());
map.insert(ACCEPT, "text/plain".parse().unwrap());
map.insert(HOST, "localhost".parse().unwrap());
assert_eq!(2, map.len());
map.append(ACCEPT, "text/html".parse().unwrap());
assert_eq!(3, map.len());pub fn keys_len(&self) -> usize
pub fn keys_len(&self) -> usize
Returns the number of keys 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 = HeaderMap::new();
assert_eq!(0, map.keys_len());
map.insert(ACCEPT, "text/plain".parse().unwrap());
map.insert(HOST, "localhost".parse().unwrap());
assert_eq!(2, map.keys_len());
map.insert(ACCEPT, "text/html".parse().unwrap());
assert_eq!(2, map.keys_len());pub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map contains no elements.
§Examples
let mut map = HeaderMap::new();
assert!(map.is_empty());
map.insert(HOST, "hello.world".parse().unwrap());
assert!(!map.is_empty());pub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of headers 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 = HeaderMap::new();
assert_eq!(0, map.capacity());
map.insert(HOST, "hello.world".parse().unwrap());
assert_eq!(6, map.capacity());pub fn get<K>(&self, key: K) -> Option<&T>where
K: AsHeaderName,
pub fn get<K>(&self, key: K) -> Option<&T>where
K: AsHeaderName,
Returns a reference to the value associated with the key.
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 = HeaderMap::new();
assert!(map.get("host").is_none());
map.insert(HOST, "hello".parse().unwrap());
assert_eq!(map.get(HOST).unwrap(), &"hello");
assert_eq!(map.get("host").unwrap(), &"hello");
map.append(HOST, "world".parse().unwrap());
assert_eq!(map.get("host").unwrap(), &"hello");pub fn get_all<K>(&self, key: K) -> GetAll<'_, T>where
K: AsHeaderName,
pub fn get_all<K>(&self, key: K) -> GetAll<'_, T>where
K: AsHeaderName,
Returns a view of all values associated with a key.
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 = HeaderMap::new();
map.insert(HOST, "hello".parse().unwrap());
map.append(HOST, "goodbye".parse().unwrap());
let view = map.get_all("host");
let mut iter = view.iter();
assert_eq!(&"hello", iter.next().unwrap());
assert_eq!(&"goodbye", iter.next().unwrap());
assert!(iter.next().is_none());pub fn contains_key<K>(&self, key: K) -> boolwhere
K: AsHeaderName,
pub fn contains_key<K>(&self, key: K) -> boolwhere
K: AsHeaderName,
Returns true if the map contains a value for the specified key.
§Examples
let mut map = HeaderMap::new();
assert!(!map.contains_key(HOST));
map.insert(HOST, "world".parse().unwrap());
assert!(map.contains_key("host"));pub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
An iterator visiting all key-value pairs.
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 = HeaderMap::new();
map.insert(HOST, "hello".parse().unwrap());
map.append(HOST, "goodbye".parse().unwrap());
map.insert(CONTENT_LENGTH, "123".parse().unwrap());
for (key, value) in map.iter() {
println!("{:?}: {:?}", key, value);
}pub fn keys(&self) -> Keys<'_, T>
pub fn keys(&self) -> Keys<'_, T>
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 = HeaderMap::new();
map.insert(HOST, "hello".parse().unwrap());
map.append(HOST, "goodbye".parse().unwrap());
map.insert(CONTENT_LENGTH, "123".parse().unwrap());
for key in map.keys() {
println!("{:?}", key);
}pub fn values(&self) -> Values<'_, T>
pub fn values(&self) -> Values<'_, T>
An iterator visiting all values.
The iteration order is arbitrary, but consistent across platforms for the same crate version.
§Examples
let mut map = HeaderMap::new();
map.insert(HOST, "hello".parse().unwrap());
map.append(HOST, "goodbye".parse().unwrap());
map.insert(CONTENT_LENGTH, "123".parse().unwrap());
for value in map.values() {
println!("{:?}", value);
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for FieldMap
impl RefUnwindSafe for FieldMap
impl Send for FieldMap
impl Sync for FieldMap
impl Unpin for FieldMap
impl UnwindSafe for FieldMap
Blanket Implementations§
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> GetSetFdFlags for T
impl<T> GetSetFdFlags for T
§fn get_fd_flags(&self) -> Result<FdFlags, Error>where
T: AsFilelike,
fn get_fd_flags(&self) -> Result<FdFlags, Error>where
T: AsFilelike,
self file descriptor.§fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>where
T: AsFilelike,
fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>where
T: AsFilelike,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more