Struct WriteHandle

Source
pub struct WriteHandle<K, V, S = RandomState>
where K: Hash + Eq, S: BuildHasher,
{ /* private fields */ }
Expand description

A write handle to the underlying map.

This type allows for the creation of WriteGuards which allow for mutation of the underlying map.

Implementations§

Source§

impl<K, V, S> WriteHandle<K, V, S>
where K: Hash + Eq, S: BuildHasher,

Source

pub fn synchronize(&self)

Blocks the calling thread until all readers see the same version of the map.

If all readers already see the same version of the map (or if there are no active readers) then this function is a no-op.

This function is meant for advanced use only. See Leaked::into_inner for an example use-case.

Source

pub fn guard(&mut self) -> View<WriteGuard<'_, K, V, S>>

Creates a new WriteGuard wrapped in a View, allowing for safe read and write access to the map.

§Examples
let (mut write, read) = flashmap::new::<String, String>();

let mut guard = write.guard();

// Insert a few values
guard.insert("apple".to_owned(), "red".to_owned());
guard.insert("banana".to_owned(), "yellow".to_owned());

// Remove a value
assert_eq!(&*guard.remove("apple".to_owned()).unwrap(), "red");

// Publishing makes all previous changes visible to new readers. Dropping the
// guard has the same effect.
guard.publish();

Unlike a read guard, when reading through a write guard, all changes will be immediately visible.

let (mut write, read) = flashmap::new::<String, String>();

let mut guard = write.guard();

// Our insert is immediately visible to us
guard.insert("apple".to_owned(), "red".to_owned());
assert_eq!(guard.get("apple").unwrap(), "red");
assert!(!guard.contains_key("banana"));

guard.insert("banana".to_owned(), "yellow".to_owned());
assert_eq!(guard.get("banana").unwrap(), "yellow");

// Likewise, removes (and all other operations) are immediately visible
assert_eq!(&*guard.remove("apple".to_owned()).unwrap(), "red");
assert!(!guard.contains_key("apple"));
Source

pub fn reclaim_one(&self, leaked: Leaked<V>) -> V

Reclaims a leaked value, providing ownership of the underlying value.

§Panics

Panics if the leaked value provided came from a different map then the one this handle is associated with.

§Examples
use flashmap::{self, Evicted};

let (mut write, read) = flashmap::new::<String, String>();

write.guard().insert("ferris".to_owned(), "crab".to_owned());

// ~~ stuff happens ~~

let leaked = write.guard().remove("ferris".to_owned())
    .map(Evicted::leak)
    .unwrap();

let value = write.reclaim_one(leaked);
assert_eq!(value, "crab");
Source

pub fn reclaimer(&self) -> impl Fn(Leaked<V>) -> V + '_

Returns a function which can safely reclaim leaked values. This is useful for reclaiming multiple leaked values while only performign the necessary synchronization once.

§Panics

The returned function will panic if given a leaked value not from the map this handle is associated with. This function itself will not panic.

§Examples
use flashmap::{self, Evicted};

let (mut write, read) = flashmap::new::<u32, String>();

let mut guard = write.guard();
guard.insert(0xFF0000, "red".to_owned());
guard.insert(0x00FF00, "green".to_owned());
guard.insert(0x0000FF, "blue".to_owned());
guard.publish();

// ~~ stuff happens ~~

let mut guard = write.guard();
let colors = [0xFF0000, 0x00FF00, 0x0000FF].map(|hex| {
    guard.remove(hex).map(Evicted::leak).unwrap()
});
guard.publish();

let [red, green, blue] = colors.map(write.reclaimer());

assert_eq!(red, "red");
assert_eq!(green, "green");
assert_eq!(blue, "blue");

Trait Implementations§

Source§

impl<K, V, S> Drop for WriteHandle<K, V, S>
where K: Hash + Eq, S: BuildHasher,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<K, V, S> Send for WriteHandle<K, V, S>
where K: Send + Sync + Hash + Eq, V: Send + Sync, S: Send + Sync + BuildHasher,

Auto Trait Implementations§

§

impl<K, V, S = RandomState> !Freeze for WriteHandle<K, V, S>

§

impl<K, V, S = RandomState> !RefUnwindSafe for WriteHandle<K, V, S>

§

impl<K, V, S = RandomState> !Sync for WriteHandle<K, V, S>

§

impl<K, V, S> Unpin for WriteHandle<K, V, S>
where K: Unpin, V: Unpin,

§

impl<K, V, S = RandomState> !UnwindSafe for WriteHandle<K, V, 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.