VecMap

Struct VecMap 

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

A Vec backed map implementation.

Implementations§

Source§

impl<K, V> VecMap<K, V>

Source

pub fn new() -> VecMap<K, V>

Creates an empty VecMap.

The map is initially created with a capacity of 0, so it will not allocate until it is first inserted into.

§Examples
use fenn::vec_map::VecMap;

let mut map: VecMap<&str, i32> = VecMap::new();
Source

pub fn with_capacity(capacity: usize) -> VecMap<K, V>

Creates an empty VecMap with the specified capacity.

The map will be able to hold at least capacity elements without reallocating. If capacity is 0, the map will not allocate.

§Examples
use fenn::vec_map::VecMap;

let mut map: VecMap<&str, i32> = VecMap::with_capacity(10);
Source

pub fn capacity(&self) -> usize

Returns the number of elements the map can hold without reallocating.

This number is a lower bound; the VecMap<K, V> might be able to hold more, but is guaranteed to be able to hold at least this many.

§Examples
use fenn::vec_map::VecMap;

let map: VecMap<i32, i32> = VecMap::with_capacity(100);

assert!(map.capacity() >= 100);
Source

pub fn keys(&self) -> Keys<'_, K, V>

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

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for key in map.keys() {
    println!("{}", key);
}
Source

pub fn values(&self) -> Values<'_, K, V>

An iterator visiting all values in arbitrary order. The iterator element type is &'a V.

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for val in map.values() {
    println!("{}", val);
}
Source

pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>

An iterator visiting all values mutably in arbitrary order. The iterator element type is &'a mut V.

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for val in map.values_mut() {
    *val = *val + 10;
}

for val in map.values() {
    println!("{}", val);
}
Source

pub fn iter(&self) -> Iter<'_, K, V>

An iterator visiting all key-value pairs in arbitrary order. The iterator element type is (&'a K, &'a V).

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for (key, val) in map.iter() {
    println!("key: {} val: {}", key, val);
}
Source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

An iterator visiting all key-value pairs in arbitrary order, with mutable references to the values.

The iterator element type is (&'a K, &'a mut V).

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

// Update all values
for (_, val) in map.iter_mut() {
    *val *= 2;
}

for (key, val) in &map {
    println!("key: {} val: {}", key, val);
}
Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

assert_eq!(map.len(), 0);

map.insert(1, "a");

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

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

assert!(map.is_empty());

map.insert(1, "a");

assert!(!map.is_empty());
Source

pub fn drain(&mut self) -> Drain<'_, K, V>

Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert(1, "a");
map.insert(2, "b");

for (k, v) in map.drain().take(1) {
    assert!(k == 1 || k == 2);
    assert!(v == "a" || v == "b");
}

assert!(map.is_empty());
Source

pub fn retain<F>(&mut self, f: F)
where 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::VecMap;

let mut map: VecMap<i32, i32> = (0..8).map(|x|(x, x*10)).collect();

map.retain(|&k, _| k % 2 == 0);

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

pub fn clear(&mut self)

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

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert(1, "a");

map.clear();

assert!(map.is_empty());
Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted in the VecMap. The collection may reserve more space to avoid frequent reallocations.

§Panics

Panics if the new allocation size overflows usize.

§Examples
use fenn::vec_map::VecMap;

let mut map: VecMap<&str, i32> = VecMap::new();

map.reserve(10);
Source

pub fn shrink_to_fit(&mut self)

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::VecMap;

let mut map: VecMap<i32, i32> = VecMap::with_capacity(100);

map.insert(1, 2);
map.insert(3, 4);

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

map.shrink_to_fit();

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

pub fn truncate(&mut self, len: usize)

Source§

impl<K, V> VecMap<K, V>
where K: Eq,

Source

pub fn entry(&mut self, key: K) -> Entry<'_, K, V>

Gets the given key’s corresponding entry in the map for in-place manipulation.

§Examples
use fenn::vec_map::VecMap;

let mut letters = VecMap::new();

for ch in "a short treatise on fungi".chars() {
    let counter = letters.entry(ch).or_insert(0);

    *counter += 1;
}

assert_eq!(letters[&'s'], 2);
assert_eq!(letters[&'t'], 3);
assert_eq!(letters[&'u'], 1);
assert_eq!(letters.get(&'y'), None);
Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Eq + ?Sized,

Returns a reference to the value corresponding to the key.

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

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert(1, "a");

assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);
Source

pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: PartialEq<K> + ?Sized,

Returns the key-value pair corresponding to the supplied key.

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

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert(1, "a");

assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
assert_eq!(map.get_key_value(&2), None);
Source

pub fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)>
where K: Borrow<Q>, Q: PartialEq<K> + ?Sized,

Returns the key-value pair corresponding to the supplied key, with a mutable reference to value.

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

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert(1, "a");

let (k, v) = map.get_key_value_mut(&1).unwrap();

assert_eq!(k, &1);
assert_eq!(v, &mut "a");

*v = "b";

assert_eq!(map.get_key_value_mut(&1), Some((&1, &mut "b")));
assert_eq!(map.get_key_value_mut(&2), None);
Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: PartialEq<K> + ?Sized,

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 PartialEq on the borrowed form must match those for the key type.

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert(1, "a");

assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);
Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Eq + ?Sized,

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 PartialEq on the borrowed form must match those for the key type.

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert(1, "a");

if let Some(x) = map.get_mut(&1) {
    *x = "b";
}

assert_eq!(map[&1], "b");
Source

pub fn insert(&mut self, key: K, value: V) -> Option<V>

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 fenn::vec_map::VecMap;

let mut map = VecMap::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");
Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: PartialEq<K> + ?Sized,

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 PartialEq on the borrowed form must match those for the key type.

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert(1, "a");

