id_effect 0.8.0

Effect<A, E, R>: lazy Send effects over a typed capability list, run on a fiber runtime (Tokio executor), with layers, streams, schedules and coordination
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! The stream-, sink- and fold-backed channel variants of the 0.6 `coordination::Channel`,
//! deferred by `c-coordination` to the streaming leaf and built here on top of
//! [`coordination::{Queue, Channel}`](crate::coordination):
//!
//! - [`Channel::to_stream`] / [`Stream::from_channel`]: read a queue-backed [`Channel`] as a
//!   stream (one `read` per pull, so nothing is read ahead); [`Channel::to_sink`] writes a stream
//!   into one.
//! - [`StreamChannel`] (0.6 `Channel::from_stream`): a read-only channel over a stream.
//! - [`FoldChannel`] (0.6 `Channel::from_fold` / `from_sink`): every write folds into an
//!   accumulator and publishes a snapshot for readers.

use std::collections::VecDeque;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};

use super::sink::{FoldSink, Sink};
use super::stream::{Stream, lock};
use crate::capability::HNil;
use crate::coordination::{Channel, Queue, QueueError};
use crate::failure::Never;
use crate::kernel::{Effect, succeed, suspend, sync};

type Emit<A> = Arc<dyn Fn(A) -> Vec<A> + Send + Sync>;

impl<Out: Send + 'static, In: Send + 'static> Channel<Out, In> {
  /// The channel's output as a stream: each pull performs exactly one
  /// [`read`](Channel::read), and the stream ends once the channel is shut down and drained.
  pub fn to_stream<E, R>(&self) -> Stream<Out, E, R>
  where
    E: Send + 'static,
    R: Send + Sync + 'static,
  {
    Stream::from_channel(self)
  }

  /// A sink that [`write`](Channel::write)s every element of a stream into this channel.
  pub fn to_sink<E, R>(&self) -> Sink<(), In, E, R>
  where
    E: Send + 'static,
    R: Send + Sync + 'static,
  {
    let ch = self.clone();
    Sink::new(move |stream: Stream<In, E, R>| {
      let ch = ch.clone();
      stream.run_for_each_effect(move |x| ch.write(x))
    })
  }
}

impl<A, E, R> Stream<A, E, R>
where
  A: Send + 'static,
  E: Send + 'static,
  R: Send + Sync + 'static,
{
  /// The output of `ch` as a stream (see [`Channel::to_stream`]).
  pub fn from_channel<In: Send + 'static>(ch: &Channel<A, In>) -> Self {
    channel_reader(ch.clone())
  }

  /// This stream as a read-only [`StreamChannel`].
  pub fn into_channel(self) -> StreamChannel<A, E, R> {
    StreamChannel::new(self)
  }
}

fn channel_reader<A, In, E, R>(ch: Channel<A, In>) -> Stream<A, E, R>
where
  A: Send + 'static,
  In: Send + 'static,
  E: Send + 'static,
  R: Send + Sync + 'static,
{
  Stream::from_step(move |_| {
    let read = ch.read::<E, R>();
    read.map(move |next| next.map(|a| (vec![a], channel_reader(ch))))
  })
}

// -----------------------------------------------------------------------------------------------
// StreamChannel
// -----------------------------------------------------------------------------------------------

/// A read-only channel over a [`Stream`] (0.6 `Channel::from_stream`): [`read`](Self::read)
/// returns the stream's elements one at a time, `None` at its end, and fails with the stream's
/// error. A pulled chunk is buffered for the following reads. Cloning yields another handle on
/// the same stream; concurrent reads take turns (the stream is parked in a one-slot [`Queue`]
/// between reads), and a read that fails or is interrupted while pulling ends the stream for
/// every handle.
pub struct StreamChannel<A, E = Never, R = HNil> {
  /// The stream before the first read (the slot queue cannot be filled outside an effect).
  initial: Arc<Mutex<Option<Stream<A, E, R>>>>,
  slot: Queue<Stream<A, E, R>>,
  pending: Arc<Mutex<VecDeque<A>>>,
  emit: Emit<A>,
}

impl<A, E, R> Clone for StreamChannel<A, E, R> {
  fn clone(&self) -> Self {
    Self {
      initial: Arc::clone(&self.initial),
      slot: self.slot.clone(),
      pending: Arc::clone(&self.pending),
      emit: Arc::clone(&self.emit),
    }
  }
}

impl<A, E, R> std::fmt::Debug for StreamChannel<A, E, R> {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("StreamChannel")
      .field("pending", &lock(&self.pending).len())
      .finish_non_exhaustive()
  }
}

