OccupiedEntry

Struct OccupiedEntry 

Source
pub struct OccupiedEntry<'a, K, T> { /* private fields */ }
Expand description

A view into an occupied entry in a LinkedHashMap.

It is part of the Entry enum.

§Examples

use tether_map::Entry;
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("key", "value");

if let Entry::Occupied(entry) = map.entry("key") {
    println!("Found key: {}, value: {}", entry.key(), entry.get());
}

Implementations§

Source§

impl<'a, K, T> OccupiedEntry<'a, K, T>

Source

pub fn get(&self) -> &T

Returns a reference to the value in the entry.

§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("key", 42);

match map.entry("key") {
    Entry::Occupied(entry) => {
        assert_eq!(entry.get(), &42);
    }
    Entry::Vacant(_) => unreachable!(),
}
Source

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

Returns a mutable reference to the value in the entry.

§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("key", 42);

match map.entry("key") {
    Entry::Occupied(mut entry) => {
        *entry.get_mut() = 100;
    }
    Entry::Vacant(_) => unreachable!(),
}
assert_eq!(map.get(&"key"), Some(&100));
Source

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

Consumes the occupied entry and returns a mutable reference to the value.

The returned reference is tied to the lifetime of the original map borrow.

Source

pub fn insert_no_move(self, value: T) -> T

Replaces the entry’s value and returns the old value without moving the entry’s position.

Unlike insert(), this method does not affect the entry’s position in the linked list.

§Arguments
  • value - The new value to insert
§Returns

The previous value that was replaced

§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 1);
map.insert("b", 2);

match map.entry("a") {
    Entry::Occupied(entry) => {
        let old = entry.insert_no_move(10);
        assert_eq!(old, 1);
    }
    Entry::Vacant(_) => unreachable!(),
}

// Order remains: a, b ("a" was not moved)
let entries: Vec<_> = map.iter().collect();
assert_eq!(entries, [(&"a", &10), (&"b", &2)]);
Source

pub fn ptr(&self) -> Ptr

Returns the pointer to this entry.

The pointer can be used for direct access operations or cursor positioning.

§Returns

The pointer to the entry

§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("key", 42);

match map.entry("key") {
    Entry::Occupied(entry) => {
        let ptr = entry.ptr();
        assert_eq!(map.ptr_get(ptr), Some(&42));
    }
    Entry::Vacant(_) => unreachable!(),
}
Source

pub fn key(&self) -> &K

Returns a reference to the key in the entry.

§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("key", 42);

match map.entry("key") {
    Entry::Occupied(entry) => {
        assert_eq!(entry.key(), &"key");
    }
    Entry::Vacant(_) => unreachable!(),
}
Source

pub fn insert(self, value: T) -> T

Replaces the entry’s value and returns the old value.

This is equivalent to insert_no_move() and does not move the entry’s position.

§Arguments
  • value - The new value to insert
§Returns

The previous value that was replaced

§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("key", 42);

match map.entry("key") {
    Entry::Occupied(entry) => {
        let old = entry.insert(100);
        assert_eq!(old, 42);
    }
    Entry::Vacant(_) => unreachable!(),
}
assert_eq!(map.get(&"key"), Some(&100));
Source

pub fn remove_entry(self) -> (K, T)

Removes the entry from the map and returns the key-value pair.

This consumes the occupied entry and requires that both the key and value types implement Default for safe removal from the underlying storage.

§Returns

A tuple containing the key and value that were removed

§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("key", 42);

match map.entry("key") {
    Entry::Occupied(entry) => {
        let (key, value) = entry.remove_entry();
        assert_eq!(key, "key");
        assert_eq!(value, 42);
    }
    Entry::Vacant(_) => unreachable!(),
}
assert_eq!(map.len(), 0);
Source

pub fn remove(self) -> T

Removes the entry from the map and returns the value.

This consumes the occupied entry and requires that both the key and value types implement Default for safe removal from the underlying storage.

§Returns

The value that was removed

§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("key", 42);

match map.entry("key") {
    Entry::Occupied(entry) => {
        let value = entry.remove();
        assert_eq!(value, 42);
    }
    Entry::Vacant(_) => unreachable!(),
}
assert_eq!(map.len(), 0);

Auto Trait Implementations§

§

impl<'a, K, T> Freeze for OccupiedEntry<'a, K, T>

§

impl<'a, K, T> !RefUnwindSafe for OccupiedEntry<'a, K, T>

§

impl<'a, K, T> !Send for OccupiedEntry<'a, K, T>

§

impl<'a, K, T> !Sync for OccupiedEntry<'a, K, T>

§

impl<'a, K, T> Unpin for OccupiedEntry<'a, K, T>

§

impl<'a, K, T> !UnwindSafe for OccupiedEntry<'a, K, T>

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.