CursorMut

Struct CursorMut 

Source
pub struct CursorMut<'m, K, T, S> { /* private fields */ }
Expand description

A cursor for navigating and modifying a linked hash map.

A CursorMut is like an iterator, except that it can freely seek back-and-forth and can safely mutate the map during iteration. This is because the lifetime is tied to the map, not the cursor itself.

Cursors always point to an element in the map. The CursorMut is positioned at a specific entry and allows for insertion and removal operations relative to that position.

§Examples

use tether_map::LinkedHashMap;

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

let mut cursor = map.head_cursor_mut();
if let Some((key, value)) = cursor.current_mut() {
    *value *= 10;
}
assert_eq!(map.get(&"a"), Some(&10));

Implementations§

Source§

impl<'m, K: Hash + Eq, T, S: BuildHasher> CursorMut<'m, K, T, S>

Source

pub fn insert_after_move_to(&mut self, key: K, value: T) -> Option<T>

Inserts a key-value pair after the cursor’s current position and moves the cursor to the inserted or updated entry.

Source

pub fn insert_before_move_to(&mut self, key: K, value: T) -> Option<T>

Inserts a key-value pair before the cursor’s current position and moves the cursor to the inserted or updated entry.

Source

pub fn get_ptr(&self, key: &K) -> Option<Ptr>

Returns the pointer to the entry with the given key.

This is a convenience method that delegates to the underlying map’s get_ptr method.

§Arguments
  • key - The key to search for
§Returns
  • Some(ptr) if the key exists in the map
  • None if the key is not found
Source§

impl<'m, K: Hash + Eq, T, S: BuildHasher> CursorMut<'m, K, T, S>

Source

pub fn remove_prev(&mut self) -> Option<(Ptr, RemovedEntry<K, T>)>

Removes the entry before the cursor’s current position and returns it.

Source

pub fn remove_next(&mut self) -> Option<(Ptr, RemovedEntry<K, T>)>

Removes the entry after the cursor’s current position and returns it.

Source

pub fn remove(self) -> Option<(Ptr, RemovedEntry<K, T>)>

Removes the entry at the cursor’s current position and returns it.

Source§

impl<'m, K, T, S> CursorMut<'m, K, T, S>

Source

pub fn iter(&self) -> Iter<'_, K, T>

Returns an iterator starting from the cursor’s current position.

Source

pub fn move_next(&mut self)

Moves the cursor to the next entry in the linked list. The internal linked list is circular, so moving next from the tail wraps around to the head.

Source

pub fn move_prev(&mut self)

Moves the cursor to the previous entry in the linked list. The internal linked list is circular, so moving previous from the head wraps around to the tail.

Source

pub fn ptr(&self) -> Option<Ptr>

Gets the current pointer of the cursor.

Source

pub fn at_tail(&self) -> bool

Checks if the cursor is currently at the tail of the linked list.

Source

pub fn at_head(&self) -> bool

Checks if the cursor is currently at the head of the linked list.

Source

pub fn current(&self) -> Option<(&K, &T)>

Returns the entry at the cursor’s current position.

Source

pub fn current_mut(&mut self) -> Option<(&K, &mut T)>

Returns a mutable reference to the key-value pair at the cursor’s current position.

The key reference is immutable while the value reference is mutable, allowing modification of the value while preserving the key’s integrity.

§Returns
  • Some((&K, &mut T)) if the cursor is positioned at a valid entry
  • None if the cursor is positioned at a null entry
§Examples
use tether_map::LinkedHashMap;

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

let mut cursor = map.head_cursor_mut();
if let Some((key, value)) = cursor.current_mut() {
    assert_eq!(key, &"key");
    *value = 100;
}
assert_eq!(map.get(&"key"), Some(&100));
Source

pub fn next_ptr(&self) -> Option<Ptr>

Returns the pointer to the next entry in the linked list from the cursor’s position.