impl<A, E, R> StreamChannel<A, E, R>
where
  A: Send + 'static,
  E: Send + 'static,
  R: Send + Sync + 'static,
{
  /// A channel reading `stream`.
  pub fn new(stream: Stream<A, E, R>) -> Self {
    Self {
      initial: Arc::new(Mutex::new(Some(stream))),
      slot: Queue::bounded(1),
      pending: Arc::new(Mutex::new(VecDeque::new())),
      emit: Arc::new(|a| vec![a]),
    }
  }

  /// Reads the next element (`None` once the stream has ended).
  pub fn read(&self) -> Effect<Option<A>, E, R> {
    let ch = self.clone();
    suspend(move || {
      if let Some(a) = lock(&ch.pending).pop_front() {
        return succeed(Some(a));
      }
      let next = ch.clone();
      let first = lock(&ch.initial).take();
      match first {
        Some(stream) => next.read_from(stream),
        None => ch
          .slot
          .take::<R>()
          .catch(|_: QueueError| succeed(Stream::empty()))
          .flat_map(move |stream| next.read_from(stream)),
      }
    })
  }

  /// Pulls one chunk from `stream` (taken out of the slot) and puts the rest back; a failed or
  /// interrupted pull puts an empty stream back instead.
  fn read_from(self, stream: Stream<A, E, R>) -> Effect<Option<A>, E, R> {
    let returned = Arc::new(AtomicBool::new(false));
    let fin = {
      let (slot, returned) = (self.slot.clone(), Arc::clone(&returned));
      suspend(move || {
        if returned.load(Ordering::Acquire) {
          succeed(())
        } else {
          slot.try_offer(Stream::empty()).map(|_| ())
        }
      })
    };
    let ch = self;
    stream
      .next_chunk()
      .flat_map(move |next| {
        let rest = match next {
          None => {
            returned.store(true, Ordering::Release);
            return ch.slot.try_offer(Stream::empty()).map(|_| None);
          }
          Some((chunk, rest)) => {
            let out: VecDeque<A> = chunk.into_vec().into_iter().flat_map(&*ch.emit).collect();
            lock(&ch.pending).extend(out);
            rest
          }
        };
        returned.store(true, Ordering::Release);
        ch.slot.try_offer(rest).flat_map(move |_| {
          let first = lock(&ch.pending).pop_front();
          match first {
            Some(a) => succeed(Some(a)),
            // Every element of the chunk expanded to nothing: read on.
            None => ch.read(),
          }
        })
      })
      .ensuring(fin)
  }

  /// The channel's elements as a stream (one [`read`](Self::read) per pull).
  pub fn to_stream(&self) -> Stream<A, E, R> {
    let ch = self.clone();
    Stream::from_step(move |_| {
      let read = ch.read();
      read.map(move |next| next.map(|a| (vec![a], ch.to_stream())))
    })
  }

  /// Maps each element on the read side.
  #[must_use]
  pub fn map_out(self, f: impl Fn(A) -> A + Send + Sync + 'static) -> Self {
    let emit = self.emit;
    Self {
      emit: Arc::new(move |a| emit(a).into_iter().map(&f).collect()),
      ..self
    }
  }

  /// Expands each element on the read side into zero or more elements (an empty expansion is
  /// skipped).
  #[must_use]
  pub fn flat_map_out(self, f: impl Fn(A) -> Vec<A> + Send + Sync + 'static) -> Self {
    let emit = self.emit;
    Self {
      emit: Arc::new(move |a| emit(a).into_iter().flat_map(&f).collect()),
      ..self
    }
  }
}

// -----------------------------------------------------------------------------------------------
// FoldChannel
// -----------------------------------------------------------------------------------------------

type FoldFn<Acc, In> = Arc<dyn Fn(Acc, In) -> Acc + Send + Sync>;

/// A fold-backed channel (0.6 `Channel::from_fold` / `Channel::from_sink`): each
/// [`write`](Self::write) folds the value into an accumulator and publishes the new
/// accumulator as a snapshot; [`read`](Self::read) returns the snapshots in order and
/// [`fold_state`](Self::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));
/// ```
pub struct FoldChannel<Acc, In> {
  acc: Arc<Mutex<Acc>>,
  step: FoldFn<Acc, In>,
  snapshots: Queue<Acc>,
  pending: Arc<Mutex<VecDeque<Acc>>>,
  emit: Emit<Acc>,
}

impl<Acc, In> Clone for FoldChannel<Acc, In> {
  fn clone(&self) -> Self {
    Self {
      acc: Arc::clone(&self.acc),
      step: Arc::clone(&self.step),
      snapshots: self.snapshots.clone(),
      pending: Arc::clone(&self.pending),
      emit: Arc::clone(&self.emit),
    }
  }
}

