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>
impl<'m, K: Hash + Eq, T, S: BuildHasher> CursorMut<'m, K, T, S>
Sourcepub fn insert_after_move_to(&mut self, key: K, value: T) -> Option<T>
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.
Sourcepub fn insert_before_move_to(&mut self, key: K, value: T) -> Option<T>
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§impl<'m, K: Hash + Eq, T, S: BuildHasher> CursorMut<'m, K, T, S>
impl<'m, K: Hash + Eq, T, S: BuildHasher> CursorMut<'m, K, T, S>
Sourcepub fn remove_prev(&mut self) -> Option<(Ptr, RemovedEntry<K, T>)>
pub fn remove_prev(&mut self) -> Option<(Ptr, RemovedEntry<K, T>)>
Removes the entry before the cursor’s current position and returns it.
Sourcepub fn remove_next(&mut self) -> Option<(Ptr, RemovedEntry<K, T>)>
pub fn remove_next(&mut self) -> Option<(Ptr, RemovedEntry<K, T>)>
Removes the entry after the cursor’s current position and returns it.
Sourcepub fn remove(self) -> Option<(Ptr, RemovedEntry<K, T>)>
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>
impl<'m, K, T, S> CursorMut<'m, K, T, S>
Sourcepub fn iter(&self) -> Iter<'_, K, T> ⓘ
pub fn iter(&self) -> Iter<'_, K, T> ⓘ
Returns an iterator starting from the cursor’s current position.
Sourcepub fn move_next(&mut self)
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.
Sourcepub fn move_prev(&mut self)
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.
Sourcepub fn at_tail(&self) -> bool
pub fn at_tail(&self) -> bool
Checks if the cursor is currently at the tail of the linked list.
Sourcepub fn at_head(&self) -> bool
pub fn at_head(&self) -> bool
Checks if the cursor is currently at the head of the linked list.
Sourcepub fn current_mut(&mut self) -> Option<(&K, &mut T)>
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 entryNone
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));
Sourcepub fn next_ptr(&self) -> Option<Ptr>
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 entryNone
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));
}
Sourcepub fn next(&self) -> Option<(&K, &T)>
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 entryNone
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);
}
Sourcepub fn next_mut(&mut self) -> Option<(&K, &mut T)>
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 entryNone
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));
Sourcepub fn prev_ptr(&self) -> Option<Ptr>
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 entryNone
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));
}
Sourcepub fn prev(&self) -> Option<(&K, &T)>
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 entryNone
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);
}
Sourcepub fn prev_mut(&mut self) -> Option<(&K, &mut T)>
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 entryNone
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));