§Returns
  • Some(next_ptr) if there is a next entry
  • None if the cursor is positioned at a null entry
§Examples
use tether_map::LinkedHashMap;

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

let cursor = map.head_cursor_mut();
if let Some(next_ptr) = cursor.next_ptr() {
    assert_eq!(map.ptr_get(next_ptr), Some(&2));
}
Source

pub fn next(&self) -> Option<(&K, &T)>

Returns a reference to the key-value pair of the next entry in the linked list.

This is a convenience method that combines next_ptr() and accessing the entry.

§Returns
  • Some((&K, &T)) if there is a next entry
  • None if the cursor is positioned at a null entry
§Examples
use tether_map::LinkedHashMap;

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

let cursor = map.head_cursor_mut();
if let Some((key, value)) = cursor.next() {
    assert_eq!(key, &"b");
    assert_eq!(value, &2);
}
Source

pub fn next_mut(&mut self) -> Option<(&K, &mut T)>

Returns a mutable reference to the key-value pair of the next entry in the linked list.

This is a convenience method that combines next_ptr() and accessing the entry mutably. The key reference is immutable while the value reference is mutable.

§Returns
  • Some((&K, &mut T)) if there is a next entry
  • None if the cursor is positioned at a null entry
§Examples
use tether_map::LinkedHashMap;

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

let mut cursor = map.head_cursor_mut();
if let Some((key, value)) = cursor.next_mut() {
    assert_eq!(key, &"b");
    *value = 20;
}
assert_eq!(map.get(&"b"), Some(&20));
Source

pub fn prev_ptr(&self) -> Option<Ptr>

Returns the pointer to the previous entry in the linked list from the cursor’s position.

§Returns
  • Some(prev_ptr) if there is a previous entry
  • None if the cursor positioned at a null entry
§Examples
use tether_map::LinkedHashMap;

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

let cursor = map.tail_cursor_mut();
if let Some(prev_ptr) = cursor.prev_ptr() {
    assert_eq!(map.ptr_get(prev_ptr), Some(&1));
}
Source

pub fn prev(&self) -> Option<(&K, &T)>

Returns a reference to the key-value pair of the previous entry in the linked list.

This is a convenience method that combines prev_ptr() and accessing the entry.

§Returns
  • Some((&K, &T)) if there is a previous entry
  • None if the cursor is positioned at a null entry
§Examples
use tether_map::LinkedHashMap;

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

let cursor = map.tail_cursor_mut();
if let Some((key, value)) = cursor.prev() {
    assert_eq!(key, &"a");
    assert_eq!(value, &1);
}
Source

pub fn prev_mut(&mut self) -> Option<(&K, &mut T)>

Returns a mutable reference to the key-value pair of the previous entry in the linked list.

This is a convenience method that combines prev_ptr() and accessing the entry mutably. The key reference is immutable while the value reference is mutable.

§Returns
  • Some((&K, &mut T)) if there is a previous entry
  • None if the cursor is positioned at a null entry
§Examples
use tether_map::LinkedHashMap;

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

let mut cursor = map.tail_cursor_mut();
if let Some((key, value)) = cursor.prev_mut() {
    assert_eq!(key, &"a");
    *value = 10;
}
assert_eq!(map.get(&"a"), Some(&10));

Trait Implementations§

Source§

impl<'m, K: Debug, T: Debug, S: Debug> Debug for CursorMut<'m, K, T, S>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'m, K, T, S> Freeze for CursorMut<'m, K, T, S>

§

impl<'m, K, T, S> !RefUnwindSafe for CursorMut<'m, K, T, S>

§

impl<'m, K, T, S> !Send for CursorMut<'m, K, T, S>

§

impl<'m, K, T, S> !Sync for CursorMut<'m, K, T, S>

§

impl<'m, K, T, S> Unpin for CursorMut<'m, K, T, S>

§

impl<'m, K, T, S> !UnwindSafe for CursorMut<'m, K, T, S>

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.