DefaultBTreeMap

Struct DefaultBTreeMap 

Source
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>

Source

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>

Source

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.

Source

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.

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 + Ord, V> DefaultBTreeMap<K, V>

Source

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

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.

Source

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);
Source

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.

Source

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>

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 + Ord, V> DefaultBTreeMap<K, V>

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

Source

pub fn clear(&mut self)

Source

pub fn first_key_value(&self) -> Option<(&K, &V)>
where K: Ord,

Source

pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>>
where K: Ord,

Source

pub fn pop_first(&mut self) -> Option<(K, V)>
where K: Ord,

Source

pub fn last_key_value(&self) -> Option<(&K, &V)>
where K: Ord,

Source

pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>>
where K: Ord,

Source

pub fn pop_last(&mut self) -> Option<(K, V)>
where K: Ord,

Source

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

Source

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

Source

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

Source

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

Source

pub fn append(&mut self, other: &mut Self)

Source

pub fn range<T, R>(&self, range: R) -> Range<'_, K, V>
where T: Ord + ?Sized, K: Borrow<T> + Ord, R: RangeBounds<T>,

Source

pub fn range_mut<T, R>(&mut self, range: R) -> RangeMut<'_, K, V>
where T: Ord + ?Sized, K: Borrow<T> + Ord, R: RangeBounds<T>,

Source

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

Source

pub fn into_keys(self) -> IntoKeys<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 keys(&self) -> Keys<'_, K, V>

Source

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

Source

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

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Trait Implementations§

Source§

impl<K: Clone + Eq + Ord, V: Clone> Clone for DefaultBTreeMap<K, V>

Source§

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

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: Eq + Ord, V> Debug for DefaultBTreeMap<K, V>
where BTreeMap<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 + Ord, V: Default> Default for DefaultBTreeMap<K, V>

Source§

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

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

Source§

impl<'de, K, V> Deserialize<'de> for DefaultBTreeMap<K, V>
where K: Deserialize<'de> + Eq + Ord, 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 + Ord, V: Default> From<BTreeMap<K, V>> for DefaultBTreeMap<K, V>

Source§

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>

Source§

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>

Source§

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

Creates a value from an iterator. Read more
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.

Source§

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 + 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.

Source§

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

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

impl<K: Eq + Ord, V: PartialEq> PartialEq for DefaultBTreeMap<K, V>

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> Serialize for DefaultBTreeMap<K, V>
where K: Serialize + Eq + Ord, 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 + Ord, V: Eq> Eq for DefaultBTreeMap<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for DefaultBTreeMap<K, V>
where V: Freeze,

§

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

§

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

§

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

§

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

§

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

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> DynClone for T
where 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 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,