OccupiedEntry

Struct OccupiedEntry 

Source
pub struct OccupiedEntry<'a, K1: 'a, K2: 'a, V: 'a> { /* private fields */ }
Expand description

A view into an occupied entry in a DHashMap. It is part of the Entry enum and OccupiedError struct.

Implementations§

Source§

impl<'a, K1, K2, V> OccupiedEntry<'a, K1, K2, V>

Source

pub fn key1(&self) -> &K1

Gets a reference to the key #1 of type K1 in the entry.

§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;

let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 12);

if let Ok(entry) = map.entry("poneyland", 0) {
    match entry {
        Entry::Occupied(oc_entry) => {
            assert_eq!(oc_entry.key1(), &"poneyland");
        }
        Entry::Vacant(_) => panic!("Something go wrong!!!")
    }
}
Source

pub fn key2(&self) -> &K2

Gets a reference to the key #2 of type K2 in the entry.

§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;

let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 12);

if let Ok(entry) = map.entry("poneyland", 0) {
    match entry {
        Entry::Occupied(oc_entry) => {
            assert_eq!(oc_entry.key2(), &0);
        }
        Entry::Vacant(_) => panic!("Something go wrong!!!")
    }
}
Source

pub fn keys(&self) -> (&K1, &K2)

Gets a reference to the keys of type K1 and K2 in the entry. Return tuple of type (&'a K1, &'a K2).

§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;

let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 12);

if let Ok(entry) = map.entry("poneyland", 0) {
    match entry {
        Entry::Occupied(oc_entry) => {
            assert_eq!(oc_entry.keys(), (&"poneyland", &0));
        }
        Entry::Vacant(_) => panic!("Something go wrong!!!")
    }
}
Source

pub fn remove_entry(self) -> (K1, K2, V)

Take the ownership of the keys and value from the map. Keeps the allocated memory for reuse.

§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;

// So lets create some map and insert some element
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 10);
map.insert("bearland",  1, 11);

// And also reserve some space for additional elements
map.reserve(15);
// Now our map can hold at least 17 elements
let capacity_before_entries_remove = map.capacity();
assert!(capacity_before_entries_remove >= 17);

assert!(map.get_key1("poneyland") == Some(&10) && map.get_key2(&0) == Some(&10));
if let Ok(entry) = map.entry("poneyland", 0) {
    match entry {
        Entry::Occupied(oc_entry) => {
            // We delete the entry from the map.
            let tuple = oc_entry.remove_entry();
            assert_eq!(tuple, ("poneyland", 0, 10));
        }
        Entry::Vacant(_) => panic!("Something go wrong!!!")
    }
}
assert!(map.get_key1("poneyland") == None && map.get_key2(&0) == None);

assert!(map.get_key1("bearland") == Some(&11) && map.get_key2(&1) == Some(&11));
if let Ok(entry) = map.entry("bearland", 1) {
    match entry {
        Entry::Occupied(oc_entry) => {
            // We delete the entry from the map.
            let tuple = oc_entry.remove_entry();
            assert_eq!(tuple, ("bearland",  1, 11));
        }
        Entry::Vacant(_) => panic!("Something go wrong!!!")
    }
}
assert!(map.get_key1("bearland") == None && map.get_key2(&1) == None);

// But the capacity of our map isn't changed and still equals to the capacity before
// using `remove_entry` method
assert_eq!(map.capacity(), capacity_before_entries_remove);
Source

pub fn get(&self) -> &V

Gets a reference to the value in the entry.

§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;

let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 12);

if let Ok(entry) = map.entry("poneyland", 0) {
    match entry {
        Entry::Occupied(oc_entry) => {
            assert_eq!(oc_entry.get(), &12);
        }
        Entry::Vacant(_) => panic!("Something go wrong!!!")
    }
}
Source

pub fn get_mut(&mut self) -> &mut V

Gets a mutable reference to the value in the entry.

If you need a reference to the OccupiedEntry which may outlive the destruction of the Entry value, see into_mut.

§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;

let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 12);
assert_eq!(map.get_key1("poneyland"), Some(&12));
assert_eq!(map.get_key2(&0),          Some(&12));

if let Ok(entry) = map.entry("poneyland", 0) {
    match entry {
        Entry::Occupied(mut oc_entry) => {
            *oc_entry.get_mut() += 10;
            assert_eq!(oc_entry.get(), &22);

            // We can use the same Entry multiple times.
            *oc_entry.get_mut() += 2;
        }
        Entry::Vacant(_) => panic!("Something go wrong!!!")
    }
}
assert_eq!(map.get_key1("poneyland"), Some(&24));
assert_eq!(map.get_key2(&0),          Some(&24));
Source

