WriteHandle

Struct WriteHandle 

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

A handle that may be used to modify the eventually consistent map.

Note that any changes made to the map will not be made visible to readers until publish is called.

When the WriteHandle is dropped, the map is immediately (but safely) taken away from all readers, causing all future lookups to return None.

§Examples

let x = ('x', 42);

let (mut w, r) = evmap::new();

// the map is uninitialized, so all lookups should return None
assert_eq!(r.get(&x.0).map(|rs| rs.len()), None);

w.publish();

// after the first publish, it is empty, but ready
assert_eq!(r.get(&x.0).map(|rs| rs.len()), None);

w.insert(x.0, x);

// it is empty even after an add (we haven't publish yet)
assert_eq!(r.get(&x.0).map(|rs| rs.len()), None);

w.publish();

// but after the swap, the record is there!
assert_eq!(r.get(&x.0).map(|rs| rs.len()), Some(1));
assert_eq!(r.get(&x.0).map(|rs| rs.iter().any(|v| v.0 == x.0 && v.1 == x.1)), Some(true));

Implementations§

Source§

impl<K, V, M, S> WriteHandle<K, V, M, S>
where K: Eq + Hash + Clone, S: BuildHasher + Clone, V: Eq + Hash, M: 'static + Clone,

Source

pub fn publish(&mut self) -> &mut Self

Publish all changes since the last call to publish to make them visible to readers.

This can take some time, especially if readers are executing slow operations, or if there are many of them.

Source

pub fn has_pending(&self) -> bool

Returns true if there are changes to the map that have not yet been exposed to readers.

Source

pub fn set_meta(&mut self, meta: M)

Set the metadata.

Will only be visible to readers after the next call to publish.

Source

pub fn insert(&mut self, k: K, v: V) -> &mut Self

Add the given value to the value-bag of the given key.

The updated value-bag will only be visible to readers after the next call to publish.

Source

pub fn update(&mut self, k: K, v: V) -> &mut Self

Replace the value-bag of the given key with the given value.

Replacing the value will automatically deallocate any heap storage and place the new value back into the SmallVec inline storage. This can improve cache locality for common cases where the value-bag is only ever a single element.

See the doc section on this for more information.

The new value will only be visible to readers after the next call to publish.

Source

pub fn clear(&mut self, k: K) -> &mut Self

Clear the value-bag of the given key, without removing it.

If a value-bag already exists, this will clear it but leave the allocated memory intact for reuse, or if no associated value-bag exists an empty value-bag will be created for the given key.

The new value will only be visible to readers after the next call to publish.

Source

pub fn remove(&mut self, k: K, v: V) -> &mut Self

👎Deprecated since 11.0.0: Renamed to remove_value

Remove the given value from the value-bag of the given key.

The updated value-bag will only be visible to readers after the next call to publish.

Source

pub fn remove_value(&mut self, k: K, v: V) -> &mut Self

Remove the given value from the value-bag of the given key.

The updated value-bag will only be visible to readers after the next call to publish.

Source

pub fn empty(&mut self, k: K) -> &mut Self

👎Deprecated since 11.0.0: Renamed to remove_entry

Remove the value-bag for the given key.

The value-bag will only disappear from readers after the next call to publish.

Source

pub fn remove_entry(&mut self, k: K) -> &mut Self

Remove the value-bag for the given key.

The value-bag will only disappear from readers after the next call to publish.

Source

pub fn purge(&mut self) -> &mut Self

Purge all value-bags from the map.

The map will only appear empty to readers after the next call to publish.

Note that this will iterate once over all the keys internally.

Source

pub unsafe fn retain<F>(&mut self, k: K, f: F) -> &mut Self
where F: FnMut(&V, bool) -> bool + 'static + Send,

Retain elements for the given key using the provided predicate function.

The remaining value-bag will only be visible to readers after the next call to publish

§Safety

The given closure is called twice for each element, once when called, and once on swap. It must retain the same elements each time, otherwise a value may exist in one map, but not the other, leaving the two maps permanently out-of-sync. This is really bad, as values are aliased between the maps, and are assumed safe to free when they leave the map during a publish. Returning true when retain is first called for a value, and false the second time would free the value, but leave an aliased pointer to it in the other side of the map.

The arguments to the predicate function are the current value in the value-bag, and true if this is the first value in the value-bag on the second map, or false otherwise. Use the second argument to know when to reset any closure-local state to ensure deterministic operation.

So, stated plainly, the given closure must return the same order of true/false for each of the two iterations over the value-bag. That is, the sequence of returned booleans before the second argument is true must be exactly equal to the sequence of returned booleans at and beyond when the second argument is true.

Source

pub fn fit(&mut self, k: K) -> &mut Self

Shrinks a value-bag to it’s minimum necessary size, freeing memory and potentially improving cache locality by switching to inline storage.

