id_effect 0.13.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Support items for the `effect!` / `effect_local!` / `caps!` expansions, reached as
//! `id_effect::__private`. Not API.
//!
//! - [`Lift`] with [`LiftNever`] / [`LiftFrom`]: error widening for `~` binds by autoref
//!   specialisation (the ADR 0011 prototype). `(&&Lift::new(e)).lift()` picks `LiftNever` for a
//!   `Never` error (widens to anything) and `LiftFrom` otherwise (`E: From<E2>`, through
//!   [`WidenFrom`] for the diagnostic).
//! - [`bind_local`]: what `~expr` expands to inside `effect_local!`.
//! - [`CapsChecked`]: the identity projection `caps!` wraps a de-duplicated list in when it
//!   also reports a duplicate (so the error does not cascade).
//! - [`branch`] and [`join_binds`]: what a parallel join `~(a, b, ..)` (2 to 8 effects) expands
//!   to inside `effect!`.
//!
//! # Parallel joins: `~(a, b, ..)`
//!
//! A `~` followed by a parenthesised, comma-separated list of 2 to 8 effects runs them as child
//! fibers through [`all`](crate::all) (one `fork_all`, `Concurrency::Unbounded`) and yields
//! the tuple of their values, in input order. It shares `zip_par`'s semantics: the first failure
//! interrupts the siblings still running and the join waits for them (their finalizers run)
//! before it fails; interrupting the body's fiber interrupts every branch.
//!
//! The body sees the **first** failure, the leftmost leaf of `zip_par`'s cause: its typed error
//! as the `~`'s error (widened like any `~` error, per branch), or its defect or interrupt as the
//! body's cause. Genuine sibling failures that `zip_par` adds with `Cause::Both` are dropped (one
//! `?` carries one error); bind `~zip_par(a, b).exit::<Never>()` to keep the whole cause as data.
//!
//! Each branch is an effect over a part of the body's environment (narrowed, as `~` does) or a
//! `.same_env()` one. Binds are statements, so a branch may not refer to another branch's value;
//! independent `let a = ~x; let b = ~y;` statements stay sequential (no implicit parallelism).
//!
//! ```
//! use id_effect::{Effect, effect, fail, succeed};
//! use id_effect::{Cause, Exit};
//!
//! fn both() -> Effect<(u8, &'static str), String> {
//!   effect!({ ~(succeed::<u8, String, _>(1), succeed::<_, String, _>("two")) })
//! }
//! assert!(matches!(both().block_on(), Exit::Success((1, "two"))));
//!
//! fn failing() -> Effect<u8, String> {
//!   effect!({
//!     let (a, _b) = ~(succeed::<u8, String, _>(1), fail::<u8, &'static str, _>("boom"));
//!     a
//!   })
//! }
//! assert!(matches!(failing().block_on(), Exit::Failure(Cause::Fail(e)) if e == "boom"));
//! ```
//!
//! More than 8 branches is a compile error (nest a join, or use `all`):
//!
//! ```compile_fail
//! use id_effect::{Effect, effect, succeed};
//! use id_effect::Never;
//!
//! fn one() -> Effect<u8, Never> {
//!   succeed(1)
//! }
//! fn nine() -> Effect<u8, Never> {
//!   effect!({
//!     let (a, ..) = ~(one(), one(), one(), one(), one(), one(), one(), one(), one());
//!     a
//!   })
//! }
//! ```
//!
//! So is a join in `effect_local!`:
//!
//! ```compile_fail
//! use id_effect::{Effect, LocalEffect, effect_local, succeed};
//! use id_effect::Never;
//!
//! fn one() -> Effect<u8, Never> {
//!   succeed(1)
//! }
//! fn local() -> LocalEffect<u8, Never> {
//!   effect_local!({
//!     let (a, b) = ~(one(), one());
//!     a + b
//!   })
//! }
//! ```

use std::any::Any;
use std::cell::Cell;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use super::anybox::AnyEnv;
use super::bind::{BindFuture, surface, surface_first};
use super::bind_same::{SameEnv, SameEnvIndex};
use super::cause::from_exit;
use super::ctx::Ctx;
use super::effect::Effect;
use super::local::{HoldMask, IntoLocal};
use crate::capability::{Embed, HList};
use crate::concurrency::Concurrency;
use crate::failure::Never;
use crate::par::all;

/// A `~` bind's error, waiting to be widened into the body's error type.
pub struct Lift<E>(Cell<Option<E>>);