pub fn into_mut(self) -> &'a mut V

Converts the OccupiedEntry into a mutable reference to the value in the entry with a lifetime bound to the map itself.

§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;

let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 12);
assert_eq!(map.get_key1("poneyland"), Some(&12));
assert_eq!(map.get_key2(&0),          Some(&12));

// Let's create a variable that outlives the OccupiedEntry (with some initial value)
let mut value: &mut i32 = &mut 0;

if let Ok(entry) = map.entry("poneyland", 0) {
    match entry {
        Entry::Occupied(oc_entry) => {
            // So we can convert the OccupiedEntry into a mutable reference to the value.
            value = oc_entry.into_mut();
            *value += 10;
        }
        Entry::Vacant(_) => panic!("Something go wrong!!!")
    }
}
// We can use the same reference outside the created oc_entry (OccupiedEntry) scope.
*value += 20;
assert_eq!(map.get_key1("poneyland"), Some(&42)); // 12 + 10 + 20
assert_eq!(map.get_key2(&0),          Some(&42));
Source

pub fn insert(&mut self, value: V) -> V

Sets the value of the entry, and returns the entry’s old value.

§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;

let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 12);
assert_eq!(map.get_key1("poneyland"), Some(&12));
assert_eq!(map.get_key2(&0),          Some(&12));

// Let's create a variable that holds value
let mut owner: i32 = 100;

if let Ok(entry) = map.entry("poneyland", 0) {
    match entry {
        Entry::Occupied(mut oc_entry) => {
            // So we can swap our created owner value with value inside the map.
            owner = oc_entry.insert(owner);
        }
        Entry::Vacant(_) => panic!("Something go wrong!!!")
    }
}
assert_eq!(owner, 12);
assert_eq!(map.get_key1("poneyland"), Some(&100));
assert_eq!(map.get_key2(&0),          Some(&100));
Source

pub fn remove(self) -> V

Take the value out of the entry (map), and return it. Keeps the allocated memory for reuse.

§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;

// So lets create some map and insert some element
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 10);
map.insert("bearland",  1, 11);

// And also reserve some space for additional elements
map.reserve(15);
// Now our map can hold at least 17 elements
let capacity_before_remove = map.capacity();
assert!(capacity_before_remove >= 17);

assert!(map.get_key1("poneyland") == Some(&10) && map.get_key2(&0) == Some(&10));
if let Ok(entry) = map.entry("poneyland", 0) {
    match entry {
        Entry::Occupied(oc_entry) => {
            // We delete the entry from the map.
            let value = oc_entry.remove();
            assert_eq!(value, 10);
        }
        Entry::Vacant(_) => panic!("Something go wrong!!!")
    }
}
assert!(map.get_key1("poneyland") == None && map.get_key2(&0) == None);

assert!(map.get_key1("bearland") == Some(&11) && map.get_key2(&1) == Some(&11));
if let Ok(entry) = map.entry("bearland", 1) {
    match entry {
        Entry::Occupied(oc_entry) => {
            // We delete the entry from the map.
            let value = oc_entry.remove();
            assert_eq!(value, 11);
        }
        Entry::Vacant(_) => panic!("Something go wrong!!!")
    }
}
assert!(map.get_key1("bearland") == None && map.get_key2(&1) == None);

// But the capacity of our map isn't changed and still equals to the capacity before
// using `remove_entry` method
assert_eq!(map.capacity(), capacity_before_remove);

Trait Implementations§

Source§

impl<'a, K1: Debug + 'a, K2: Debug + 'a, V: Debug + 'a> Debug for OccupiedEntry<'a, K1, K2, V>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, K1, K2, V> Freeze for OccupiedEntry<'a, K1, K2, V>

§

impl<'a, K1, K2, V> RefUnwindSafe for OccupiedEntry<'a, K1, K2, V>

§

impl<'a, K1, K2, V> Send for OccupiedEntry<'a, K1, K2, V>
where K1: Send, K2: Send, V: Send,

§

impl<'a, K1, K2, V> Sync for OccupiedEntry<'a, K1, K2, V>
where K1: Sync, K2: Sync, V: Sync,

§

impl<'a, K1, K2, V> Unpin for OccupiedEntry<'a, K1, K2, V>

§

impl<'a, K1, K2, V> !UnwindSafe for OccupiedEntry<'a, K1, K2, 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> 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, 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.