The optimized value-bag will only be visible to readers after the next call to publish

Source

pub fn fit_all(&mut self) -> &mut Self

Like WriteHandle::fit, but shrinks all value-bags in the map.

The optimized value-bags will only be visible to readers after the next call to publish

Source

pub fn reserve(&mut self, k: K, additional: usize) -> &mut Self

Reserves capacity for some number of additional elements in a value-bag, or creates an empty value-bag for this key with the given capacity if it doesn’t already exist.

Readers are unaffected by this operation, but it can improve performance by pre-allocating space for large value-bags.

Source

pub fn empty_random<'a>( &'a mut self, rng: &mut impl Rng, n: usize, ) -> impl ExactSizeIterator<Item = (&'a K, &'a Values<V, S>)>

Available on crate feature eviction only.

Remove the value-bag for n randomly chosen keys.

This method immediately calls publish to ensure that the keys and values it returns match the elements that will be emptied on the next call to publish. The value-bags will only disappear from readers after the next call to publish.

Methods from Deref<Target = ReadHandle<K, V, M, S>>§

Source

pub fn enter(&self) -> Option<MapReadRef<'_, K, V, M, S>>

Take out a guarded live reference to the read side of the map.

This lets you perform more complex read operations on the map.

While the reference lives, changes to the map cannot be published.

If no publish has happened, or the map has been destroyed, this function returns None.

See MapReadRef.

Source

pub fn len(&self) -> usize

Returns the number of non-empty keys present in the map.

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

Source

pub fn meta(&self) -> Option<ReadGuard<'_, M>>

Get the current meta value.

Source

pub fn get<'rh, Q>(&'rh self, key: &Q) -> Option<ReadGuard<'rh, Values<V, S>>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a guarded reference to the values corresponding to the key.

While the guard lives, changes to the map cannot be published.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Note that not all writes will be included with this read – only those that have been published by the writer. If no publish has happened, or the map has been destroyed, this function returns None.

Source

pub fn get_one<'rh, Q>(&'rh self, key: &Q) -> Option<ReadGuard<'rh, V>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a guarded reference to one value corresponding to the key.

This is mostly intended for use when you are working with no more than one value per key. If there are multiple values stored for this key, there are no guarantees to which element is returned.

While the guard lives, changes to the map cannot be published.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Note that not all writes will be included with this read – only those that have been refreshed by the writer. If no refresh has happened, or the map has been destroyed, this function returns None.

Source

pub fn meta_get<Q>( &self, key: &Q, ) -> Option<(Option<ReadGuard<'_, Values<V, S>>>, M)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a guarded reference to the values corresponding to the key along with the map meta.

While the guard lives, changes to the map cannot be published.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Note that not all writes will be included with this read – only those that have been refreshed by the writer. If no refresh has happened, or the map has been destroyed, this function returns None.

If no values exist for the given key, Some(None, _) is returned.

Source

pub fn was_dropped(&self) -> bool

Returns true if the WriteHandle has been dropped.

Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns true if the map contains any values for the specified key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Source

pub fn contains_value<Q, W>(&self, key: &Q, value: &W) -> bool
where K: Borrow<Q>, Aliased<V, NoDrop>: Borrow<W>, Q: Hash + Eq + ?Sized, W: Hash + Eq + ?Sized, V: Hash + Eq,

Returns true if the map contains the specified value for the specified key.

The key and value may be any borrowed form of the map’s respective types, but Hash and Eq on the borrowed form must match.

Source

pub fn map_into<Map, Collector, Target>(&self, f: Map) -> Collector
where Map: FnMut(&K, &Values<V, S>) -> Target, Collector: FromIterator<Target>,

Read all values in the map, and transform them into a new collection.

Trait Implementations§

Source§

impl<K, V, M, S> Debug for WriteHandle<K, V, M, S>
where K: Eq + Hash + Clone + Debug, S: BuildHasher + Clone, V: Eq + Hash + Debug, M: 'static + Clone + Debug,

Source§

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

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

impl<K, V, M, S> Deref for WriteHandle<K, V, M, S>
where K: Eq + Hash + Clone, S: BuildHasher + Clone, V: Eq + Hash, M: 'static + Clone,

Source§

type Target = ReadHandle<K, V, M, S>

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<K, V, M, S> Extend<(K, V)> for WriteHandle<K, V, M, S>
where K: Eq + Hash + Clone, S: BuildHasher + Clone, V: Eq + Hash, M: 'static + Clone,

Source§

fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more

Auto Trait Implementations§

§

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

§

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

§

impl<K, V, M, S> Send for WriteHandle<K, V, M, S>
where M: Send + Sync, K: Send + Sync, V: Send + Sync, S: Send + Sync,

§

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

§

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

§

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V