impl<E> Lift<E> {
  /// Wraps the sub-effect's error.
  pub fn new(e: E) -> Self {
    Self(Cell::new(Some(e)))
  }
}

/// Widening of a `Never` error (picked first by autoref).
pub trait LiftNever {
  /// Any `E` (a `Never` has no values).
  fn lift<E>(&self) -> E;
}

impl LiftNever for &Lift<Never> {
  fn lift<E>(&self) -> E {
    match self.0.take() {
      Some(n) => match n {},
      None => unreachable!("id_effect: `~` error lifted twice (macro bug)"),
    }
  }
}

/// Widening through `From` (every other error type).
pub trait LiftFrom<E2> {
  /// `E::from(e2)`.
  fn lift<E: WidenFrom<E2>>(&self) -> E;
}

impl<E2> LiftFrom<E2> for Lift<E2> {
  fn lift<E: WidenFrom<E2>>(&self) -> E {
    match self.0.take() {
      Some(e) => E::widen(e),
      None => unreachable!("id_effect: `~` error lifted twice (macro bug)"),
    }
  }
}

/// `From<E2>` under another name, so a `~` bind whose error does not widen into the body's error
/// reports a diagnostic that states the fix instead of a bare `From` bound. Implemented for every
/// `E: From<E2>` and nothing else.
#[diagnostic::on_unimplemented(
  message = "the error `{E2}` of this `~` bind does not widen into the body's error `{Self}`",
  label = "`{Self}` has no `From<{E2}>`",
  note = "convert it at the bind: `~x.map_err(..)` (into an `Or<L, R>` body error, `.map_err(Or::Right)` for the right arm), `~x.catch(..)` to handle it (the only option in a `Never` body), or `impl From<{E2}> for {Self}`"
)]
pub trait WidenFrom<E2> {
  /// `Self::from(e)`.
  fn widen(e: E2) -> Self;
}

#[diagnostic::do_not_recommend]
impl<E: From<E2>, E2> WidenFrom<E2> for E {
  fn widen(e: E2) -> E {
    E::from(e)
  }
}

/// `~sub` inside `effect_local!`: runs a [`LocalEffect`](super::LocalEffect) or a `Send`
/// [`Effect`](super::Effect) (via [`IntoLocal`]) whose environment `Sub` is a part of `R`, on
/// `ctx`'s fiber, as a nested local runloop. Outcomes surface as in [`Ctx::bind`]: the typed error
/// as `Err`, a defect or an interrupt re-raised in the enclosing runloop.
pub fn bind_local<R, X, A, E, Sub, Is>(
  ctx: &Ctx<R>,
  sub: X,
) -> impl Future<Output = Result<A, E>> + use<R, X, A, E, Sub, Is>
where
  X: IntoLocal<A, E, Sub>,
  R: Embed<Sub, Is> + Send + Sync + 'static,
  Sub: Send + Sync + 'static,
  A: 'static,
  E: 'static,
{
  // Same environment: share it. Otherwise project the subset (one `Arc` clone per cap).
  let env: Arc<Sub> = match (Arc::clone(&ctx.env) as AnyEnv).downcast::<Sub>() {
    Ok(same) => same,
    Err(_) => Arc::new(Embed::<Sub, Is>::project(&*ctx.env)),
  };
  let run = sub
    .into_local()
    .run_in(Ctx::new(env, Arc::clone(&ctx.fiber)));
  // Holds a mask level while suspended, as the `Send` bind does (`BindFuture`), so an interrupt
  // reaches the nested runloop and its finalizers run.
  let run = HoldMask::new(run, Arc::clone(&ctx.fiber));
  async move { surface(from_exit(run.await)) }
}

/// The identity on `Self`, with a const argument that carries `caps!`'s duplicate errors.
pub trait CapsChecked<const N: usize> {
  /// `Self`.
  type Out;
}

impl<T, const N: usize> CapsChecked<N> for T {
  type Out = T;
}

/// One branch of a `~(a, b, ..)` join, as an effect over the body's environment `R`.
#[diagnostic::on_unimplemented(
  message = "`{Self}` cannot be a branch of a `~(..)` join in an effect over `{R}`",
  label = "not an effect over a part of `{R}`",
  note = "each branch of `~(a, b, ..)` is an `Effect` (or a `.same_env()` one); bind a `Result` with `?` or a future with `.await` before the join"
)]
pub trait JoinBranch<R, Is> {
  /// The branch's success type.
  type Output;
  /// The branch's typed error.
  type Error;
  /// The branch over `R`.
  fn into_branch(self) -> Effect<Self::Output, Self::Error, R>;
}

