pub struct Map<K, V> { /* private fields */ }Expand description
Represents a serializable key/value type.
Implementations§
Source§impl Map<String, Value>
impl Map<String, Value>
Sourcepub fn new() -> Map<String, Value>
pub fn new() -> Map<String, Value>
Makes a new, empty Map.
§Examples
use tower_sesh::value::Map;
let mut map = Map::new();
// entries can now be inserted into the empty map
map.insert("sesh".to_owned(), "a".into());Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, removing all elements.
§Examples
use tower_sesh::value::Map;
let mut a = Map::new();
a.insert("sesh".to_owned(), "a".into());
a.clear();
assert!(a.is_empty());Sourcepub fn get<Q>(&self, key: &Q) -> Option<&Value>
pub fn get<Q>(&self, key: &Q) -> Option<&Value>
Returns a reference to the value corresponding to the key.
The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.
§Examples
use tower_sesh::value::Map;
let mut map = Map::new();
map.insert("sesh".to_owned(), "a".into());
assert_eq!(map.get("sesh").and_then(|v| v.as_str()), Some("a"));
assert_eq!(map.get("notexist"), None);Sourcepub fn get_key_value<Q>(&self, key: &Q) -> Option<(&String, &Value)>
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&String, &Value)>
Returns the key-value pair matching the given key.
The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.
§Examples
use tower_sesh::value::Map;
let mut map = Map::new();
map.insert("sesh".to_owned(), "a".into());
let entry = map
.get_key_value("sesh")
.and_then(|(k, v)| Some(k.as_str()).zip(v.as_str()));
assert_eq!(entry, Some(("sesh", "a")));
assert_eq!(map.get_key_value("notexist"), None);Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Returns true if the map contains a value for the specified key.
The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.
§Examples
use tower_sesh::value::Map;
let mut map = Map::new();
map.insert("sesh".to_owned(), "a".into());
assert_eq!(map.contains_key("sesh"), true);
assert_eq!(map.contains_key("notexist"), false);Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut Value>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut Value>
Returns a mutable reference to the value corresponding to the key.
The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.
§Examples
use tower_sesh::value::Map;
let mut map = Map::new();
map.insert("sesh".to_owned(), "a".into());
if let Some(x) = map.get_mut("sesh") {
*x = "b".into();
}
assert_eq!(map["sesh"], "b");Sourcepub fn insert(&mut self, key: String, value: Value) -> Option<Value>
pub fn insert(&mut self, key: String, value: Value) -> Option<Value>
Inserts a key-value pair into the map.
If the map did not have this key present, None is returned.
If the map did have this key present, the value is updated, and the old value is returned.
§Examples
use tower_sesh::value::Map;
let mut map = Map::new();
assert_eq!(map.insert("sesh".to_owned(), "a".into()), None);
assert_eq!(map.is_empty(), false);
map.insert("sesh".to_owned(), "b".into());
let prev = map.insert("sesh".to_owned(), "c".into());
assert_eq!(prev.as_ref().and_then(|v| v.as_str()), Some("b"));
assert_eq!(map["sesh"], "c");Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<Value>
pub fn remove<Q>(&mut self, key: &Q) -> Option<Value>
Removes a key from the map, returning the value at the key if the key was previously in the map.
The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.
§Examples
use tower_sesh::value::Map;
let mut map = Map::new();
map.insert("sesh".to_owned(), "a".into());
assert_eq!(map.remove("sesh").as_ref().and_then(|v| v.as_str()), Some("a"));
assert_eq!(map.remove("sesh"), None);Sourcepub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(String, Value)>
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(String, Value)>
Removes a key from the map, returning the stored key and value if the key was previously in the map.
The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.
§Examples
use tower_sesh::value::Map;
let mut map = Map::new();
map.insert("sesh".to_owned(), "a".into());
let entry = map.remove_entry("sesh");
assert_eq!(
entry.as_ref()
.and_then(|(k, v)| Some(k.as_str()).zip(v.as_str())),
Some(("sesh", "a"))
);
assert_eq!(map.remove_entry("sesh"), None);Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the elements specified by the predicate.
In other words, remove all pairs (k, v) for which f(&k, &mut v)
returns false.
§Examples
use tower_sesh::{value::Map, Value};
let mut map = Map::from_iter([
("rust".into(), "a".into()),
("sesh".into(), "b".into()),
("tower".into(), "c".into()),
]);
// Keep only the elements with keys of length 4.
map.retain(|k, _| k.len() == 4);
let elements = map.into_iter().collect::<Vec<(String, Value)>>();
assert_eq!(
elements,
vec![("rust".into(), "a".into()), ("sesh".into(), "b".into())]
);Sourcepub fn append(&mut self, other: &mut Map<String, Value>)
pub fn append(&mut self, other: &mut Map<String, Value>)
Moves all elements from other into self, leaving other empty.
If a key from other is already present in self, the respective
value from self will be overwritten with the respective value from
other.
§Examples
use tower_sesh::value::Map;
let mut a = Map::new();
a.insert("one".to_owned(), "a".into());
a.insert("two".to_owned(), "b".into());
a.insert("three".to_owned(), "c".into()); // Note: Key ("three") also present in b.
let mut b = Map::new();
b.insert("three".to_owned(), "d".into()); // Note: Key ("three") also present in a.
b.insert("four".to_owned(), "e".into());
b.insert("five".to_owned(), "f".into());
a.append(&mut b);
assert_eq!(a.len(), 5);
assert_eq!(b.len(), 0);
assert_eq!(a["one"], "a");
assert_eq!(a["two"], "b");
assert_eq!(a["three"], "d"); // Note: "c" has been overwritten.
assert_eq!(a["four"], "e");
assert_eq!(a["five"], "f");Sourcepub fn entry<S>(&mut self, key: S) -> Entry<'_>
pub fn entry<S>(&mut self, key: S) -> Entry<'_>
Gets the given key’s corresponding entry in the map for in-place manipulation.
§Examples
use tower_sesh::value::Map;
let mut count = Map::new();
// count the number of occurrences of letters in the vec
for x in ["a", "b", "a", "c", "a", "b"] {
count.entry(x)
.and_modify(|curr| *curr = (curr.as_u64().unwrap_or(0) + 1).into())
.or_insert_with(|| 1.into());
}
assert_eq!(count["a"], 3);
assert_eq!(count["b"], 2);
assert_eq!(count["c"], 1);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the map.
§Examples
use tower_sesh::value::Map;
let mut a = Map::new();
assert_eq!(a.len(), 0);
a.insert("sesh".to_owned(), "a".into());
assert_eq!(a.len(), 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map contains no elements.
§Examples
use tower_sesh::value::Map;
let mut a = Map::new();
assert!(a.is_empty());
a.insert("sesh".to_owned(), "a".into());
assert!(!a.is_empty());Sourcepub fn iter(&self) -> Iter<'_> ⓘ
pub fn iter(&self) -> Iter<'_> ⓘ
Gets an iterator over the entries of the map.
§Examples
use tower_sesh::value::Map;
let mut map = Map::new();
map.insert("1".to_owned(), "a".into());
map.insert("2".to_owned(), "b".into());
map.insert("3".to_owned(), "c".into());
for (key, value) in map.iter() {
println!("{key}: {value:?}");
}
let (first_key, first_value) = map.iter().next().unwrap();
assert_eq!((first_key.as_str(), first_value.as_str().unwrap()), ("1", "a"));Sourcepub fn iter_mut(&mut self) -> IterMut<'_> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_> ⓘ
Gets a mutable iterator over the entries of the map.
§Examples
use tower_sesh::value::Map;
let mut map = Map::from_iter([
("a".to_owned(), 1.into()),
("b".to_owned(), 2.into()),
("c".to_owned(), 3.into()),
]);
// add 10 to the value if the key isn't "a"
for (key, value) in map.iter_mut() {
if key != "a" {
*value = (value.as_u64().unwrap_or(0) + 10).into();
}
}
assert_eq!(map["a"], 1);
assert_eq!(map["b"], 12);
assert_eq!(map["c"], 13);Sourcepub fn keys(&self) -> Keys<'_> ⓘ
pub fn keys(&self) -> Keys<'_> ⓘ
Gets an iterator over the keys of the map.
§Examples
use tower_sesh::value::Map;
let mut a = Map::new();
a.insert("rust".to_owned(), "a".into());
a.insert("sesh".to_owned(), "b".into());
let keys: Vec<_> = a.keys().cloned().collect();
assert_eq!(keys, ["rust", "sesh"]);Sourcepub fn values(&self) -> Values<'_> ⓘ
pub fn values(&self) -> Values<'_> ⓘ
Gets an iterator over the values of the map.
§Examples
use tower_sesh::value::Map;
let mut a = Map::new();
a.insert("rust".to_owned(), "hello".into());
a.insert("sesh".to_owned(), "goodbye".into());
let values: Vec<_> = a.values().cloned().collect();
assert_eq!(values, ["hello", "goodbye"]);Sourcepub fn values_mut(&mut self) -> ValuesMut<'_> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_> ⓘ
Gets a mutable iterator over the values of the map.
§Examples
use tower_sesh::{value::Map, Value};
let mut a = Map::new();
a.insert("rust".to_owned(), "hello".into());
a.insert("sesh".to_owned(), "goodbye".into());
for value in a.values_mut() {
match value {
Value::String(s) => s.push_str("!"),
_ => unimplemented!(),
}
}
let values: Vec<_> = a.values().cloned().collect();
assert_eq!(values, ["hello!", "goodbye!"]);Sourcepub fn into_values(self) -> IntoValues ⓘ
pub fn into_values(self) -> IntoValues ⓘ
Creates a consuming iterator visiting all the values of the map. The map cannot be used after calling this.
§Examples
use tower_sesh::value::Map;
let mut a = Map::new();
a.insert("rust".to_owned(), "hello".into());
a.insert("sesh".to_owned(), "goodbye".into());
let values: Vec<_> = a.into_values().collect();
assert_eq!(values, ["hello", "goodbye"]);Trait Implementations§
Source§impl<'de> Deserialize<'de> for Map<String, Value>
impl<'de> Deserialize<'de> for Map<String, Value>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl Extend<(String, Value)> for Map<String, Value>
impl Extend<(String, Value)> for Map<String, Value>
Source§fn extend<T: IntoIterator<Item = (String, Value)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (String, Value)>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<Q> Index<&Q> for Map<String, Value>
Access an element of this map. Panics if the given key is not present in the
map.
impl<Q> Index<&Q> for Map<String, Value>
Access an element of this map. Panics if the given key is not present in the map.
match val {
Value::String(s) => Some(s.as_str()),
Value::Array(arr) => arr[0].as_str(),
Value::Map(map) => map["type"].as_str(),
_ => None,
}Source§impl<Q> IndexMut<&Q> for Map<String, Value>
Mutably access an element of this map. Panics if the given key is not
present in the map.
impl<Q> IndexMut<&Q> for Map<String, Value>
Mutably access an element of this map. Panics if the given key is not present in the map.
map["key"] = Value::String("value".to_owned());