pub struct DefaultHashMap<K: Eq + Hash, V> { /* private fields */ }
Expand description

A HashMap that returns a default when keys are accessed that are not present.

Implementations§

source§

impl<K: Eq + Hash, V: Default> DefaultHashMap<K, V>

source

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

The new() constructor creates an empty DefaultHashMap with the default of V as the default for missing keys. This is desired default for most use cases, if your case requires a different default you should use the with_default() constructor.

source§

impl<K: Eq + Hash, V: Clone + 'static> DefaultHashMap<K, V>

source

pub fn with_default(default: V) -> DefaultHashMap<K, V>

Creates an empty DefaultHashMap with default as the default for missing keys. When the provided default is equivalent to V::default() it is preferred to use DefaultHashMap::default() instead.

source

pub fn from_map_with_default( map: HashMap<K, V>, default: V ) -> DefaultHashMap<K, V>

Creates a DefaultHashMap based on a default and an already existing HashMap. If V::default() is the supplied default, usage of the from() constructor or the into() method on the original HashMap is preferred.

source

pub fn set_default(&mut self, new_default: V)

Changes the default value permanently or until set_default() is called again.

source§

impl<K: Eq + Hash, V> DefaultHashMap<K, V>

source

pub fn get<Q, QB: Borrow<Q>>(&self, key: QB) -> &Vwhere K: Borrow<Q>, Q: ?Sized + Hash + Eq,

Returns a reference to the value stored for the provided key. If the key is not in the DefaultHashMap a reference to the default value is returned. Usually the map[key] method of retrieving keys is preferred over using get directly. This method accepts both references and owned values as the key.

source

pub fn get_default(&self) -> V

Returns the an owned version of the default value

use defaultmap::DefaultHashMap;
assert_eq!(DefaultHashMap::<String, i32>::new().get_default(), 0);
source

pub fn with_fn(default_fn: impl DefaultFn<V> + 'static) -> DefaultHashMap<K, V>

Creates an empty DefaultHashMap with default_fn as the default value generation function for missing keys. When the provided default_fn only calls clone on a value, using DefaultHashMap::new is preferred.

source

pub fn from_map_with_fn( map: HashMap<K, V>, default_fn: impl DefaultFn<V> + 'static ) -> DefaultHashMap<K, V>

Creates a DefaultHashMap based on an existing map and using default_fn as the default value generation function for missing keys. When the provided default_fn is equivalent to V::default(), then using DefaultHashMap::from(map) is preferred.

source§

impl<K: Eq + Hash, V> DefaultHashMap<K, V>

source

pub fn get_mut(&mut self, key: K) -> &mut V

Returns a mutable reference to the value stored for the provided key. If there is no value stored for the key the default value is first inserted for this key before returning the reference. Usually the map[key] = new_val is prefered over using get_mut directly. This method only accepts owned values as the key.

source§

impl<K: Eq + Hash, V> DefaultHashMap<K, V>

These methods simply forward to the underlying HashMap, see that documentation for the usage of these methods.

source

pub fn capacity(&self) -> usize

source

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

source

pub fn into_keys(self) -> IntoKeys<K, V>

source

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

source

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

source

pub fn into_values(self) -> IntoValues<K, V>

source

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

source

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

source

pub fn len(&self) -> usize

source

pub fn is_empty(&self) -> bool

source

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

source

pub fn retain<RF>(&mut self, f: RF)where RF: FnMut(&K, &mut V) -> bool,

source

pub fn clear(&mut self)

source

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

source

pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>

source

pub fn shrink_to_fit(&mut self)

source

pub fn shrink_to(&mut self, min_capacity: usize)

source

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

source

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

source

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

source

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

source

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

Trait Implementations§

source§

impl<K: Clone + Eq + Hash, V: Clone> Clone for DefaultHashMap<K, V>

source§

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

Returns a copy 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: Eq + Hash, V> Debug for DefaultHashMap<K, V>where HashMap<K, V>: Debug, V: Debug,

source§

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

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

impl<K: Eq + Hash, V: Default> Default for DefaultHashMap<K, V>

source§

fn default() -> DefaultHashMap<K, V>

The default() method is equivalent to DefaultHashMap::new().

source§

impl<'de, K, V> Deserialize<'de> for DefaultHashMap<K, V>where K: Deserialize<'de> + Eq + Hash, V: Deserialize<'de> + Default,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<K: Eq + Hash, V> From<DefaultHashMap<K, V>> for HashMap<K, V>

source§

fn from(default_map: DefaultHashMap<K, V>) -> HashMap<K, V>

The into method can be used to convert a DefaultHashMap back into a HashMap.

source§

impl<K: Eq + Hash, V: Default> From<HashMap<K, V, RandomState>> for DefaultHashMap<K, V>

source§

fn from(map: HashMap<K, V>) -> DefaultHashMap<K, V>

If you already have a HashMap that you would like to convert to a DefaultHashMap you can use the into() method on the HashMap or the from() constructor of DefaultHashMap. The default value for missing keys will be V::default(), if this is not desired DefaultHashMap::from_map_with_default() should be used.

source§

impl<K: Eq + Hash, V: Default> FromIterator<(K, V)> for DefaultHashMap<K, V>

source§

fn from_iter<I>(iter: I) -> Selfwhere I: IntoIterator<Item = (K, V)>,

Creates a value from an iterator. Read more
source§

impl<K: Eq + Hash, KB: Borrow<K>, V> Index<KB> for DefaultHashMap<K, V>

Implements the Index trait so you can do map[key]. Nonmutable indexing can be done both by passing a reference or an owned value as the key.

§

type Output = V

The returned type after indexing.
source§

fn index(&self, index: KB) -> &V

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

impl<K: Eq + Hash, V> IndexMut<K> for DefaultHashMap<K, V>

Implements the IndexMut trait so you can do map[key] = val. Mutably indexing can only be done when passing an owned value as the key.

source§

fn index_mut(&mut self, index: K) -> &mut V

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

impl<K: Eq + Hash, V: PartialEq> PartialEq<DefaultHashMap<K, V>> for DefaultHashMap<K, V>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K, V> Serialize for DefaultHashMap<K, V>where K: Serialize + Eq + Hash, V: Serialize,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<K: Eq + Hash, V: Eq> Eq for DefaultHashMap<K, V>

Auto Trait Implementations§

§

impl<K, V> !RefUnwindSafe for DefaultHashMap<K, V>

§

impl<K, V> !Send for DefaultHashMap<K, V>

§

impl<K, V> !Sync for DefaultHashMap<K, V>

§

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

§

impl<K, V> !UnwindSafe for DefaultHashMap<K, V>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> DynClone for Twhere T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,