Skip to main content

BackingMap

Trait BackingMap 

Source
pub trait BackingMap<K, V>: Sized {
    // Required methods
    fn new() -> Self;
    fn insert(&mut self, key: K, value: V) -> Option<V>;
    fn get(&self, key: &K) -> Option<&V>;
    fn get_mut(&mut self, key: &K) -> Option<&mut V>;
    fn remove(&mut self, key: &K) -> Option<V>;
    fn len(&self) -> usize;
    fn is_empty(&self) -> bool;

    // Provided method
    fn with_capacity(capacity: usize) -> Self { ... }
}
Expand description

Trait for types that can back a structible struct.

This trait defines the operations required for a map type to be used as the backing storage for a structible struct. It is implemented for HashMap and BTreeMap from the standard library.

Users can implement this trait for custom map types to use them as backing storage.

Required Methods§

Source

fn new() -> Self

Creates a new, empty map.

Source

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

Inserts a key-value pair into the map, returning the previous value if present.

Source

fn get(&self, key: &K) -> Option<&V>

Returns a reference to the value for the given key.

Source

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

Returns a mutable reference to the value for the given key.

Source

fn remove(&mut self, key: &K) -> Option<V>

Removes a key from the map, returning the value if present.

Source

fn len(&self) -> usize

Returns the number of entries in the map.

Source

fn is_empty(&self) -> bool

Returns true if the map contains no entries.

Provided Methods§

Source

fn with_capacity(capacity: usize) -> Self

Creates a new, empty map with capacity for at least capacity elements.

Map types that don’t support pre-allocation (like BTreeMap) can ignore the capacity hint and just call new().

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<K, V> BackingMap<K, V> for BTreeMap<K, V>
where K: Ord,

Source§

fn new() -> Self

Source§

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

Source§

fn get(&self, key: &K) -> Option<&V>

Source§

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

Source§

fn remove(&mut self, key: &K) -> Option<V>

Source§

fn len(&self) -> usize

Source§

fn is_empty(&self) -> bool

Source§

impl<K, V> BackingMap<K, V> for HashMap<K, V>
where K: Eq + Hash,

Source§

fn new() -> Self

Source§

fn with_capacity(capacity: usize) -> Self

Source§

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

Source§

fn get(&self, key: &K) -> Option<&V>

Source§

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

Source§

fn remove(&mut self, key: &K) -> Option<V>

Source§

fn len(&self) -> usize

Source§

fn is_empty(&self) -> bool

Implementors§