pub struct DefaultBTreeMap<K: Eq + Ord, V> { /* private fields */ }Expand description
A BTreeMap that returns a default when keys are accessed that are not present.
Implementations§
Source§impl<K: Eq + Ord, V: Default> DefaultBTreeMap<K, V>
impl<K: Eq + Ord, V: Default> DefaultBTreeMap<K, V>
Sourcepub fn new() -> DefaultBTreeMap<K, V>
pub fn new() -> DefaultBTreeMap<K, V>
The new() constructor creates an empty DefaultBTreeMap 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 + Ord, V: Clone + 'static> DefaultBTreeMap<K, V>
impl<K: Eq + Ord, V: Clone + 'static> DefaultBTreeMap<K, V>
Sourcepub fn with_default(default: V) -> DefaultBTreeMap<K, V>
pub fn with_default(default: V) -> DefaultBTreeMap<K, V>
Creates an empty DefaultBTreeMap with default as the default for missing keys.
When the provided default is equivalent to V::default() it is preferred to use
DefaultBTreeMap::default() instead.
Sourcepub fn from_map_with_default(
map: BTreeMap<K, V>,
default: V,
) -> DefaultBTreeMap<K, V>
pub fn from_map_with_default( map: BTreeMap<K, V>, default: V, ) -> DefaultBTreeMap<K, V>
Creates a DefaultBTreeMap based on a default and an already existing BTreeMap.
If V::default() is the supplied default, usage of the from() constructor or the
into() method on the original BTreeMap is preferred.
Sourcepub fn set_default(&mut self, new_default: V)
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 + Ord, V> DefaultBTreeMap<K, V>
impl<K: Eq + Ord, V> DefaultBTreeMap<K, V>
Sourcepub fn get<Q, QB: Borrow<Q>>(&self, key: QB) -> &V
pub fn get<Q, QB: Borrow<Q>>(&self, key: QB) -> &V
Returns a reference to the value stored for the provided key.
If the key is not in the DefaultBTreeMap 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.
Sourcepub fn get_default(&self) -> V
pub fn get_default(&self) -> V
Returns the an owned version of the default value
use defaultmap::DefaultBTreeMap;
assert_eq!(DefaultBTreeMap::<String, i32>::new().get_default(), 0);Sourcepub fn with_fn(default_fn: impl DefaultFn<V> + 'static) -> DefaultBTreeMap<K, V>
pub fn with_fn(default_fn: impl DefaultFn<V> + 'static) -> DefaultBTreeMap<K, V>
Creates an empty DefaultBTreeMap with default_fn as the default value generation
function for missing keys. When the provided default_fn only calls clone on a value,
using DefaultBTreeMap::new is preferred.
Sourcepub fn from_map_with_fn(
map: BTreeMap<K, V>,
default_fn: impl DefaultFn<V> + 'static,
) -> DefaultBTreeMap<K, V>
pub fn from_map_with_fn( map: BTreeMap<K, V>, default_fn: impl DefaultFn<V> + 'static, ) -> DefaultBTreeMap<K, V>
Creates a DefaultBTreeMap 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 DefaultBTreeMap::from(map) is preferred.
Source§impl<K: Eq + Ord, V> DefaultBTreeMap<K, V>
impl<K: Eq + Ord, V> DefaultBTreeMap<K, V>
Sourcepub fn get_mut(&mut self, key: K) -> &mut V
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 + Ord, V> DefaultBTreeMap<K, V>
These methods simply forward to the underlying BTreeMap, see that
documentation for
the usage of these methods.
impl<K: Eq + Ord, V> DefaultBTreeMap<K, V>
These methods simply forward to the underlying BTreeMap, see that
documentation for
the usage of these methods.
pub fn clear(&mut self)
pub fn first_key_value(&self) -> Option<(&K, &V)>where
K: Ord,
pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>>where
K: Ord,
pub fn pop_first(&mut self) -> Option<(K, V)>where
K: Ord,
pub fn last_key_value(&self) -> Option<(&K, &V)>where
K: Ord,
pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>>where
K: Ord,
pub fn pop_last(&mut self) -> Option<(K, V)>where
K: Ord,
pub fn contains_key<Q>(&self, k: &Q) -> bool
pub fn insert(&mut self, k: K, v: V) -> Option<V>
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
pub fn retain<RF>(&mut self, f: RF)
pub fn append(&mut self, other: &mut Self)
pub fn range<T, R>(&self, range: R) -> Range<'_, K, V>
pub fn range_mut<T, R>(&mut self, range: R) -> RangeMut<'_, K, V>
pub fn entry(&mut self, key: K) -> Entry<'_, K, V>
pub fn into_keys(self) -> IntoKeys<K, V>
pub fn into_values(self) -> IntoValues<K, V>
pub fn iter(&self) -> Iter<'_, K, V>
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>
pub fn keys(&self) -> Keys<'_, K, V>
pub fn values(&self) -> Values<'_, K, V>
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Trait Implementations§
Source§impl<K: Clone + Eq + Ord, V: Clone> Clone for DefaultBTreeMap<K, V>
impl<K: Clone + Eq + Ord, V: Clone> Clone for DefaultBTreeMap<K, V>
Source§fn clone(&self) -> DefaultBTreeMap<K, V>
fn clone(&self) -> DefaultBTreeMap<K, V>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<K: Eq + Ord, V: Default> Default for DefaultBTreeMap<K, V>
impl<K: Eq + Ord, V: Default> Default for DefaultBTreeMap<K, V>
Source§fn default() -> DefaultBTreeMap<K, V>
fn default() -> DefaultBTreeMap<K, V>
The default() method is equivalent to DefaultBTreeMap::new().
Source§impl<'de, K, V> Deserialize<'de> for DefaultBTreeMap<K, V>
impl<'de, K, V> Deserialize<'de> for DefaultBTreeMap<K, V>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<K: Eq + Ord, V: Default> From<BTreeMap<K, V>> for DefaultBTreeMap<K, V>
impl<K: Eq + Ord, V: Default> From<BTreeMap<K, V>> for DefaultBTreeMap<K, V>
Source§fn from(map: BTreeMap<K, V>) -> DefaultBTreeMap<K, V>
fn from(map: BTreeMap<K, V>) -> DefaultBTreeMap<K, V>
If you already have a BTreeMap that you would like to convert to a
DefaultBTreeMap you can use the into() method on the BTreeMap or the
from() constructor of DefaultBTreeMap.
The default value for missing keys will be V::default(),
if this is not desired DefaultBTreeMap::from_map_with_default() should be used.
Source§impl<K: Eq + Ord, V> From<DefaultBTreeMap<K, V>> for BTreeMap<K, V>
impl<K: Eq + Ord, V> From<DefaultBTreeMap<K, V>> for BTreeMap<K, V>
Source§fn from(default_map: DefaultBTreeMap<K, V>) -> BTreeMap<K, V>
fn from(default_map: DefaultBTreeMap<K, V>) -> BTreeMap<K, V>
The into method can be used to convert a DefaultBTreeMap back into a
BTreeMap.
Source§impl<K: Eq + Ord, V: Default> FromIterator<(K, V)> for DefaultBTreeMap<K, V>
impl<K: Eq + Ord, V: Default> FromIterator<(K, V)> for DefaultBTreeMap<K, V>
Source§impl<K: Eq + Ord, KB: Borrow<K>, V> Index<KB> for DefaultBTreeMap<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.
impl<K: Eq + Ord, KB: Borrow<K>, V> Index<KB> for DefaultBTreeMap<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.
Source§impl<K: Eq + Ord, V> IndexMut<K> for DefaultBTreeMap<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.
impl<K: Eq + Ord, V> IndexMut<K> for DefaultBTreeMap<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.