Struct Map

Source
pub struct Map<K, V> { /* private fields */ }
Expand description

Represents a serializable key/value type.

Implementations§

Source§

impl Map<String, Value>

Source

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());
Source

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());
Source

pub fn get<Q>(&self, key: &Q) -> Option<&Value>
where String: Borrow<Q>, Q: ?Sized + Ord + Eq + Hash,

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

pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&String, &Value)>
where String: Borrow<Q>, Q: ?Sized + Ord + Eq + Hash,

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

pub fn contains_key<Q>(&self, key: &Q) -> bool
where String: Borrow<Q>, Q: ?Sized + Ord + Eq + Hash,

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

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut Value>
where String: Borrow<Q>, Q: ?Sized + Ord + Eq + Hash,

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");
Source

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");
Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<Value>
where String: Borrow<Q>, Q: ?Sized + Ord + Eq + Hash,

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

pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(String, Value)>
where String: Borrow<Q>, Q: ?Sized + Ord + Eq + Hash,

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

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

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

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");
Source

pub fn entry<S>(&mut self, key: S) -> Entry<'_>
where S: Into<String>,

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

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

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());
Source

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"));
Source

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

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

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

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

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 Clone for Map<String, Value>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Map<String, Value>

Source§

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

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

impl Default for Map<String, Value>

Source§

fn default() -> Self

Creates an empty Map.

Source§

impl<'de> Deserialize<'de> for Map<String, Value>

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Extend<(String, Value)> for Map<String, Value>

Source§

fn extend<T: IntoIterator<Item = (String, Value)>>(&mut self, iter: T)

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

fn extend_one(&mut self, item: A)

🔬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<String, Value>> for Value

Source§

fn from(value: Map<String, Value>) -> Self

Convert Map to Value::Map.

§Examples
use tower_sesh::{value::Map, Value};

let mut m = Map::new();
m.insert("Lorem".to_owned(), "ipsum".into());
let x: Value = m.into();
Source§

impl FromIterator<(String, Value)> for Map<String, Value>

Source§

fn from_iter<T: IntoIterator<Item = (String, Value)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<Q> Index<&Q> for Map<String, Value>
where String: Borrow<Q>, Q: ?Sized + Ord + Eq + Hash,

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§

type Output = Value

The returned type after indexing.
Source§

fn index(&self, index: &Q) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<Q> IndexMut<&Q> for Map<String, Value>
where String: Borrow<Q>, Q: ?Sized + Ord + Eq + Hash,

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());
Source§

fn index_mut(&mut self, index: &Q) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a> IntoIterator for &'a Map<String, Value>

Source§

type Item = <<&'a Map<String, Value> as IntoIterator>::IntoIter as Iterator>::Item

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) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a mut Map<String, Value>

Source§

type Item = <<&'a mut Map<String, Value> as IntoIterator>::IntoIter as Iterator>::Item

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) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for Map<String, Value>

Source§

type Item = <<Map<String, Value> as IntoIterator>::IntoIter as Iterator>::Item

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) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Map<String, Value>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Map<String, Value>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Map<String, Value>

Auto Trait Implementations§

§

impl<K, V> Freeze for Map<K, V>

§

impl<K, V> RefUnwindSafe for Map<K, V>

§

impl<K, V> Send for Map<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for Map<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for Map<K, V>

§

impl<K, V> UnwindSafe for Map<K, V>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

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

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,