Trait MutableKeys

Source
pub trait MutableKeys: Sealed {
    type Key;
    type Value;

    // Required methods
    fn get_full_mut2<Q>(
        &mut self,
        key: &Q,
    ) -> Option<(usize, &mut Self::Key, &mut Self::Value)>
       where Self::Key: Borrow<Q>,
             Q: Eq + ?Sized;
    fn get_index_mut2(
        &mut self,
        index: usize,
    ) -> Option<(&mut Self::Key, &mut Self::Value)>;
    fn retain2<F>(&mut self, f: F)
       where F: FnMut(&mut Self::Key, &mut Self::Value) -> bool;
    fn iter_mut2(&mut self) -> IterMut2<'_, Self::Key, Self::Value> ;
    fn keys_mut(&mut self) -> KeysMut<'_, Self::Key, Self::Value> ;
}
Expand description

Opt-in mutable access to keys.

These methods expose &mut K, mutable references to the key as it is stored in the map.

You are allowed to modify the keys in the map as long as the modified key does not compare equal to another key already in the map.

If keys are modified erroneously, you are no longer able to look up values for duplicate keys directly. This is sound (memory safe) but a logical error hazard (just like implementing PartialEq, Eq, or Hash incorrectly would be).

This trait is sealed and cannot be implemented for types outside this crate.

Required Associated Types§

Source

type Key

The map’s key type.

Source

type Value

The map’s value type.

Required Methods§

Source

fn get_full_mut2<Q>( &mut self, key: &Q, ) -> Option<(usize, &mut Self::Key, &mut Self::Value)>
where Self::Key: Borrow<Q>, Q: Eq + ?Sized,

Return the index, a mutable reference to the key and a mutable reference to the value stored for key, if it is present, else None.

§Examples
use vecmap::map::{MutableKeys, VecMap};

let mut map = VecMap::new();
map.insert(1, "a");

if let Some((_, k, v)) = map.get_full_mut2(&1) {
    *k = 2;
    *v = "b";
}
assert_eq!(map.get(&1), None);
assert_eq!(map.get(&2), Some(&"b"));
Source

fn get_index_mut2( &mut self, index: usize, ) -> Option<(&mut Self::Key, &mut Self::Value)>

Return a mutable reference to the key and a mutable reference to the value stored at index, if it is present, else None.

§Examples
use vecmap::map::{MutableKeys, VecMap};

let mut map = VecMap::new();
map.insert(1, "a");
if let Some((k, v)) = map.get_index_mut2(0) {
    *k = 2;
    *v = "b";
}
assert_eq!(map[0], "b");
assert_eq!(map.get(&1), None);
assert_eq!(map.get(&2), Some(&"b"));
Source

fn retain2<F>(&mut self, f: F)
where F: FnMut(&mut Self::Key, &mut Self::Value) -> bool,

Retains only the elements specified by the predicate.

In other words, remove all pairs (k, v) for which f(&mut k, &mut v) returns false.

§Examples
use vecmap::map::{MutableKeys, VecMap};

let mut map: VecMap<i32, i32> = (0..8).map(|x| (x, x*10)).collect();
map.retain2(|k, v| {
    std::mem::swap(k, v);  
    *v % 2 == 0
});
assert_eq!(map.len(), 4);
assert_eq!(map, VecMap::from([(0, 0), (20, 2), (40, 4), (60, 6)]));
Source

fn iter_mut2(&mut self) -> IterMut2<'_, Self::Key, Self::Value>

An iterator visiting all key-value pairs in insertion order, with mutable references to the values. The iterator element type is (&'a mut K, &'a mut V).

§Examples
use vecmap::map::{MutableKeys, VecMap};

let mut map = VecMap::from([
    ('a', 1),
    ('b', 2),
    ('c', 3),
]);

// Update all values
for (key, val) in map.iter_mut2() {
    *key = key.to_ascii_uppercase();
    *val *= 2;
}

assert_eq!(map, VecMap::from([('A', 2), ('B', 4), ('C', 6)]));
Source

fn keys_mut(&mut self) -> KeysMut<'_, Self::Key, Self::Value>

An iterator visiting all keys mutably in insertion order. The iterator element type is &'a mut K.

§Examples
use vecmap::map::{MutableKeys, VecMap};

let mut map = VecMap::from([
    (1, "a"),
    (2, "b"),
    (3, "c"),
]);

for key in map.keys_mut() {
    *key = *key * 10;
}

assert_eq!(map, VecMap::from([(10, "a"), (20, "b"), (30, "c")]));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<K, V> MutableKeys for VecMap<K, V>

Source§

type Key = K

Source§

type Value = V