VecMapExt

Trait VecMapExt 

Source
pub trait VecMapExt<K, V> {
    // Required methods
    fn cleared(self) -> Self;
    fn inserted(self, k: K, v: V) -> Self
       where K: Eq;
    fn removed<Q>(self, k: &Q) -> Self
       where K: Eq + Borrow<Q>,
             Q: Eq + PartialEq<K>;
    fn retained<F>(self, f: F) -> Self
       where K: Eq,
             F: FnMut(&K, &mut V) -> bool;
    fn shrinked_to_fit(self) -> Self
       where K: Eq;
}
Expand description

Extension trait that contains functions that allow for chaining of VecMap functions.

Before:

use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert(37, "a");
map.insert(38, "b");

map.remove(&37);

assert_eq!(map.get(&37), None);
assert_eq!(map.get(&38), Some(&"b"));

After:

use fenn::vec_map::{VecMapExt, VecMap};

let map = VecMap::new()
    .inserted(37, "a")
    .inserted(38, "b")
    .removed(&37);

assert_eq!(map.get(&37), None);
assert_eq!(map.get(&38), Some(&"b"));

Required Methods§

Source

fn cleared(self) -> Self

Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.

§Examples
use fenn::vec_map::{VecMapExt, VecMap};

let a = VecMap::new()
    .inserted(1, "a")
    .cleared();

assert!(a.is_empty());
Source

fn inserted(self, k: K, v: V) -> Self
where K: Eq,

Inserts a key-value pair into the map.

If the map did have this key present, the value is updated. The key is not updated, though; this matters for types that can be == without being identical.

§Warning

Unlike the standard VecMap::insert that this wraps, this function ignores any returned values.

§Examples
use fenn::vec_map::{VecMapExt, VecMap};

let map = VecMap::new()
    .inserted(37, "a");

assert_eq!(map[&37], "a");
assert_eq!(map.is_empty(), false);
Source

fn removed<Q>(self, k: &Q) -> Self
where K: Eq + Borrow<Q>, Q: Eq + PartialEq<K>,

Removes a key from the map.

The key may be any borrowed form of the map’s key type, but Eq on the borrowed form must match those for the key type.

Source

fn retained<F>(self, f: F) -> Self
where K: Eq, F: FnMut(&K, &mut V) -> bool,

Retains only the elements specified by the predicate.

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

§Examples
use fenn::vec_map::{VecMapExt, VecMap};

let map = (0..8).map(|x|(x, x * 10)).collect::<VecMap<i32, i32>>()
    .retained(|&k, _| k % 2 == 0);

assert_eq!(map.len(), 4);
Source

fn shrinked_to_fit(self) -> Self
where K: Eq,

Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.

§Examples
use fenn::vec_map::{VecMapExt, VecMap};

let map: VecMap<i32, i32> = VecMap::with_capacity(100)
    .inserted(1, 2)
    .inserted(3, 4)
    .shrinked_to_fit();

assert!(map.capacity() >= 2);

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> VecMapExt<K, V> for VecMap<K, V>