Trait OccupiedEntry

Source
pub trait OccupiedEntry<'a, K, V> {
    // Required methods
    fn key(&self) -> K;
    fn get(&self) -> &V;
    fn get_mut(&mut self) -> &mut V;
    fn into_mut(self) -> &'a mut V;
    fn insert(&mut self, value: V) -> V;
    fn remove(self) -> V;
}
Expand description

A view into an occupied entry in a Map. It is part of the Entry enum.

Required Methods§

Source

fn key(&self) -> K

Gets a copy of the key in the entry.

§Examples
use fixed_map::{Key, Map};
use fixed_map::map::{Entry, OccupiedEntry};

#[derive(Clone, Copy, Key, Debug, PartialEq)]
enum MyKey {
    First,
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();
map.insert(MyKey::First, 12);

let occupied = match map.entry(MyKey::First) {
    Entry::Occupied(entry) => entry,
    _ => unreachable!(),
};

assert_eq!(occupied.key(), MyKey::First);

Using a composite key:

use fixed_map::{Key, Map};
use fixed_map::map::{Entry, OccupiedEntry};

#[derive(Clone, Copy, Key, Debug, PartialEq)]
enum MyKey {
    First(bool),
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();
map.insert(MyKey::First(false), 12);

let occupied = match map.entry(MyKey::First(false)) {
    Entry::Occupied(entry) => entry,
    _ => unreachable!(),
};

assert_eq!(occupied.key(), MyKey::First(false));
Source

fn get(&self) -> &V

Gets a reference to the value in the entry.

§Examples
use fixed_map::{Key, Map};
use fixed_map::map::{Entry, OccupiedEntry};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First,
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();
map.insert(MyKey::First, 12);

let occupied = match map.entry(MyKey::First) {
    Entry::Occupied(entry) => entry,
    _ => unreachable!(),
};

assert_eq!(occupied.get(), &12);

Using a composite key:

use fixed_map::{Key, Map};
use fixed_map::map::{Entry, OccupiedEntry};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First(bool),
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();
map.insert(MyKey::First(false), 12);

let occupied = match map.entry(MyKey::First(false)) {
    Entry::Occupied(entry) => entry,
    _ => unreachable!(),
};

assert_eq!(occupied.get(), &12);
Source

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 fixed_map::{Key, Map};
use fixed_map::map::{Entry, OccupiedEntry};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First,
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();
map.insert(MyKey::First, 12);

let mut occupied = match map.entry(MyKey::First) {
    Entry::Occupied(entry) => entry,
    _ => unreachable!(),
};

assert_eq!(occupied.get_mut(), &12);

Using a composite key:

use fixed_map::{Key, Map};
use fixed_map::map::{Entry, OccupiedEntry};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First(bool),
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();
map.insert(MyKey::First(false), 12);

let mut occupied = match map.entry(MyKey::First(false)) {
    Entry::Occupied(entry) => entry,
    _ => unreachable!(),
};

*occupied.get_mut() *= 2;
assert_eq!(occupied.get(), &24);
// We can use the same Entry multiple times.
*occupied.get_mut() -= 10;
assert_eq!(occupied.get(), &14);
Source

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.

If you need multiple references to the OccupiedEntry, see get_mut.

§Examples
use fixed_map::{Key, Map};
use fixed_map::map::{Entry, OccupiedEntry};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First,
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();
map.insert(MyKey::First, 12);

if let Entry::Occupied(occupied) = map.entry(MyKey::First) {
    *occupied.into_mut() += 10;
};

assert_eq!(map.get(MyKey::First), Some(&22));

Using a composite key:

use fixed_map::{Key, Map};
use fixed_map::map::{Entry, OccupiedEntry};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First(bool),
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();
map.insert(MyKey::First(false), 12);

if let Entry::Occupied(occupied) = map.entry(MyKey::First(false)) {
    *occupied.into_mut() += 10;
};

assert_eq!(map.get(MyKey::First(false)), Some(&22));
Source

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

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

§Examples
use fixed_map::{Key, Map};
use fixed_map::map::{Entry, OccupiedEntry};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First,
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();
map.insert(MyKey::First, 12);

if let Entry::Occupied(mut occupied) = map.entry(MyKey::First) {
    assert_eq!(occupied.insert(10), 12);
};

assert_eq!(map.get(MyKey::First), Some(&10));

Using a composite key:

use fixed_map::{Key, Map};
use fixed_map::map::{Entry, OccupiedEntry};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First(bool),
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();
map.insert(MyKey::First(false), 12);

if let Entry::Occupied(mut occupied) = map.entry(MyKey::First(false)) {
    assert_eq!(occupied.insert(10), 12);
};

assert_eq!(map.get(MyKey::First(false)), Some(&10));
Source

fn remove(self) -> V

Takes the value out of the entry, and returns it.

§Examples
use fixed_map::{Key, Map};
use fixed_map::map::{Entry, OccupiedEntry};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First,
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();
map.insert(MyKey::First, 12);

if let Entry::Occupied(occupied) = map.entry(MyKey::First) {
    assert_eq!(occupied.remove(), 12);
};

assert_eq!(map.contains_key(MyKey::First), false);

Using a composite key:

use fixed_map::{Key, Map};
use fixed_map::map::{Entry, OccupiedEntry};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First(bool),
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();
map.insert(MyKey::First(true), 12);

if let Entry::Occupied(occupied) = map.entry(MyKey::First(true)) {
    assert_eq!(occupied.remove(), 12);
};

assert_eq!(map.contains_key(MyKey::First(true)), false);

Implementations on Foreign Types§

Source§

impl<'a, K, V> OccupiedEntry<'a, K, V> for OccupiedEntry<'a, K, V, DefaultHashBuilder>
where K: Copy,

Source§

fn key(&self) -> K

Source§

fn get(&self) -> &V

Source§

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

Source§

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

Source§

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

Source§

fn remove(self) -> V

Implementors§