[−][src]Struct ordnung::Map
A binary tree implementation of a string -> JsonValue
map. You normally don't
have to interact with instances of Object
, much more likely you will be
using the JsonValue::Object
variant, which wraps around this struct.
Methods
impl<K, V> Map<K, V> where
K: Hash + Eq,
[src]
K: Hash + Eq,
pub fn new() -> Self
[src]
Create a new Map
.
pub fn with_capacity(capacity: usize) -> Self
[src]
Create a Map
with a given capacity
pub fn insert(&mut self, key: K, value: V) -> Option<V>
[src]
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. The key is not updated, though; this matters for
types that can be ==
without being identical.
Examples
use ordnung::Map; let mut map = Map::new(); assert_eq!(map.insert(37, "a"), None); assert_eq!(map.is_empty(), false); map.insert(37, "b"); assert_eq!(map.insert(37, "c"), Some("b")); assert_eq!(map[&37], "c");
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Hash + Eq,
[src]
K: Borrow<Q>,
Q: Hash + Eq,
Returns a reference to the value corresponding to the key.
The key may be any borrowed form of the map's key type, but Hash
and
Eq
on the borrowed form must match those for the key type.
Examples
use ordnung::Map; let mut map = Map::new(); map.insert(1, "a"); assert_eq!(map.get(&1), Some(&"a")); assert_eq!(map.get(&2), None);
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where
K: Borrow<Q>,
Q: Hash + Eq,
[src]
K: Borrow<Q>,
Q: Hash + Eq,
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 Hash
and
Eq
on the borrowed form must match those for the key type.
Examples
use ordnung::Map; let mut map = Map::new(); map.insert(1, "a"); assert_eq!(map.contains_key(&1), true); assert_eq!(map.contains_key(&2), false);
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Hash + Eq,
[src]
K: Borrow<Q>,
Q: Hash + Eq,
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 Hash and Eq on the borrowed form must match those for the key type.
Examples
use ordnung::Map; let mut map = Map::new(); map.insert(1, "a"); if let Some(x) = map.get_mut(&1) { *x = "b"; } assert_eq!(map[&1], "b");
pub fn get_or_insert<F>(&mut self, key: K, fill: F) -> &mut V where
F: FnOnce() -> V,
[src]
F: FnOnce() -> V,
Get a mutable reference to entry at key. Inserts a new entry by
calling F
if absent.
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Hash + Eq,
[src]
K: Borrow<Q>,
Q: Hash + Eq,
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 Hash
and
Eq
on the borrowed form must match those for the key type.
Examples
use ordnung::Map; let mut map = Map::new(); map.insert(1, "a"); assert_eq!(map.remove(&1), Some("a")); assert_eq!(map.remove(&1), None);
pub fn len(&self) -> usize
[src]
Returns the number of elements in the map.
pub fn is_empty(&self) -> bool
[src]
Returns true
if the map contains no elements.
pub fn clear(&mut self)
[src]
Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
pub fn iter(&self) -> Iter<K, V>
[src]
An iterator visiting all key-value pairs in insertion order.
The iterator element type is (&K, &V)
.
Examples
use ordnung::Map; let mut map = Map::new(); map.insert("a", 1); map.insert("b", 2); map.insert("c", 3); let entries: Vec<_> = map.iter().collect(); assert_eq!( entries, &[ (&"a", &1), (&"b", &2), (&"c", &3), ], );
pub fn iter_mut(&mut self) -> IterMut<K, V>
[src]
An iterator visiting all key-value pairs in insertion order, with mutable references to the values. The iterator element type is (&K, &mut V).
Examples
use ordnung::Map; let mut map = Map::new(); map.insert("a", 1); map.insert("b", 2); map.insert("c", 3); // Update all values for (_, val) in map.iter_mut() { *val *= 2; } // Check if values are doubled let entries: Vec<_> = map.iter().collect(); assert_eq!( entries, &[ (&"a", &2), (&"b", &4), (&"c", &6), ], );
Trait Implementations
impl<K: Clone, V: Clone> Clone for Map<K, V>
[src]
impl<K: Debug, V: Debug> Debug for Map<K, V>
[src]
impl<'json, IK, IV, K, V> FromIterator<(IK, IV)> for Map<K, V> where
IK: Into<K>,
IV: Into<V>,
K: Hash + Eq,
[src]
IK: Into<K>,
IV: Into<V>,
K: Hash + Eq,
fn from_iter<I>(iter: I) -> Self where
I: IntoIterator<Item = (IK, IV)>,
[src]
I: IntoIterator<Item = (IK, IV)>,
impl<'_, K, Q: ?Sized, V> Index<&'_ Q> for Map<K, V> where
K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash,
[src]
K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash,
type Output = V
The returned type after indexing.
fn index(&self, key: &Q) -> &V
[src]
Returns a reference to the value corresponding to the supplied key.
Panics
Panics if the key is not present in the HashMap.
impl<K, V> PartialEq<Map<K, V>> for Map<K, V> where
K: Hash + Eq,
V: PartialEq,
[src]
K: Hash + Eq,
V: PartialEq,
Auto Trait Implementations
impl<K, V> !RefUnwindSafe for Map<K, V>
impl<K, V> Send for Map<K, V> where
K: Send,
V: Send,
K: Send,
V: Send,
impl<K, V> Sync for Map<K, V> where
K: Sync,
V: Sync,
K: Sync,
V: Sync,
impl<K, V> Unpin for Map<K, V>
impl<K, V> !UnwindSafe for Map<K, V>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,