Skip to main content

FoldChannel

Struct FoldChannel 

Source
pub struct FoldChannel<Acc, In> { /* private fields */ }
Expand description

A fold-backed channel (0.6 Channel::from_fold / Channel::from_sink): each write folds the value into an accumulator and publishes the new accumulator as a snapshot; read returns the snapshots in order and fold_state the current accumulator. Cloning yields another handle on the same accumulator.

use id_effect::{FoldChannel, Stream};
use id_effect::Never;

let sum = FoldChannel::from_fold(0_u32, |acc, x: u32| acc + x);
let total = sum.consume_stream(Stream::<u32, Never>::from_iterable([1, 2, 3]));
assert_eq!(total.block_on().into_result().ok(), Some(6));

Implementations§

Source§

impl<Acc, In> FoldChannel<Acc, In>
where Acc: Clone + Send + 'static, In: Send + 'static,

Source

pub fn from_fold( init: Acc, step: impl Fn(Acc, In) -> Acc + Send + Sync + 'static, ) -> Self

A channel folding every write into init with step.

Source

pub fn from_sink<E, R>(sink: Sink<Acc, In, E, R>) -> Self
where Acc: Sync,

The fold of a Sink::fold_left sink as a writable channel.

§Panics

If sink is not a fold (Sink::fold_left / Sink::from_fold and the sinks built on them). Other sinks can feed a queue with Sink::to_queue and a Channel.

Source

pub fn write<E, R>(&self, value: In) -> Effect<(), E, R>
where E: Send + 'static, R: Send + Sync + 'static,

Folds value into the accumulator and publishes the new accumulator for readers.

Source

pub fn fold_state<E, R>(&self) -> Effect<Acc, E, R>
where E: Send + 'static, R: Send + Sync + 'static,

The current accumulator.

Source

pub fn read<E, R>(&self) -> Effect<Option<Acc>, E, R>
where E: Send + 'static, R: Send + Sync + 'static,

The next snapshot, in write order (suspending until a write); None once the channel is shut down and every snapshot has been read.

Source

pub fn consume_stream<E, R>( &self, stream: Stream<In, E, R>, ) -> Effect<Acc, E, R>
where E: Send + 'static, R: Send + Sync + 'static,

Folds every element of stream into the channel (as writes), then returns the final accumulator. A stream failure ends the drain with that error.

Source

pub fn shutdown<E, R>(&self) -> Effect<(), E, R>

Ends the snapshot feed: snapshots already published stay readable, then reads return None. Later writes still fold but publish nothing.

Source

pub fn map_out(self, f: impl Fn(Acc) -> Acc + Send + Sync + 'static) -> Self

Maps each snapshot on the read side.

Source

pub fn flat_map_out( self, f: impl Fn(Acc) -> Vec<Acc> + Send + Sync + 'static, ) -> Self

Expands each snapshot on the read side into zero or more values (an empty expansion is skipped).

Trait Implementations§

Source§

impl<Acc, In> Clone for FoldChannel<Acc, In>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<Acc, In> Debug for FoldChannel<Acc, In>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Acc, In> !RefUnwindSafe for FoldChannel<Acc, In>

§

impl<Acc, In> !UnwindSafe for FoldChannel<Acc, In>

§

impl<Acc, In> Freeze for FoldChannel<Acc, In>
where Arc<Mutex<Acc>>: Freeze, Arc<dyn Fn(Acc, In) -> Acc + Sync + Send>: Freeze, Queue<Acc>: Freeze, Arc<Mutex<VecDeque<Acc>>>: Freeze, Arc<dyn Fn(Acc) -> Vec<Acc> + Sync + Send>: Freeze,

§

impl<Acc, In> Send for FoldChannel<Acc, In>
where Arc<Mutex<Acc>>: Send, Arc<dyn Fn(Acc, In) -> Acc + Sync + Send>: Send, Queue<Acc>: Send, Arc<Mutex<VecDeque<Acc>>>: Send, Arc<dyn Fn(Acc) -> Vec<Acc> + Sync + Send>: Send,

§

impl<Acc, In> Sync for FoldChannel<Acc, In>
where Arc<Mutex<Acc>>: Sync, Arc<dyn Fn(Acc, In) -> Acc + Sync + Send>: Sync, Queue<Acc>: Sync, Arc<Mutex<VecDeque<Acc>>>: Sync, Arc<dyn Fn(Acc) -> Vec<Acc> + Sync + Send>: Sync,

§

impl<Acc, In> Unpin for FoldChannel<Acc, In>
where Arc<Mutex<Acc>>: Unpin, Arc<dyn Fn(Acc, In) -> Acc + Sync + Send>: Unpin, Queue<Acc>: Unpin, Arc<Mutex<VecDeque<Acc>>>: Unpin, Arc<dyn Fn(Acc) -> Vec<Acc> + Sync + Send>: Unpin,

§

impl<Acc, In> UnsafeUnpin for FoldChannel<Acc, In>
where Arc<Mutex<Acc>>: UnsafeUnpin, Arc<dyn Fn(Acc, In) -> Acc + Sync + Send>: UnsafeUnpin, Queue<Acc>: UnsafeUnpin, Arc<Mutex<VecDeque<Acc>>>: UnsafeUnpin, Arc<dyn Fn(Acc) -> Vec<Acc> + Sync + Send>: UnsafeUnpin,

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<S> Embed<HNil, HNil> for S

Source§

fn project(&self) -> HNil

The sub-environment.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pipe for T

Source§

fn pipe<F, R>(self, f: F) -> R
where F: FnOnce(Self) -> R,

Applies f to self (F#-style forward pipe).
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.