use super::anybox::{AnyBox, downcast_env, erase, unbox, unerase_exit};
use super::cause::{map_fail_once, split_failure, to_exit};
use super::runloop::{Frame, Instr, SeqBody, frame_vec};
use crate::capability::HNil;
use crate::failure::Never;
use crate::failure::{Cause, Exit};
pub(crate) const FUSE_MAX: usize = 4;
type SyncFn<A, E, R> = Box<dyn FnOnce(&R) -> Effect<A, E, R> + Send>;
#[must_use = "effects are lazy; run them with a run edge (`run_on`, `Runtime::run`, `block_on`)"]
pub struct Effect<A, E = Never, R = HNil> {
pub(crate) node: Node<A, E, R>,
}
pub(crate) enum Node<A, E, R> {
Pure(Result<A, Cause<E>>),
Sync(SyncFn<A, E, R>, usize),
Op(Instr),
}
impl<A, E, R> std::fmt::Debug for Effect<A, E, R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self.node {
Node::Pure(_) => "Effect::Pure",
Node::Sync(..) => "Effect::Sync",
Node::Op(_) => "Effect::Op",
})
}
}
impl<A, E, R> Effect<A, E, R> {
#[inline]
pub(crate) const fn pure(r: Result<A, Cause<E>>) -> Self {
Self {
node: Node::Pure(r),
}
}
}
impl<A, E, R> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
#[inline]
pub(crate) fn from_instr(i: Instr) -> Self {
Self { node: Node::Op(i) }
}
fn sync_node(f: impl FnOnce(&R) -> Effect<A, E, R> + Send + 'static, depth: usize) -> Self {
Self {
node: Node::Sync(Box::new(f), depth),
}
}
#[inline(always)]
pub(crate) fn into_instr(self) -> Instr {
match self.node {
Node::Pure(r) => Instr::Pure(super::anybox::erase_exit(r)),
Node::Sync(f, _) => Instr::Sync(Box::new(move |env| f(downcast_env::<R>(env)).into_instr())),
Node::Op(i) => i,
}
}
pub fn map<B: Send + 'static>(self, f: impl FnOnce(A) -> B + Send + 'static) -> Effect<B, E, R> {
match self.node {
Node::Pure(r) => Effect::sync_node(move |_| Effect::pure(r.map(f)), 1),
Node::Sync(g, d) if d < FUSE_MAX => Effect::sync_node(move |r| g(r).map_now(f), d + 1),
node => Effect::from_instr(
Self { node }.seq(Box::new(move |v| Instr::Pure(Ok(erase(f(unbox::<A>(v))))))),
),
}
}
fn map_now<B: Send + 'static>(self, f: impl FnOnce(A) -> B + Send + 'static) -> Effect<B, E, R> {
match self.node {
Node::Pure(r) => Effect::pure(r.map(f)),
node => Self { node }.map(f),
}
}
pub fn flat_map<B: Send + 'static>(
self,
f: impl FnOnce(A) -> Effect<B, E, R> + Send + 'static,
) -> Effect<B, E, R> {
match self.node {
Node::Pure(Ok(a)) => Effect::sync_node(move |_| f(a), 1),
Node::Pure(Err(c)) => Effect::pure(Err(c)),
Node::Sync(g, d) if d < FUSE_MAX => Effect::sync_node(move |r| g(r).flat_map_now(f), d + 1),
node => {
Effect::from_instr(Self { node }.seq(Box::new(move |v| f(unbox::<A>(v)).into_instr())))
}
}
}
fn flat_map_now<B: Send + 'static>(
self,
f: impl FnOnce(A) -> Effect<B, E, R> + Send + 'static,
) -> Effect<B, E, R> {
match self.node {
Node::Pure(Ok(a)) => f(a),
Node::Pure(Err(c)) => Effect::pure(Err(c)),
node => Self { node }.flat_map(f),
}
}
#[inline(always)]
fn seq(self, k: Box<dyn FnOnce(AnyBox) -> Instr + Send>) -> Instr {
match self.node {
Node::Op(Instr::Seq(mut body)) => {
body.frames.push(Frame::FlatMap(k));
Instr::Seq(body)
}
node => {
let mut frames = frame_vec();
frames.push(Frame::FlatMap(k));
Instr::Seq(Box::new(SeqBody {
inner: Self { node }.into_instr(),
frames,
}))
}
}
}
pub fn then<B: Send + 'static>(self, next: Effect<B, E, R>) -> Effect<B, E, R> {
self.flat_map(move |_| next)
}
pub fn zip<B: Send + 'static>(self, other: Effect<B, E, R>) -> Effect<(A, B), E, R> {
self.flat_map(move |a| other.map(move |b| (a, b)))
}
pub fn map_err<E2: Send + 'static>(
self,
f: impl FnOnce(E) -> E2 + Send + 'static,
) -> Effect<A, E2, R> {
match self.node {
Node::Pure(r) => {
Effect::sync_node(move |_| Effect::pure(r.map_err(|c| map_fail_once(c, f))), 1)
}
node => Effect::from_instr(Instr::Fold(
Box::new(Self { node }.into_instr()),
Box::new(move |exit| match exit {
Ok(v) => Instr::Pure(Ok(v)),
Err(c) => Instr::Pure(Err(map_fail_once(c, |e| erase(f(unbox::<E>(e)))))),
}),
)),
}
}
pub fn catch<E2: Send + 'static>(
self,
f: impl FnOnce(E) -> Effect<A, E2, R> + Send + 'static,
) -> Effect<A, E2, R> {
match self.node {
Node::Pure(Ok(a)) => Effect::pure(Ok(a)),
Node::Pure(Err(c)) => match split_failure::<E, E2>(c) {
Ok(e) => Effect::sync_node(move |_| f(e), 1),
Err(c) => Effect::pure(Err(c)),
},
node => Effect::from_instr(Instr::Fold(
Box::new(Self { node }.into_instr()),
Box::new(move |exit| match exit {
Ok(v) => Instr::Pure(Ok(v)),
Err(c) => match split_failure::<AnyBox, AnyBox>(c) {
Ok(e) => f(unbox::<E>(e)).into_instr(),
Err(c) => Instr::Pure(Err(c)),
},
}),
)),
}
}
pub fn exit<E2: Send + 'static>(self) -> Effect<Exit<A, E>, E2, R> {
match self.node {
Node::Pure(r) => Effect::pure(Ok(to_exit(r))),
node => Effect::from_instr(Instr::Fold(
Box::new(Self { node }.into_instr()),
Box::new(|exit| Instr::Pure(Ok(erase(to_exit(unerase_exit::<A, E>(exit)))))),
)),
}
}
}
impl<A: Send + 'static, R: Send + Sync + 'static> Effect<A, Never, R> {
pub fn widen<E: Send + 'static>(self) -> Effect<A, E, R> {
self.map_err(|n| match n {})
}
}