Skip to main content

Producer

Struct Producer 

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

The producing side of a shared state channel.

Producers hold mutable access to the shared value. When the state is modified through Mut, all registered consumers are automatically notified. Cloning a producer increments the producer reference count. When the last producer is dropped, the channel is closed.

Implementations§

Source§

impl<T> Producer<T>

Source

pub fn new(value: T) -> Self

Create a new producer with the given initial value.

Source

pub fn consume(&self) -> Consumer<T>

Create a new Consumer that shares this producer’s state.

Source

pub fn close(&self) -> Result<(), Ref<'_, T>>

Close the channel, notifying all consumers.

Source

pub fn write(&self) -> Result<Mut<'_, T>, Ref<'_, T>>

Acquire mutable access to the shared state.

Returns Ok(Mut) if the channel is open, or Err(Ref) with read-only access if closed. Only locks once.

Source

pub fn poll<F, R>(&self, waiter: &Waiter, f: F) -> Poll<Result<R, Ref<'_, T>>>
where F: FnMut(&mut Mut<'_, T>) -> Poll<R>,

Poll-based mutable access with waker registration.

Calls f with a Mut guard. If f returns Poll::Pending, registers the Waiter for notification when the state next changes. Returns Poll::Ready(Err(Ref)) if the channel is closed.

Source

pub async fn wait<F, R>(&self, f: F) -> Result<R, Ref<'_, T>>
where F: FnMut(&mut Mut<'_, T>) -> Poll<R> + Unpin, R: Unpin,

Wait for the closure to return Poll::Ready, re-polling on each state change.

Returns Ok(R) when the closure returns Poll::Ready, or Err(Ref) with read-only access to the final state if the channel closes first.

Source

pub async fn closed(&self)

Wait until the channel is closed.

Source

pub async fn unused(&self) -> Result<(), Ref<'_, T>>

Wait until all consumers have been dropped.

Returns Ok(()) when no consumers remain, or Err(Ref) if the channel closes first.

Source

pub fn read(&self) -> Ref<'_, T>

Get read-only access to the shared state.

Source

pub fn same_channel(&self, other: &Self) -> bool

Returns true if both producers share the same underlying state.

Source

pub fn weak(&self) -> Weak<T>

Create a Weak reference that doesn’t affect the producer/consumer ref counts.

Trait Implementations§

Source§

impl<T> Clone for Producer<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Producer<T>

Source§

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

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

impl<T: Default> Default for Producer<T>

Source§

fn default() -> Self

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

impl<T> Drop for Producer<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Producer<T>

§

impl<T> RefUnwindSafe for Producer<T>

§

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

§

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

§

impl<T> Unpin for Producer<T>

§

impl<T> UnsafeUnpin for Producer<T>

§

impl<T> UnwindSafe for Producer<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.