Struct UnsafeIter

pub struct UnsafeIter<'a, T> { /* private fields */ }

An unsafe iterator over the entries of a HashTable in arbitrary order. The iterator element type is NonNull<T>.

This struct is created by the unsafe_iter method on HashTable.

This is used for implementations of iterators with "mixed" mutability on the iterated elements. For example, a mutable iterator for a map may return an immutable key alongside a mutable value, even though these are both stored inside the table.

If you have no idea what any of this means, you probably should be using IterMut instead, as it does not have any safety requirements.

Safety

In order to correctly use this iterator, it should be wrapped in a safe iterator struct with the appropriate PhantomData marker to indicate the correct variance.

For example, below is a simplified hash_map::IterMut implementation that correctly returns a covariant key, and an invariant value:

use core::marker::PhantomData;
use hashbrown::hash_table;

pub struct IterMut<'a, K, V> {
    inner: hash_table::UnsafeIter<'a, (K, V)>,
    // Covariant over keys, invariant over values
    marker: PhantomData<(&'a K, &'a mut V)>,
}
impl<'a, K, V> Iterator for IterMut<'a, K, V> {
    // Immutable keys, mutable values
    type Item = (&'a K, &'a mut V);

    fn next(&mut self) -> Option<Self::Item> {
        // SAFETY: The lifetime of the dereferenced pointer is derived from
        // the lifetime of its iterator, ensuring that it's always valid.
        // Additionally, we match the mutability in `self.marker` to ensure
        // the correct variance.
        let &mut (ref key, ref mut val) = unsafe { self.inner.next()?.as_mut() };
        Some((key, val))
    }
}

Implementations

impl<'a, T> UnsafeIter<'a, T>

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

Returns a iterator of references over the remaining items.

Trait Implementations

impl<'a, T> Iterator for UnsafeIter<'a, T>

type Item = NonNull<T>;
fn next(&mut self) -> Option<Self::Item>
fn size_hint(&self) -> (usize, Option<usize>)
fn fold<B, F>(self, init: B, f: F) -> B
where
    Self: Sized,
    F: FnMut(B, Self::Item) -> B,

impl<T> Debug for UnsafeIter<'_, T> where T: Debug,

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

impl<T> Default for UnsafeIter<'_, T>

fn default() -> Self

impl<T> ExactSizeIterator for UnsafeIter<'_, T>

fn len(&self) -> usize

impl<T> FusedIterator for UnsafeIter<'_, T>

Auto Trait Implementations

impl<'a, T> Freeze for UnsafeIter<'a, T> where RawIter<T>: Freeze,

impl<'a, T> RefUnwindSafe for UnsafeIter<'a, T> where RawIter<T>: RefUnwindSafe,

impl<'a, T> Send for UnsafeIter<'a, T> where RawIter<T>: Send,

impl<'a, T> Sync for UnsafeIter<'a, T> where RawIter<T>: Sync,

impl<'a, T> Unpin for UnsafeIter<'a, T> where RawIter<T>: Unpin,

impl<'a, T> UnsafeUnpin for UnsafeIter<'a, T> where RawIter<T>: UnsafeUnpin,

impl<'a, T> UnwindSafe for UnsafeIter<'a, T> where RawIter<T>: UnwindSafe,

Blanket Implementations

impl<I> IntoIterator for UnsafeIter<'a, T> where I: Iterator,

type Item = <I as Iterator>::Item;
type IntoIter = I;
fn into_iter(self) -> I

impl<T> Any for UnsafeIter<'a, T> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for UnsafeIter<'a, T> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for UnsafeIter<'a, T> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for UnsafeIter<'a, T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for UnsafeIter<'a, T> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for UnsafeIter<'a, T> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for UnsafeIter<'a, T> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>