Struct flashmap::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 WriteGuard
s which allow for
mutation of the underlying map.
Implementations
sourceimpl<K, V, S> WriteHandle<K, V, S> where
K: Hash + Eq,
S: BuildHasher,
impl<K, V, S> WriteHandle<K, V, S> where
K: Hash + Eq,
S: BuildHasher,
sourcepub fn synchronize(&self)
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.
sourcepub fn guard(&mut self) -> View<WriteGuard<'_, K, V, S>>
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"));
sourcepub fn reclaim_one(&self, leaked: Leaked<V>) -> V
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");
sourcepub fn reclaimer(&self) -> impl Fn(Leaked<V>) -> V + '_
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
sourceimpl<K, V, S> Drop for WriteHandle<K, V, S> where
K: Hash + Eq,
S: BuildHasher,
impl<K, V, S> Drop for WriteHandle<K, V, S> where
K: Hash + Eq,
S: BuildHasher,
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> !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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more