pub struct Map { /* private fields */ }Expand description
A map of values.
Maps retain the order in which the entries were inserted and every key
is unique. Keys can be any value, not just strings. Additionally a
map holds the Order of its entries and if it’s a multimap (see
is_multimap) which are passed on to formats when
the map is serialized. Neither is considered when maps are compared:
maps are equal if they contain the same entries. The
exception are entries with maps or sequences as keys, which are compared
in order (this allows comparing maps without recursion).
use deser_value::{Map, Value};
let mut map = Map::new();
map.insert("name", "Jane");
map.insert(42, true);
assert_eq!(map.get("name"), Some(&Value::from("Jane")));
assert_eq!(map.get(&42), Some(&Value::from(true)));
assert_eq!(
map.keys().collect::<Vec<_>>(),
[&Value::from("name"), &Value::from(42)]
);Keys can be looked up by anything that implements MapKey, which
includes strings, integers and values.
Implementations§
Source§impl Map
impl Map
Sourcepub fn with_capacity(capacity: usize) -> Map
pub fn with_capacity(capacity: usize) -> Map
Creates an empty map with a capacity.
Sourcepub fn is_multimap(&self) -> bool
pub fn is_multimap(&self) -> bool
Returns true if the map is a multimap.
Maps that are deserialized from multimaps (like query strings, see
ContainerShape::set_multimap)
are multimaps. The values of a key that was given more than once
are a Seq marked as
repeated. When the map is deserialized
into another type, it’s a multimap again and the values of repeated
keys are passed on as the values of repeated keys. A key that was
given once is a single value, so types that collect the values of
keys (like Vec<T>) receive the same values as from the original
input.
use deser_value::{Map, Seq, Value};
#[derive(deser::Deserialize, Debug, PartialEq)]
struct Query {
tag: Vec<String>,
page: Vec<u32>,
}
let mut map = Map::new();
map.set_multimap(true);
let mut tags = Seq::from(vec![Value::from("a"), Value::from("b")]);
tags.set_repeated(true);
map.insert("tag", tags);
map.insert("page", 1);
let query: Query = deser_value::from_value(&Value::from(map)).unwrap();
assert_eq!(
query,
Query { tag: vec!["a".into(), "b".into()], page: vec![1] }
);Sourcepub fn set_multimap(&mut self, yes: bool)
pub fn set_multimap(&mut self, yes: bool)
Sets if the map is a multimap.
Sourcepub fn is_ambiguous_empty(&self) -> bool
pub fn is_ambiguous_empty(&self) -> bool
Returns true if the map is empty and could also be an empty
sequence.
This is the counterpart of
Seq::is_ambiguous_empty: types
that expect a sequence receive an empty sequence. The flag has no
effect once the map has entries.
Sourcepub fn set_ambiguous_empty(&mut self, yes: bool)
pub fn set_ambiguous_empty(&mut self, yes: bool)
Sets if the map could also be an empty sequence when it’s empty.
Sourcepub fn get_mut<K: MapKey + ?Sized>(&mut self, key: &K) -> Option<&mut Value>
pub fn get_mut<K: MapKey + ?Sized>(&mut self, key: &K) -> Option<&mut Value>
Returns the value of a key mutably.
Sourcepub fn get_key_value<K: MapKey + ?Sized>(
&self,
key: &K,
) -> Option<(&Value, &Value)>
pub fn get_key_value<K: MapKey + ?Sized>( &self, key: &K, ) -> Option<(&Value, &Value)>
Returns the key and value of a key.
This is useful to get to the meta data of the key.
Sourcepub fn get_index_of<K: MapKey + ?Sized>(&self, key: &K) -> Option<usize>
pub fn get_index_of<K: MapKey + ?Sized>(&self, key: &K) -> Option<usize>
Returns the index of a key.
Sourcepub fn get_index(&self, index: usize) -> Option<(&Value, &Value)>
pub fn get_index(&self, index: usize) -> Option<(&Value, &Value)>
Returns the key and value at an index.
Sourcepub fn get_index_mut(&mut self, index: usize) -> Option<(&Value, &mut Value)>
pub fn get_index_mut(&mut self, index: usize) -> Option<(&Value, &mut Value)>
Returns the key and the mutable value at an index.
Sourcepub fn contains_key<K: MapKey + ?Sized>(&self, key: &K) -> bool
pub fn contains_key<K: MapKey + ?Sized>(&self, key: &K) -> bool
Returns true if the map contains a key.
Sourcepub fn insert<K: Into<Value>, V: Into<Value>>(
&mut self,
key: K,
value: V,
) -> Option<Value>
pub fn insert<K: Into<Value>, V: Into<Value>>( &mut self, key: K, value: V, ) -> Option<Value>
Inserts a value for a key.
If the key already exists, its value is replaced (the entry keeps its position and key) and the old value is returned. Otherwise the entry is added at the end.
Sourcepub fn get_or_insert_with<K: Into<Value>, F: FnOnce() -> Value>(
&mut self,
key: K,
f: F,
) -> &mut Value
pub fn get_or_insert_with<K: Into<Value>, F: FnOnce() -> Value>( &mut self, key: K, f: F, ) -> &mut Value
Returns the value of a key, inserting the result of f if the key
does not exist.
Sourcepub fn remove<K: MapKey + ?Sized>(&mut self, key: &K) -> Option<Value>
pub fn remove<K: MapKey + ?Sized>(&mut self, key: &K) -> Option<Value>
Removes a key and returns its value.
The order of the remaining entries is retained.
Sourcepub fn remove_entry<K: MapKey + ?Sized>(
&mut self,
key: &K,
) -> Option<(Value, Value)>
pub fn remove_entry<K: MapKey + ?Sized>( &mut self, key: &K, ) -> Option<(Value, Value)>
Removes a key and returns it together with its value.
The order of the remaining entries is retained.
Sourcepub fn retain<F: FnMut(&Value, &mut Value) -> bool>(&mut self, f: F)
pub fn retain<F: FnMut(&Value, &mut Value) -> bool>(&mut self, f: F)
Retains the entries for which the predicate returns true.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_> ⓘ
Returns an iterator over the entries with mutable values.
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_> ⓘ
Returns an iterator over the mutable values.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Map
impl<'de> Deserialize<'de> for Map
Source§fn deserialize_update<'out>(
value: &'out mut Self,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_update<'out>( value: &'out mut Self, state: &mut State, ) -> SinkHandle<'out, 'de>
Merges the data into the map, the values of keys that exist are replaced.
Source§fn deserialize_into<'out>(
out: &'out mut Option<Self>,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_into<'out>( out: &'out mut Option<Self>, state: &mut State, ) -> SinkHandle<'out, 'de>
Source§fn expecting() -> Cow<'static, str>
fn expecting() -> Cow<'static, str>
Source§fn deserialize_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'_>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_atom( slot: &mut Slot<T, Self>, atom: Atom<'_>, state: &mut State, ) -> Result<(), Error>
Source§fn deserialize_borrowed_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_borrowed_atom( slot: &mut Slot<T, Self>, atom: Atom<'de>, state: &mut State, ) -> Result<(), Error>
Source§fn describe_type(d: &mut dyn Describe)
fn describe_type(d: &mut dyn Describe)
Source§fn initial_value() -> Option<T>
fn initial_value() -> Option<T>
impl Eq for Map
Source§impl<K: Into<Value>, V: Into<Value>> Extend<(K, V)> for Map
impl<K: Into<Value>, V: Into<Value>> Extend<(K, V)> for Map
Source§fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
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 IntoIterator for Map
impl IntoIterator for Map
Source§impl<'a> IntoIterator for &'a Map
impl<'a> IntoIterator for &'a Map
Source§impl<'a> IntoIterator for &'a mut Map
impl<'a> IntoIterator for &'a mut Map
Source§impl Serialize for Map
impl Serialize for Map
Source§fn serialize<'a>(this: &'a Self, state: &mut State) -> Result<Emit<'a>, Error>
fn serialize<'a>(this: &'a Self, state: &mut State) -> Result<Emit<'a>, Error>
Source§fn container_shape(value: &Self) -> ContainerShape
fn container_shape(value: &Self) -> ContainerShape
Auto Trait Implementations§
impl !RefUnwindSafe for Map
impl !UnwindSafe for Map
impl Freeze for Map
impl Send for Map
impl Sync for Map
impl Unpin for Map
impl UnsafeUnpin for Map
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> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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.