impl<B, E2, Sub, R, Is> JoinBranch<R, Is> for Effect<B, E2, Sub>
where
  B: Send + 'static,
  E2: Send + 'static,
  Sub: HList + Send + Sync + 'static,
  R: Embed<Sub, Is> + HList,
{
  type Output = B;
  type Error = E2;

  fn into_branch(self) -> Effect<B, E2, R> {
    self.narrow::<R, Is>()
  }
}

impl<B, E2, R> JoinBranch<R, SameEnvIndex> for SameEnv<Effect<B, E2, R>> {
  type Output = B;
  type Error = E2;

  fn into_branch(self) -> Effect<B, E2, R> {
    self.into_inner()
  }
}

/// A branch of `~(a, b, ..)` over the body's environment (`ctx` fixes `R`); the macro then
/// widens its error into the body's with `map_err` and [`Lift`].
pub fn branch<R, X, Is>(_ctx: &Ctx<R>, x: X) -> Effect<X::Output, X::Error, R>
where
  X: JoinBranch<R, Is>,
{
  x.into_branch()
}

/// A tuple of 2 to 8 branches over one environment and one error, joined by [`join_binds`].
#[diagnostic::on_unimplemented(
  message = "`~(..)` joins 2 to 8 effects over the same environment and error",
  label = "not a tuple of 2 to 8 effects",
  note = "pass 2 to 8 effects; for more, nest a join or use `all`"
)]
pub trait JoinTuple<R> {
  /// The tuple of the branches' values.
  type Output;
  /// The common (already widened) error.
  type Error;
  /// The branches as one effect: `all` over their erased values, re-tupled in input order.
  fn join(self) -> Effect<Self::Output, Self::Error, R>;
}

/// A branch's value, erased so that one homogeneous `fork_all` runs every branch.
type Slot = Box<dyn Any + Send>;

fn unslot<A: 'static>(slot: Option<Slot>) -> A {
  match slot.map(|s| s.downcast::<A>()) {
    Some(Ok(a)) => *a,
    _ => unreachable!("id_effect: `~(..)` join lost a branch value (kernel bug)"),
  }
}

