Relay

Struct Relay 

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

Move single values between threads

A condition variable with a single slot that allows to pass values from producer to consumer threads. Producers and consumers may arrive at any point in time.

A typical scenario involves only a single producer and a single consumer thread implementing a handover protocol for passing the latest (= most recent) value between each other.

The value is buffered until the consumer is ready to take it. Each value can be consumed at most once. Producers can replace the current value if it has not been consumed yet.

Implementations§

Source§

impl<T> Relay<T>

Source

pub const fn new() -> Self

Source

pub const fn with_value(value: T) -> Self

Source§

impl<T> Relay<T>

Source

pub fn replace_notify_one(&self, value: T) -> Option<T>

Replace the current value and notify a single waiting consumer

Returns the previous value or None. If None is returned then a notification has been triggered.

Source

pub fn replace_notify_all(&self, value: T) -> Option<T>

Replace the current value and notify all waiting consumers

Returns the previous value or None. If None is returned then a notification has been triggered.

Source

pub fn take(&self) -> Option<T>

Take the current value immediately

Resets the internal state on return.

Returns the previous value or None.

Source

pub fn wait(&self) -> T

Wait for a value and then take it

Resets the internal state on return.

Returns the previous value.

Source

pub fn wait_for(&self, timeout: Duration) -> Option<T>

Wait for a value with a timeout and then take it

Resets the internal state on return, i.e. either takes the value or on timeout the internal value already was None and doesn’t need to be reset.

Returns the value if available or None if the timeout expired.

Source

pub fn wait_until(&self, deadline: Instant) -> Option<T>

Wait for a value until a deadline and then take it

Resets the internal state on return, i.e. either takes the value or on timeout the internal value already was None and doesn’t need to be reset.

Returns the value if available or None if the deadline expired.

Trait Implementations§

Source§

impl<T: Debug> Debug for Relay<T>

Source§

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

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

impl<T> Default for Relay<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Relay<T>

§

impl<T> RefUnwindSafe for Relay<T>

§

impl<T> Send for Relay<T>
where T: Send,

§

impl<T> Sync for Relay<T>
where T: Send,

§

impl<T> Unpin for Relay<T>
where T: Unpin,

§

impl<T> UnwindSafe for Relay<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.