id_effect 0.10.1

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
//! `Effect::fork` and `Effect::fork_daemon`.
//!
//! Both are built on `Instr::WithFiber`: the child is spawned from the forking fiber's context
//! and env when the fork step runs, never when the effect is built.

use std::sync::Arc;

use super::anybox::erase;
use super::effect::Effect;
use super::runloop::Instr;
use crate::failure::Never;
use crate::runtime::{FiberContext, FiberHandle, spawn_fiber};

impl<A, E, R> Effect<A, E, R>
where
  A: Send + 'static,
  E: Send + 'static,
  R: Send + Sync + 'static,
{
  /// Forks this effect as a child fiber of the current one, under the current env.
  ///
  /// The child is interrupted and awaited when its parent exits (success, failure or
  /// interrupt). Use [`widen`](Effect::widen) to fit the fork step into a fallible chain.
  pub fn fork(self) -> Effect<FiberHandle<A, E>, Never, R> {
    Effect::from_instr(Instr::WithFiber(Box::new(move |env, fiber| {
      let child = FiberContext::new_child(fiber);
      let cell = spawn_fiber(&child, self.into_instr(), Arc::clone(env));
      Instr::Pure(Ok(erase(FiberHandle::<A, E>::new(child, cell))))
    })))
  }

  /// Forks this effect as a daemon: a root fiber with no parent, which keeps running after the
  /// forking fiber has exited.
  ///
  /// Like Effect.ts `forkDaemon`, the daemon starts with a copy of the forking fiber's
  /// [`FiberRef`](crate::FiberRef) values at fork time; only its lifetime is detached.
  pub fn fork_daemon(self) -> Effect<FiberHandle<A, E>, Never, R> {
    Effect::from_instr(Instr::WithFiber(Box::new(move |env, fiber| {
      let child = FiberContext::new_detached(fiber);
      let cell = spawn_fiber(&child, self.into_instr(), Arc::clone(env));
      Instr::Pure(Ok(erase(FiberHandle::<A, E>::new(child, cell))))
    })))
  }
}