scc::hash_index

Struct OccupiedEntry

Source
pub struct OccupiedEntry<'h, K, V, H = RandomState>
where H: BuildHasher,
{ /* private fields */ }
Expand description

OccupiedEntry is a view into an occupied entry in a HashIndex.

Implementations§

Source§

impl<'h, K, V, H> OccupiedEntry<'h, K, V, H>
where K: 'static + Clone + Eq + Hash, V: 'static + Clone, H: BuildHasher,

Source

pub fn key(&self) -> &K

Gets a reference to the key in the entry.

§Examples
use scc::HashIndex;

let hashindex: HashIndex<u64, u32> = HashIndex::default();

assert_eq!(hashindex.entry(29).or_default().key(), &29);
Source

pub fn remove_entry(self)

Marks that the entry is removed from the HashIndex.

§Examples
use scc::HashIndex;
use scc::hash_index::Entry;

let hashindex: HashIndex<u64, u32> = HashIndex::default();

hashindex.entry(11).or_insert(17);

if let Entry::Occupied(o) = hashindex.entry(11) {
    o.remove_entry();
};
assert_eq!(hashindex.peek_with(&11, |_, v| *v), None);
Source

pub fn get(&self) -> &V

Gets a reference to the value in the entry.

§Examples
use scc::HashIndex;
use scc::hash_index::Entry;

let hashindex: HashIndex<u64, u32> = HashIndex::default();

hashindex.entry(19).or_insert(11);

if let Entry::Occupied(o) = hashindex.entry(19) {
    assert_eq!(o.get(), &11);
};
Source

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

Gets a mutable reference to the value in the entry.

§Safety

The caller has to make sure that there are no readers of the entry, e.g., a reader keeping a reference to the entry via HashIndex::iter, HashIndex::peek, or HashIndex::peek_with, unless an instance of V can be safely read when there is a single writer, e.g., V = [u8; 32].

§Examples
use scc::HashIndex;
use scc::hash_index::Entry;

let hashindex: HashIndex<u64, u32> = HashIndex::default();

hashindex.entry(37).or_insert(11);

if let Entry::Occupied(mut o) = hashindex.entry(37) {
    // Safety: `u32` can be safely read while being modified.
    unsafe { *o.get_mut() += 18; }
    assert_eq!(*o.get(), 29);
}

assert_eq!(hashindex.peek_with(&37, |_, v| *v), Some(29));
Source

pub fn update(self, val: V)

Updates the entry by inserting a new entry and marking the existing entry removed.

§Examples
use scc::HashIndex;
use scc::hash_index::Entry;

let hashindex: HashIndex<u64, u32> = HashIndex::default();

hashindex.entry(37).or_insert(11);

if let Entry::Occupied(mut o) = hashindex.entry(37) {
    o.update(29);
}

assert_eq!(hashindex.peek_with(&37, |_, v| *v), Some(29));
Source

pub fn next(self) -> Option<Self>

Gets the next closest occupied entry.

HashIndex::first_entry, HashIndex::first_entry_async, and this method together enables the OccupiedEntry to effectively act as a mutable iterator over entries. The method never acquires more than one lock even when it searches other buckets for the next closest occupied entry.

§Examples
use scc::HashIndex;
use scc::hash_index::Entry;

let hashindex: HashIndex<u64, u32> = HashIndex::default();

assert!(hashindex.insert(1, 0).is_ok());
assert!(hashindex.insert(2, 0).is_ok());

let first_entry = hashindex.first_entry().unwrap();
let first_key = *first_entry.key();
let second_entry = first_entry.next().unwrap();
let second_key = *second_entry.key();

assert!(second_entry.next().is_none());
assert_eq!(first_key + second_key, 3);
Source

pub async fn next_async(self) -> Option<OccupiedEntry<'h, K, V, H>>

Gets the next closest occupied entry.

HashIndex::first_entry, HashIndex::first_entry_async, and this method together enables the OccupiedEntry to effectively act as a mutable iterator over entries. The method never acquires more than one lock even when it searches other buckets for the next closest occupied entry.

It is an asynchronous method returning an impl Future for the caller to await.

§Examples
use scc::HashIndex;
use scc::hash_index::Entry;

let hashindex: HashIndex<u64, u32> = HashIndex::default();

assert!(hashindex.insert(1, 0).is_ok());
assert!(hashindex.insert(2, 0).is_ok());

let second_entry_future = hashindex.first_entry().unwrap().next_async();

Trait Implementations§

Source§

impl<'h, K, V, H> Debug for OccupiedEntry<'h, K, V, H>
where K: 'static + Clone + Debug + Eq + Hash, V: 'static + Clone + Debug, H: BuildHasher,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'h, K, V, H> Deref for OccupiedEntry<'h, K, V, H>
where K: 'static + Clone + Debug + Eq + Hash, V: 'static + Clone + Debug, H: BuildHasher,

Source§

type Target = V

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<'h, K, V, H> Freeze for OccupiedEntry<'h, K, V, H>

§

impl<'h, K, V, H> RefUnwindSafe for OccupiedEntry<'h, K, V, H>

§

impl<'h, K, V, H> Send for OccupiedEntry<'h, K, V, H>
where K: Eq + Hash + Send + Sync, V: Send + Sync, H: Sync,

§

impl<'h, K, V, H> Sync for OccupiedEntry<'h, K, V, H>
where H: Sync, K: Sync, V: Sync,

§

impl<'h, K, V, H> Unpin for OccupiedEntry<'h, K, V, H>

§

impl<'h, K, V, H = RandomState> !UnwindSafe for OccupiedEntry<'h, K, V, H>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.