Struct ChainMap

Source
pub struct ChainMap<K, V, S = RandomState> { /* private fields */ }
Expand description

The ChainMap type. See the module level documentation for more.

Implementations§

Source§

impl<K, V, S> ChainMap<K, V, S>

Source

pub fn new() -> Self

Creates an empty ChainMap.

The chain is initially created with a capacity of 0, so it will not allocated until a HashMap is inserted into the chain.

§Examples
use chain_map::ChainMap;
let mut chain: ChainMap<&str, i32> = ChainMap::new();
Source

pub fn with_capacity(capacity: usize) -> Self

Creates a ChainMap with the specified capacity.

Will be able to hold at least capacity HashMaps without reallocating. If capacity is 0, the chain will not allocate.

§Examples
use chain_map::ChainMap;
let mut chain: ChainMap<&str, i32> = ChainMap::with_capacity(10);
Source

pub fn push_map(&mut self, map: HashMap<K, V, S>)

Appends a map to the lowest-precedence end of the chain

§Panics

Panics if the number of maps in the chain overflows a usize.

§Examples
use std::collections::HashMap;
use chain_map::ChainMap;

let mut hash = HashMap::new();
hash.insert("key", "value");

let mut chain = ChainMap::new();
chain.push_map(hash);
Source§

impl<K, V, S> ChainMap<K, V, S>
where K: Hash + Eq, S: BuildHasher,

Source

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

Returns true if the ChainMap contains a value for the given key.

As with HashMap::contains_key, the supplied key may be any borrowed form of the key type, but Hash and Eq on the borrowed form must match those for the key type.

§Examples
use std::collections::HashMap;
use chain_map::ChainMap;

let mut hash = HashMap::new();
hash.insert("key", "value");

let mut chain = ChainMap::new();
chain.push_map(hash);
assert!(chain.contains_key("key"));
Source

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

Returns the highest-precedence value associated with the given key.

As with HashMap::get, the supplied key may be any borrowed form of the key type, but Hash and Eq on the borrowed form must match those for the key type.

§Examples
use std::collections::HashMap;
use chain_map::ChainMap;

let mut hash = HashMap::new();
hash.insert("key", "value");

let mut chain = ChainMap::new();
chain.push_map(hash);
assert_eq!(chain.get("key"), Some(&"value"));

Trait Implementations§

Source§

impl<K: Clone, V: Clone, S: Clone> Clone for ChainMap<K, V, S>

Source§

fn clone(&self) -> ChainMap<K, V, S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K, V, S> Debug for ChainMap<K, V, S>
where K: Eq + Hash + Debug, V: Debug, S: BuildHasher,

Source§

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

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

impl<K, V, S> Default for ChainMap<K, V, S>

Source§

fn default() -> Self

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

impl<K, V, S> Extend<HashMap<K, V, S>> for ChainMap<K, V, S>

Source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = HashMap<K, V, S>>,

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<K, V, S> FromIterator<HashMap<K, V, S>> for ChainMap<K, V, S>

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = HashMap<K, V, S>>,

Creates a value from an iterator. Read more
Source§

impl<K, Q, V, S> Index<&Q> for ChainMap<K, V, S>
where K: Eq + Hash + Borrow<Q>, Q: Eq + Hash + ?Sized, S: BuildHasher,

Source§

type Output = V

The returned type after indexing.
Source§

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

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

impl<K, V, S> PartialEq for ChainMap<K, V, S>
where K: Eq + Hash, V: PartialEq, S: BuildHasher,

Source§

fn eq(&self, other: &ChainMap<K, V, S>) -> 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, S> Eq for ChainMap<K, V, S>
where K: Eq + Hash, V: Eq, S: BuildHasher,

Auto Trait Implementations§

§

impl<K, V, S> Freeze for ChainMap<K, V, S>

§

impl<K, V, S> RefUnwindSafe for ChainMap<K, V, S>

§

impl<K, V, S> Send for ChainMap<K, V, S>
where S: Send, K: Send, V: Send,

§

impl<K, V, S> Sync for ChainMap<K, V, S>
where S: Sync, K: Sync, V: Sync,

§

impl<K, V, S> Unpin for ChainMap<K, V, S>
where S: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, S> UnwindSafe for ChainMap<K, V, S>
where K: UnwindSafe, V: UnwindSafe, S: 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> 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.