macro_rules! join_tuple {
  ($($a:ident $i:tt),+) => {
    impl<E, R, $($a),+> JoinTuple<R> for ($(Effect<$a, E, R>,)+)
    where
      E: Send + 'static,
      R: Send + Sync + 'static,
      $($a: Send + 'static,)+
    {
      type Output = ($($a,)+);
      type Error = E;

      fn join(self) -> Effect<Self::Output, E, R> {
        let branches: Vec<Effect<Slot, E, R>> =
          vec![$(self.$i.map(|v| Box::new(v) as Slot)),+];
        all(branches, Concurrency::Unbounded).map(|slots| {
          let mut slots = slots.into_iter();
          ($(unslot::<$a>(slots.next()),)+)
        })
      }
    }
  };
}

join_tuple!(A0 0, A1 1);
join_tuple!(A0 0, A1 1, A2 2);
join_tuple!(A0 0, A1 1, A2 2, A3 3);
join_tuple!(A0 0, A1 1, A2 2, A3 3, A4 4);
join_tuple!(A0 0, A1 1, A2 2, A3 3, A4 4, A5 5);
join_tuple!(A0 0, A1 1, A2 2, A3 3, A4 4, A5 5, A6 6);
join_tuple!(A0 0, A1 1, A2 2, A3 3, A4 4, A5 5, A6 6, A7 7);

/// `~(a, b, ..)` inside `effect!`: runs the branches as child fibers (see the
/// [module docs](self)) and yields their values, or the first failure.
pub fn join_binds<R, T>(ctx: &Ctx<R>, branches: T) -> JoinFuture<T::Output, T::Error>
where
  T: JoinTuple<R>,
  T::Output: Send + 'static,
  T::Error: Send + 'static,
  R: Send + Sync + 'static,
{
  JoinFuture(ctx.bind_same_env(branches.join()))
}

/// The future of a `~(a, b, ..)` join: the bind of the joined effect, surfaced by its first
/// failure (the leftmost leaf of the cause) rather than its first typed failure.
#[must_use = "futures do nothing unless polled"]
#[derive(Debug)]
pub struct JoinFuture<B, E>(BindFuture<B, E>);

impl<B: 'static, E: 'static> Future for JoinFuture<B, E> {
  type Output = Result<B, E>;

  fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<B, E>> {
    self.get_mut().0.poll_exit(cx).map(surface_first)
  }
}

#[cfg(test)]
mod tests {
  use std::future::Future;
  use std::pin::Pin;
  use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
  use std::task::{Context, Poll, Wake, Waker};
  use std::time::{Duration, Instant};

  use super::*;
  use crate::capability::HNil;
  use crate::failure::{Cause, Exit};
  use crate::kernel::{LocalEffect, from_async, succeed, sync};
  use crate::runtime::{BoxFuture, Executor, FiberContext, LocalBoxFuture, NotLocal};

  struct NoExec;

  /// A waker that drops its wake-ups: these tests poll by hand.
  struct Ignore;

  #[allow(
    clippy::manual_noop_waker,
    reason = "the K-nb gate bans the std no-op waker anywhere under src/next"
  )]
  impl Wake for Ignore {
    fn wake(self: Arc<Self>) {}
  }

  impl Executor for NoExec {
    fn spawn(&self, _: BoxFuture<'static, ()>) {
      unreachable!("bind_local unit tests spawn nothing")
    }
    fn spawn_local(&self, _: LocalBoxFuture<'static, ()>) -> Result<(), NotLocal> {
      Err(NotLocal)
    }
    fn spawn_blocking(&self, _: Box<dyn FnOnce() + Send>) {
      unreachable!("bind_local unit tests spawn nothing")
    }
    fn sleep(&self, _: Duration) -> BoxFuture<'static, ()> {
      Box::pin(async {})
    }
    fn now(&self) -> Instant {
      Instant::now()
    }
  }

  /// Runs `~sub` (as `effect_local!` expands it) until `sub` is suspended, holding one
  /// mask level, then interrupts the fiber and polls again: `sub`'s finalizer runs (checked by
  /// the callers), the body does not continue, and no mask level leaks. Polled by hand, so the
  /// interrupt always lands on an already suspended runloop.
  fn interrupt_a_suspended_local_bind<X>(sub: X)
  where
    X: IntoLocal<u8, (), HNil> + 'static,
  {
    let fiber = FiberContext::new_root(Arc::new(NoExec));
    let after = Arc::new(AtomicUsize::new(0));
    let after2 = Arc::clone(&after);
    let body: LocalEffect<u8, (), HNil> = LocalEffect::from_ctx(move |ctx| async move {
      let v = bind_local::<HNil, X, u8, (), HNil, HNil>(&ctx, sub).await?;
      after2.fetch_add(1, Ordering::SeqCst);
      Ok(v)
    });
    let mut run = body.run_on(Arc::clone(&fiber));
    let waker = Waker::from(Arc::new(Ignore));
    let mut cx = Context::from_waker(&waker);
    assert!(Pin::new(&mut run).poll(&mut cx).is_pending());
    assert!(fiber.mask() > 0, "the suspended bind holds a mask level");
    fiber.request_interrupt();
    let Poll::Ready(exit) = Pin::new(&mut run).poll(&mut cx) else {
      panic!("an interrupted bind completes")
    };
    assert!(
      matches!(exit, Exit::Failure(Cause::Interrupt(id)) if id == fiber.id()),
      "{exit:?}"
    );
    assert_eq!(after.load(Ordering::SeqCst), 0, "the body did not continue");
    assert_eq!(fiber.mask(), 0, "no mask level leaked");
  }

  #[test]
  fn an_interrupted_local_bind_of_a_local_effect_runs_its_finalizer() {
    let finalized = Arc::new(AtomicBool::new(false));
    let fin = Arc::clone(&finalized);
    interrupt_a_suspended_local_bind(
      LocalEffect::<u8, (), HNil>::from_future(|| async {
        std::future::pending::<()>().await;
        Ok(0)
      })
      .ensuring(LocalEffect::sync(move || fin.store(true, Ordering::SeqCst))),
    );
    assert!(finalized.load(Ordering::SeqCst), "sub-effect finalizer ran");
  }

  #[test]
  fn an_interrupted_local_bind_of_a_send_effect_runs_its_finalizer() {
    let finalized = Arc::new(AtomicBool::new(false));
    let fin = Arc::clone(&finalized);
    interrupt_a_suspended_local_bind(
      succeed::<u8, (), HNil>(0)
        .flat_map(|_| {
          from_async(|| async {
            std::future::pending::<()>().await;
            Ok(0)
          })
        })
        .ensuring(sync(move || fin.store(true, Ordering::SeqCst))),
    );
    assert!(finalized.load(Ordering::SeqCst), "sub-effect finalizer ran");
  }
}