Struct SharedLocalState

Source
pub struct SharedLocalState<T> { /* private fields */ }
Expand description

Facilitates sharing of some local state with other threads.

§Examples

Maintain a counter of concurrent threads:

use shared_local_state::SharedLocalState;

let sls = SharedLocalState::new(());
assert_eq!(sls.len(), 1);

// adds a new shared state, causing the count to grow to 2
let sls_2 = sls.insert(());

// signal 1
let (tx1, rx1) = std::sync::mpsc::channel::<()>();
// signal 2
let (tx2, rx2) = std::sync::mpsc::channel::<()>();

std::thread::spawn(move || {
    // perform some work with the shared state in another thread
    sls_2.update_and_notify(|state| assert_eq!(*state, ()));

    // wait for signal 1 which lets us clean up
    for _ in rx1 {}

    // remove shared state, causing the number of shared
    // states to drop back to 1.
    drop(sls_2);

    // send signal 2, telling the main thread that we have
    // cleaned up our shared local state.
    drop(tx2);
});

assert_eq!(sls.len(), 2);

// send signal 1, telling the spawned thread they can clean up
drop(tx1);

// wait for signal 2, when we know the spawned thread has cleaned up
for _ in rx2 {}

assert_eq!(sls.len(), 1);

Implementations§

Source§

impl<T> SharedLocalState<T>

Source

pub fn new(state: T) -> SharedLocalState<T>

Create a new shared registry that makes the provided local state visible to other SharedLocalState handles created through the insert method.

If the returned SharedLocalState object is dropped, the shared state will be removed from the shared registry and dropped as well.

Source

pub fn insert(&self, state: T) -> SharedLocalState<T>

Registers some local state for the rest of the SharedLocalState handles to access.

If the returned SharedLocalState object is dropped, the shared state will be removed from the shared registry and dropped.

Source

pub fn len(&self) -> usize

The number of shared states associated with this SharedLocalState. This will always be non-zero because the existence of a single SharedLocalState implies the existence of at least one shared state.

Source

pub fn update_and_notify<F, R>(&self, f: F) -> R
where F: Fn(&T) -> R,

Update the local shared state and notify any other threads that may be waiting on updates via the find_or_wait method. Only makes sense if T is Sync because it must be accessed through an immutable reference. If you want to minimize the underlying Condvar notification effort, or if you are only interested in viewing the shared local state, use access_without_notification instead.

Source

pub fn access_without_notification<F, R>(&self, f: F) -> R
where F: Fn(&T) -> R,

Accesses the shared local state without notifying other threads that may be waiting for updates in concurrent calls to find_or_wait.

Source

pub fn notify_all(&self)

Ensures that any modifications performed via access_without_notification are visible to threads waiting for updates in concurrent calls to find_or_wait.

Source

pub fn find_or_wait<F, R>(&self, f: F) -> R
where F: Fn(&T) -> Option<R>,

Iterates over all shared states until the provided F returns Some(R), which is then returned from this method. If F does not return Some(R) for any shared state, a condition variable is used to avoid spinning until shared states have been modified.

Source

pub fn fold<B, F>(&self, init: B, f: F) -> B
where F: FnMut(B, &T) -> B,

Folds over all shared local states.

Source

pub fn map<B, F, R>(&self, f: F) -> R
where F: FnMut(&T) -> B, R: FromIterator<B>,

Maps over all shared local states.

Source

pub fn filter_map<B, F, R>(&self, f: F) -> R
where F: FnMut(&T) -> Option<B>, R: FromIterator<B>,

Filter-maps over all shared local states.

Trait Implementations§

Source§

impl<T: Debug> Debug for SharedLocalState<T>

Source§

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

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

impl<T> Drop for SharedLocalState<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for SharedLocalState<T>

§

impl<T> !RefUnwindSafe for SharedLocalState<T>

§

impl<T> Send for SharedLocalState<T>
where T: Sync + Send,

§

impl<T> Sync for SharedLocalState<T>
where T: Sync + Send,

§

impl<T> Unpin for SharedLocalState<T>

§

impl<T> !UnwindSafe for SharedLocalState<T>

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.