Skip to main content

Map

Struct Map 

Source
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

Source

pub fn new() -> Map

Creates an empty map.

Source

pub fn with_capacity(capacity: usize) -> Map

Creates an empty map with a capacity.

Source

pub fn order(&self) -> Order

Returns the order of the entries.

Source

pub fn set_order(&mut self, order: Order)

Sets the order of the entries.

Source

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] }
);
Source

pub fn set_multimap(&mut self, yes: bool)

Sets if the map is a multimap.

Source

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.

Source

pub fn set_ambiguous_empty(&mut self, yes: bool)

Sets if the map could also be an empty sequence when it’s empty.

Source

pub fn len(&self) -> usize

Returns the number of entries.

Source

pub fn is_empty(&self) -> bool

Returns true if the map has no entries.

Source

pub fn get<K: MapKey + ?Sized>(&self, key: &K) -> Option<&Value>

Returns the value of a key.

Source

pub fn get_mut<K: MapKey + ?Sized>(&mut self, key: &K) -> Option<&mut Value>

Returns the value of a key mutably.

Source

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.

Source

pub fn get_index_of<K: MapKey + ?Sized>(&self, key: &K) -> Option<usize>

Returns the index of a key.

Source

pub fn get_index(&self, index: usize) -> Option<(&Value, &Value)>

Returns the key and value at an index.

Source

pub fn get_index_mut(&mut self, index: usize) -> Option<(&Value, &mut Value)>

Returns the key and the mutable value at an index.

Source

pub fn contains_key<K: MapKey + ?Sized>(&self, key: &K) -> bool

Returns true if the map contains a key.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn retain<F: FnMut(&Value, &mut Value) -> bool>(&mut self, f: F)

Retains the entries for which the predicate returns true.

Source

pub fn sort_by<F>(&mut self, f: F)
where F: FnMut(&Value, &Value, &Value, &Value) -> Ordering,

Sorts the entries with a comparison function.

Source

pub fn clear(&mut self)

Removes all entries.

Source

pub fn iter(&self) -> Iter<'_> ⓘ

Returns an iterator over the entries.

Source

pub fn iter_mut(&mut self) -> IterMut<'_> ⓘ

Returns an iterator over the entries with mutable values.

Source

pub fn keys(&self) -> Keys<'_> ⓘ

Returns an iterator over the keys.

Source

pub fn values(&self) -> Values<'_> ⓘ

Returns an iterator over the values.

Source

pub fn values_mut(&mut self) -> ValuesMut<'_> ⓘ

Returns an iterator over the mutable values.

Trait Implementations§

Source§

impl Clone for Map

Source§

fn clone(&self) -> Map

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Map

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Map

Source§

fn default() -> Map

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Map

Source§

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>

Creates a sink that deserializes the value into the given slot. Read more
Source§

fn expecting() -> Cow<'static, str>

Returns what the value expects, for error messages. Read more
Source§

fn deserialize_atom( slot: &mut Slot<T, Self>, atom: Atom<'_>, state: &mut State, ) -> Result<(), Error>

Deserializes an atom into the slot. Read more
Source§

fn deserialize_borrowed_atom( slot: &mut Slot<T, Self>, atom: Atom<'de>, state: &mut State, ) -> Result<(), Error>

Deserializes an atom that borrows from the data being deserialized into the slot. Read more
Source§

fn describe_type(d: &mut dyn Describe)

Describes the Rust shape of the type. Read more
Source§

fn initial_value() -> Option<T>

Provides the value of a missing struct field. Read more
Source§

impl Drop for Map

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Eq for Map

Source§

impl<K: Into<Value>, V: Into<Value>> Extend<(K, V)> for Map

Source§

fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl From<Map> for Value

Source§

fn from(value: Map) -> Value

Converts to this type from the input type.
Source§

impl<K: Into<Value>, V: Into<Value>> FromIterator<(K, V)> for Map

Source§

fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Map

Creates a value from an iterator. Read more
Source§

impl Hash for Map

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl IntoIterator for Map

Source§

type Item = (Value, Value)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IntoIter ⓘ

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a Map

Source§

type Item = (&'a Value, &'a Value)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'a> ⓘ

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a mut Map

Source§

type Item = (&'a Value, &'a mut Value)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IterMut<'a> ⓘ

Creates an iterator from a value. Read more
Source§

impl PartialEq for Map

Source§

fn eq(&self, other: &Map) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Map

Source§

fn serialize<'a>(this: &'a Self, state: &mut State) -> Result<Emit<'a>, Error>

Serializes the value.
Source§

fn container_shape(value: &Self) -> ContainerShape

Returns the shape of the value if it’s a map or sequence. Read more
Source§

fn finish(value: &T, state: &mut State) -> Result<(), Error>

Invoked after the serialization finished. Read more
Source§

fn is_optional(value: &T) -> bool

Checks if the value represents an optional value. Read more
Source§

fn describe(value: &T, d: &mut dyn Describe)

Describes the Rust shape of the value. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.