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>
impl<Acc, In> FoldChannel<Acc, In>
Sourcepub fn from_fold(
init: Acc,
step: impl Fn(Acc, In) -> Acc + Send + Sync + 'static,
) -> Self
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.
Sourcepub fn from_sink<E, R>(sink: FoldSink<Acc, In, E, R>) -> Selfwhere
Acc: Sync,
pub fn from_sink<E, R>(sink: FoldSink<Acc, In, E, R>) -> Selfwhere
Acc: Sync,
The fold of a FoldSink (Sink::fold_left / Sink::from_fold and the sinks built
on them) as a writable channel. Other sinks can feed a queue with Sink::to_queue and a
Channel.
use id_effect::{FoldChannel, Sink};
// `collect_all_while` is not a fold.
let _ = FoldChannel::from_sink(Sink::<Vec<u32>, u32>::collect_all_while(|_| true));Sourcepub fn write<E, R>(&self, value: In) -> Effect<(), E, R>
pub fn write<E, R>(&self, value: In) -> Effect<(), E, R>
Folds value into the accumulator and publishes the new accumulator for readers.
Sourcepub fn fold_state<E, R>(&self) -> Effect<Acc, E, R>
pub fn fold_state<E, R>(&self) -> Effect<Acc, E, R>
The current accumulator.
Sourcepub fn read<E, R>(&self) -> Effect<Option<Acc>, E, R>
pub fn read<E, R>(&self) -> Effect<Option<Acc>, E, R>
The next snapshot, in write order (suspending until a write); None once the channel is
shut down and every snapshot has been read.
Sourcepub fn consume_stream<E, R>(
&self,
stream: Stream<In, E, R>,
) -> Effect<Acc, E, R>
pub fn consume_stream<E, R>( &self, stream: Stream<In, E, R>, ) -> Effect<Acc, E, R>
Folds every element of stream into the channel (as writes), then
returns the final accumulator. A stream failure ends the drain with that error.
Sourcepub fn shutdown<E, R>(&self) -> Effect<(), E, R>
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.
Trait Implementations§
Source§impl<Acc, In> Clone for FoldChannel<Acc, In>
impl<Acc, In> Clone for FoldChannel<Acc, In>
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>
impl<Acc, In> Send for FoldChannel<Acc, In>
impl<Acc, In> Sync for FoldChannel<Acc, In>
impl<Acc, In> Unpin for FoldChannel<Acc, In>
impl<Acc, In> UnsafeUnpin for FoldChannel<Acc, In>where
Arc<Mutex<Acc>>: UnsafeUnpin,
Arc<dyn Fn(Acc, In) -> Acc + Send + Sync>: UnsafeUnpin,
Queue<Acc>: UnsafeUnpin,
Arc<Mutex<VecDeque<Acc>>>: UnsafeUnpin,
Arc<dyn Fn(Acc) -> Vec<Acc> + Send + Sync>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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