assert_eq!(map.remove(&1), Some("a"));
assert_eq!(map.remove(&1), None);
Source

pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
where K: Borrow<Q>, Q: PartialEq<K> + ?Sized,

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 PartialEq on the borrowed form must match those for the key type.

§Examples
use fenn::vec_map::VecMap;

let mut map = VecMap::new();

map.insert(1, "a");

assert_eq!(map.remove_entry(&1), Some((1, "a")));
assert_eq!(map.remove(&1), None);

Trait Implementations§

Source§

impl<K, V> Clone for VecMap<K, V>
where K: Clone, V: Clone,

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<K, V> Debug for VecMap<K, V>
where K: Debug, V: Debug,

Source§

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

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

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

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<K, V> FromIterator<(K, V)> for VecMap<K, V>
where K: Eq,

Source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<K, Q, V> Index<&Q> for VecMap<K, V>
where K: Eq + Borrow<Q>, Q: Eq + ?Sized,

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, key: &Q) -> &V

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

impl<K, Q, V> IndexMut<&Q> for VecMap<K, V>
where K: Eq + Borrow<Q>, Q: Eq + ?Sized,

Source§

fn index_mut(&mut self, key: &Q) -> &mut V

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

impl<'a, K, V> IntoIterator for &'a VecMap<K, V>

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

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, K, V> IntoIterator for &'a mut VecMap<K, V>

Source§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V>

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

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

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<K, V> PartialEq for VecMap<K, V>
where K: PartialEq, V: PartialEq,

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

Source§

fn cleared(self) -> Self

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

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

Inserts a key-value pair into the map. Read more
Source§

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

Removes a key from the map. Read more
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. Read more
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. Read more
Source§

impl<K, V> Eq for VecMap<K, V>
where K: Eq, V: Eq,

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<K, V> Unpin for VecMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for VecMap<K, V>
where K: UnwindSafe, V: UnwindSafe,

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, 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoIteratorExt for T
where T: IntoIterator,

Source§

fn chain_if_with<F, C>(self, cond: bool, fun: F) -> ChainIf<T, C>
where F: FnOnce() -> C, C: IntoIterator<Item = <T as IntoIterator>::Item>,

Conditionally chains a given iterator. Read more
Source§

fn chain_if_else_with<FI, I, FE, E>( self, cond: bool, fun_if: FI, fun_else: FE, ) -> ChainIfElse<T, I, E>
where FI: FnOnce() -> I, I: IntoIterator<Item = <T as IntoIterator>::Item>, FE: FnOnce() -> E, E: IntoIterator<Item = <T as IntoIterator>::Item>,

Conditionally chains a given iterator falling back on another. Read more
Source§

fn chain_if<C>(self, cond: bool, iter: C) -> ChainIf<Self, C>
where C: IntoIterator<Item = Self::Item>,

Conditionally chains a given iterator. Read more
Source§

fn chain_if_else<I, E>( self, cond: bool, iter_if: I, iter_else: E, ) -> ChainIfElse<Self, I, E>
where I: IntoIterator<Item = Self::Item>, E: IntoIterator<Item = Self::Item>,

Conditionally chains a given iterator falling back on another. Read more
Source§

impl<T> IntoOption for T

Source§

fn some_if(self, predicate: bool) -> Option<Self>

Results Some(self) if the predicate returns true, or None otherwise.
Source§

fn with_some_if<F>(self, predicate: F) -> Option<Self>
where F: FnOnce(&Self) -> bool,

Results Some(self) if the predicate returns true, or None otherwise.
Source§

fn none_if(self, predicate: bool) -> Option<Self>

Results None if the predicate returns true, or Some(self) otherwise.
Source§

fn with_none_if<F>(self, predicate: F) -> Option<Self>
where F: FnOnce(&Self) -> bool,

Results None if the predicate returns true, or Some(self) otherwise.
Source§

impl<T> IntoResult for T

Source§

fn ok_if<E>(self, predicate: bool, err: E) -> Result<Self, E>

Results Ok(self) if the predicate returns true, or Err(err) otherwise.
Source§

fn with_ok_if<F, E>(self, predicate: F, err: E) -> Result<Self, E>
where F: FnOnce(&Self) -> bool,

Results Ok(self) if the predicate returns true, or Err(err) otherwise.
Source§

fn err_if<E>(self, predicate: bool, err: E) -> Result<Self, E>

Results Err(err) if the predicate returns true, or Ok(self) otherwise.
Source§

fn with_err_if<F, E>(self, predicate: F, err: E) -> Result<Self, E>
where F: FnOnce(&Self) -> bool,

Results Err(err) if the predicate returns true, or Ok(self) otherwise.
Source§

impl<T> Peep for T

Source§

fn peep<F, R>(self, run: F) -> Self
where F: FnOnce(&Self) -> R, R: Sized,

Source§

fn peep_dbg<F, R>(self, run: F) -> Self
where F: FnOnce(&Self) -> R, R: Sized,

Source§

fn peep_mut<F, R>(self, run: F) -> Self
where F: FnOnce(&mut Self) -> R, R: Sized,

Source§

fn peep_mut_dbg<F, R>(self, run: F) -> Self
where F: FnOnce(&mut Self) -> R, R: Sized,

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

Source§

fn wrap_ref<F>(self, wrap: F) -> Self
where F: FnOnce(&Self),

Turns a self reference function call into an ‘inline’/‘builder’ call. Read more
Source§

fn wrap_mut<F>(self, wrap: F) -> Self
where F: FnOnce(&mut Self),

Turns a self mutable reference function call into an ‘inline’/‘builder’ call. Read more
Source§

fn wrap_map<F, R>(self, wrap: F) -> R
where F: FnOnce(Self) -> R,

Turns a consuming function call into an ‘inline’/‘builder’ call. Read more