Entry

Enum Entry 

Source
pub enum Entry<'a, K1: 'a, K2: 'a, V: 'a> {
    Occupied(OccupiedEntry<'a, K1, K2, V>),
    Vacant(VacantEntry<'a, K1, K2, V>),
}
Expand description

A view into a single entry in a map, which may either be vacant or occupied.

This enum is constructed from the entry method on DHashMap.

Variants§

§

Occupied(OccupiedEntry<'a, K1, K2, V>)

An occupied entry.

§

Vacant(VacantEntry<'a, K1, K2, V>)

A vacant entry.

Implementations§

Source§

impl<'a, K1: Clone, K2: Clone, V> Entry<'a, K1, K2, V>

Source

pub fn or_insert(self, default: V) -> &'a mut V

Ensures a value is in the entry by inserting the default if empty, and returns a mutable reference to the value in the entry.

§Examples
use double_map::DHashMap;

let mut map: DHashMap<&str, u32, i32> = DHashMap::new();

match map.entry("poneyland", 0) {
    Ok(entry) => {
        entry.or_insert(3);
    }
    Err(_) => unreachable!(),
}
assert_eq!(map.get_key1(&"poneyland"), Some(&3));

map.entry("poneyland", 0).map(|entry| *entry.or_insert(10) *= 2);
assert_eq!(map.get_key1(&"poneyland"), Some(&6));
Source

pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V

Ensures a value is in the entry by inserting the result of the default function if empty, and returns a mutable reference to the value in the entry.

§Examples
use double_map::DHashMap;

let mut map: DHashMap<&str, u32, String> = DHashMap::new();
let s = "hoho".to_owned();

match map.entry("poneyland", 0) {
    Ok(entry) => {
        entry.or_insert_with(|| s);
    }
    Err(_) => unreachable!(),
}
assert_eq!(map.get_key1("poneyland"), Some(&"hoho".to_owned()));

let k = "another string".to_owned();
map.entry("poneyland", 0).map(|entry| entry.or_insert_with(|| k).push_str("ho"));
assert_eq!(map.get_key1(&"poneyland"), Some(&"hohoho".to_owned()));
Source

pub fn or_insert_with_key1<F: FnOnce(&K1) -> V>(self, default: F) -> &'a mut V

Ensures a value is in the entry by inserting, if empty, the result of the default function. This method allows generating key-derived (with using key # 1 K1) value for insertion by providing the default function a reference to the key that was moved during the .entry(key) method call.

The reference to the moved key is provided so that cloning or copying the key is unnecessary, unlike with .or_insert_with(|| ... ).

§Examples
use double_map::DHashMap;

let mut map: DHashMap<&str, usize, u64> = DHashMap::new();

 match map.entry("poneyland", 0) {
    Ok(entry) => {
        entry.or_insert_with_key1(|k1| k1.chars().count() as u64);
    },
    Err(_) => unreachable!(),
}
assert_eq!(map.get_key1(&"poneyland"), Some(&9));

map.entry("bearland", 1).map(
    |ent| ent.or_insert_with_key1(|k1| (k1.chars().count() * 2) as u64)
);
assert_eq!(map.get_key1(&"bearland"), Some(&16));
Source

pub fn or_insert_with_key2<F: FnOnce(&K2) -> V>(self, default: F) -> &'a mut V

Ensures a value is in the entry by inserting, if empty, the result of the default function. This method allows generating key-derived (with using key # 2 K2) value for insertion by providing the default function a reference to the key that was moved during the .entry(key) method call.

The reference to the moved key is provided so that cloning or copying the key is unnecessary, unlike with .or_insert_with(|| ... ).

§Examples
use double_map::DHashMap;

let mut map: DHashMap<&str, usize, u64> = DHashMap::new();

 match map.entry("poneyland", 10) {
    Ok(entry) => {
        entry.or_insert_with_key2(|k2| (k2 + 10) as u64);
    },
    Err(_) => unreachable!(),
}
assert_eq!(map.get_key1(&"poneyland"), Some(&20));

map.entry("bearland", 11).map(
    |ent| ent.or_insert_with_key2(|k1| (k1 * 3) as u64)
);
assert_eq!(map.get_key1(&"bearland"), Some(&33));
Source

pub fn or_insert_with_keys<F: FnOnce(&K1, &K2) -> V>( self, default: F, ) -> &'a mut V

Ensures a value is in the entry by inserting, if empty, the result of the default function. This method allows generating key-derived (with using key #1 of type K1 and key # 2 of type K2) values for insertion by providing the default function a reference to the keys that were moved during the .entry(key) method call.

The reference to the moved keys is provided so that cloning or copying the key is unnecessary, unlike with .or_insert_with(|| ... ).

§Examples
use double_map::DHashMap;

let mut map: DHashMap<&str, usize, u64> = DHashMap::new();

 match map.entry("poneyland", 10) {
    Ok(entry) => {
        entry.or_insert_with_keys(|k1, k2| (k1.chars().count() + k2) as u64);
    },
    Err(_) => unreachable!(),
}
assert_eq!(map.get_key1(&"poneyland"), Some(&19));

map.entry("bearland", 11).map(
    |ent| ent.or_insert_with_keys(|k1, k2| (k1.chars().count() + k2 * 3) as u64)
);
assert_eq!(map.get_key1(&"bearland"), Some(&41));
Source§

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

Source

pub fn key1(&self) -> &K1

Returns a reference to this entry’s first key (key #1).

§Examples
use double_map::DHashMap;

let mut map: DHashMap<&str, u32, i32> = DHashMap::new();

// It is VacantEntry
match map.entry("poneyland", 0) {
    Ok(entry) => {
        // key equal to provided one
        assert_eq!(entry.key1(), &"poneyland");
        // we insert some value
        entry.or_insert(25);
    },
    Err(_) => unreachable!(),
}
// As we can see, now this element exists
assert_eq!(map.get_key1(&"poneyland"), Some(&25));

// So now it is OccupiedEntry
match map.entry("poneyland", 0) {
    Ok(entry) => {
        // And key equals to provided one too
        assert_eq!(entry.key1(), &"poneyland");
        entry.or_insert(25);
    },
    Err(_) => unreachable!(),
}
Source

pub fn key2(&self) -> &K2

Returns a reference to this entry’s second key (key #2).

§Examples
use double_map::DHashMap;

let mut map: DHashMap<&str, u32, i32> = DHashMap::new();

// It is VacantEntry
match map.entry("poneyland", 10) {
    Ok(entry) => {
        // key equal to provided one
        assert_eq!(entry.key2(), &10);
        // we insert some value
        entry.or_insert(25);
    },
    Err(_) => unreachable!(),
}
// As we can see, now this element exists
assert_eq!(map.get_key2(&10), Some(&25));

// So now it is OccupiedEntry
match map.entry("poneyland", 10) {
    Ok(entry) => {
        // And key equals to provided one too
        assert_eq!(entry.key2(), &10);
        entry.or_insert(25);
    },
    Err(_) => unreachable!(),
}
Source

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

Returns a reference to this entry’s keys. Return tuple of type (&’a K1, &’a K2).

§Examples
use double_map::DHashMap;

let mut map: DHashMap<&str, u32, i32> = DHashMap::new();

// It is VacantEntry
match map.entry("poneyland", 10) {
    Ok(entry) => {
        // keys equal to provided one
        assert_eq!(entry.keys(), (&"poneyland", &10));
        // we insert some value
        entry.or_insert(25);
    },
    Err(_) => unreachable!(),
}
// As we can see, now this element exists
assert_eq!(map.get_key1(&"poneyland"), Some(&25));

// So now it is OccupiedEntry
match map.entry("poneyland", 10) {
    Ok(entry) => {
        // And keys equal to provided one too
        assert_eq!(entry.keys(), (&"poneyland", &10));
        entry.or_insert(25);
    },
    Err(_) => unreachable!(),
}
Source

pub fn and_modify<F>(self, f: F) -> Self
where F: FnOnce(&mut V),

Provides in-place mutable access to an occupied entry before any potential inserts into the map.

§Examples
use double_map::DHashMap;

let mut map: DHashMap<&str, u32, u32> = DHashMap::new();

map.entry("poneyland", 1).map( |entry|
   entry.and_modify(|value| { *value += 100 })
   .or_insert(42)
);
assert_eq!(map.get_key1(&"poneyland"), Some(&42));

map.entry("poneyland", 1).map( |entry|
   entry.and_modify(|value| { *value += 100 })
   .or_insert(42)
);
assert_eq!(map.get_key1(&"poneyland"), Some(&142));
Source§

impl<'a, K1: Clone, K2: Clone, V: Default> Entry<'a, K1, K2, V>

Source

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

Ensures a value is in the entry by inserting the default value if empty, and returns a mutable reference to the value in the entry.

§Examples
use double_map::DHashMap;

let mut map: DHashMap<&str, usize, Option<u32>> = DHashMap::new();
map.entry("poneyland", 1).map(|entry| entry.or_default());

assert_eq!(map.get_key1(&"poneyland"), Option::<&Option<u32>>::Some(&None));

Trait Implementations§

Source§

impl<'a, K1: Debug + 'a, K2: Debug + 'a, V: Debug + 'a> Debug for Entry<'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 Entry<'a, K1, K2, V>
where K1: Freeze, K2: Freeze,

§

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

§

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

§

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

§

impl<'a, K1, K2, V> Unpin for Entry<'a, K1, K2, V>
where K1: Unpin, K2: Unpin,

§

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