impl<Acc, In> std::fmt::Debug for FoldChannel<Acc, In> {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("FoldChannel")
      .field("snapshots", &self.snapshots)
      .finish_non_exhaustive()
  }
}

impl<Acc, In> FoldChannel<Acc, In>
where
  Acc: Clone + Send + 'static,
  In: Send + 'static,
{
  /// A channel folding every write into `init` with `step`.
  pub fn from_fold(init: Acc, step: impl Fn(Acc, In) -> Acc + Send + Sync + 'static) -> Self {
    Self::with_step(init, Arc::new(step))
  }

  fn with_step(init: Acc, step: FoldFn<Acc, In>) -> Self {
    Self {
      acc: Arc::new(Mutex::new(init)),
      step,
      snapshots: Queue::unbounded(),
      pending: Arc::new(Mutex::new(VecDeque::new())),
      emit: Arc::new(|a| vec![a]),
    }
  }

  /// 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`].
  ///
  /// ```compile_fail,E0308
  /// use id_effect::{FoldChannel, Sink};
  ///
  /// // `collect_all_while` is not a fold.
  /// let _ = FoldChannel::from_sink(Sink::<Vec<u32>, u32>::collect_all_while(|_| true));
  /// ```
  pub fn from_sink<E, R>(sink: FoldSink<Acc, In, E, R>) -> Self
  where
    Acc: Sync,
  {
    let fold = sink.fold;
    Self::with_step(fold.init.clone(), Arc::clone(&fold.step))
  }

  /// Folds `value` into the accumulator and publishes the new accumulator for readers.
  pub fn write<E, R>(&self, value: In) -> Effect<(), E, R>
  where
    E: Send + 'static,
    R: Send + Sync + 'static,
  {
    let ch = self.clone();
    suspend(move || {
      let next = {
        let mut acc = lock(&ch.acc);
        let next = (ch.step)(acc.clone(), value);
        *acc = next.clone();
        next
      };
      ch.snapshots.try_offer(next).map(|_| ())
    })
  }

  /// The current accumulator.
  pub fn fold_state<E, R>(&self) -> Effect<Acc, E, R>
  where
    E: Send + 'static,
    R: Send + Sync + 'static,
  {
    let acc = Arc::clone(&self.acc);
    sync(move || lock(&acc).clone())
  }

  /// The next snapshot, in write order (suspending until a write); `None` once the channel is
  /// [shut down](Self::shutdown) and every snapshot has been read.
  pub fn read<E, R>(&self) -> Effect<Option<Acc>, E, R>
  where
    E: Send + 'static,
    R: Send + Sync + 'static,
  {
    let ch = self.clone();
    suspend(move || {
      if let Some(a) = lock(&ch.pending).pop_front() {
        return succeed(Some(a));
      }
      let next = ch.clone();
      ch.snapshots
        .take::<R>()
        .map(Some)
        .catch(|_: QueueError| succeed(None))
        .flat_map(move |snap| match snap {
          None => succeed(None),
          Some(s) => {
            let mut out: VecDeque<Acc> = (next.emit)(s).into();
            match out.pop_front() {
              Some(first) => {
                lock(&next.pending).extend(out);
                succeed(Some(first))
              }
              None => next.read(),
            }
          }
        })
    })
  }

  /// Folds every element of `stream` into the channel (as [`write`](Self::write)s), then
  /// returns the final accumulator. A stream failure ends the drain with that error.
  pub fn consume_stream<E, R>(&self, stream: Stream<In, E, R>) -> Effect<Acc, E, R>
  where
    E: Send + 'static,
    R: Send + Sync + 'static,
  {
    let (w, s) = (self.clone(), self.clone());
    stream
      .run_for_each_effect(move |x| w.write(x))
      .flat_map(move |()| s.fold_state())
  }

  /// Ends the snapshot feed: snapshots already published stay readable, then reads return
  /// `None`. Later writes still fold but publish nothing.
  pub fn shutdown<E, R>(&self) -> Effect<(), E, R> {
    self.snapshots.shutdown()
  }

  /// Maps each snapshot on the read side.
  #[must_use]
  pub fn map_out(self, f: impl Fn(Acc) -> Acc + Send + Sync + 'static) -> Self {
    let emit = self.emit;
    Self {
      emit: Arc::new(move |a| emit(a).into_iter().map(&f).collect()),
      ..self
    }
  }

  /// Expands each snapshot on the read side into zero or more values (an empty expansion is
  /// skipped).
  #[must_use]
  pub fn flat_map_out(self, f: impl Fn(Acc) -> Vec<Acc> + Send + Sync + 'static) -> Self {
    let emit = self.emit;
    Self {
      emit: Arc::new(move |a| emit(a).into_iter().flat_map(&f).collect()),
      ..self
    }
  }
}