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

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!!!")
    }
}

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!!!")
    }
}

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

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!!!")
    }
}

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

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

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

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

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.