pub struct Ptr { /* private fields */ }
Expand description
A pointer type used to identify entries in the linked hash map.
This is an opaque handle that can be used to directly access entries without key lookup. It provides O(1) access to entries.
By default, Ptr
is non-generational, meaning that once an entry is
removed, the pointer may be re-used for a new entry. With the generational
feature enabled, Ptr
includes generation tracking that will panic or
return None when attempting to use a stale pointer after its entry has been
removed.
§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;
use tether_map::Ptr;
let mut map = LinkedHashMap::new();
let ptr = match map.entry("key") {
Entry::Vacant(entry) => entry.insert_tail(42).0,
Entry::Occupied(entry) => entry.ptr(),
};
// Use the pointer for direct access
assert_eq!(map.ptr_get(ptr), Some(&42));
// Remove the entry
map.remove(&"key");
// Using the stale pointer is a logic error but will not panic
assert_eq!(map.ptr_get(ptr), None);
// Insert a new entry, which may reuse the same Ptr value
map.insert("key", 100);
// The old pointer is stale, but may point to the new entry by coincidence
// This may work or not depending on whether the same Ptr value was reused:
// assert_eq!(map.ptr_get(ptr), Some(100));
With the generational
feature enabled, using a stale pointer will return
None or panic:
use tether_map::Entry;
use tether_map::LinkedHashMap;
use tether_map::Ptr;
let mut map = LinkedHashMap::new();
let ptr = match map.entry("key") {
Entry::Vacant(entry) => entry.insert_tail(42).0,
Entry::Occupied(entry) => entry.ptr(),
};
// Remove the entry
map.remove(&"key");
// Using the stale pointer will return None or panic
assert_eq!(map.ptr_get(ptr), None);
// Insert a new entry, which may reuse the same Ptr value
map.insert("key", 100);
// The old pointer is stale, so this will definitely return None
assert_eq!(map.ptr_get(ptr), None);
Trait Implementations§
Source§impl<K, T, S> Index<Ptr> for LinkedHashMap<K, T, S>
impl<K, T, S> Index<Ptr> for LinkedHashMap<K, T, S>
Source§impl<K, T, S> IndexMut<Ptr> for LinkedHashMap<K, T, S>
impl<K, T, S> IndexMut<Ptr> for LinkedHashMap<K, T, S>
Source§impl Ord for Ptr
impl Ord for Ptr
Source§impl PartialOrd for Ptr
impl PartialOrd for Ptr
impl Copy for Ptr
impl Eq for Ptr
impl StructuralPartialEq for Ptr
Auto Trait Implementations§
impl Freeze for Ptr
impl RefUnwindSafe for Ptr
impl Send for Ptr
impl Sync for Ptr
impl Unpin for Ptr
impl UnwindSafe for Ptr
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more