use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use super::anybox::{downcast_env_arc, erase, erase_exit};
use super::ctx::Ctx;
use super::effect::{Effect, Node};
use super::runloop::Instr;
use crate::failure::{Cause, Defect};
pub fn succeed<A, E, R>(a: A) -> Effect<A, E, R> {
Effect::pure(Ok(a))
}
pub fn fail<A, E, R>(e: E) -> Effect<A, E, R> {
Effect::pure(Err(Cause::Fail(e)))
}
pub fn die<A, E, R>(defect: impl Into<Defect>) -> Effect<A, E, R> {
Effect::pure(Err(Cause::Die(defect.into())))
}
pub fn suspend<A, E, R>(f: impl FnOnce() -> Effect<A, E, R> + Send + 'static) -> Effect<A, E, R> {
Effect {
node: Node::Sync(Box::new(move |_| f()), 1),
}
}
pub fn sync<A, E, R>(f: impl FnOnce() -> A + Send + 'static) -> Effect<A, E, R> {
Effect {
node: Node::Sync(Box::new(move |_| succeed(f())), 1),
}
}
pub fn from_async<A, E, R, F, Fut>(f: F) -> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
F: FnOnce() -> Fut + Send + 'static,
Fut: Future<Output = Result<A, E>> + Send + 'static,
{
Effect::from_instr(Instr::Future(Box::pin(async move {
erase_exit(f().await.map_err(Cause::Fail))
})))
}
pub fn sleep<E: Send + 'static, R: Send + Sync + 'static>(d: Duration) -> Effect<(), E, R> {
Effect::from_instr(Instr::Async(Box::new(move |_, fiber| {
let s = fiber.executor().sleep(d);
Box::pin(async move {
s.await;
Ok(erase(()))
})
})))
}
pub fn yield_fiber<E: Send + 'static, R: Send + Sync + 'static>() -> Effect<(), E, R> {
Effect::from_instr(Instr::Async(Box::new(|_, _| {
Box::pin(async {
YieldOnce(false).await;
Ok(erase(()))
})
})))
}
struct YieldOnce(bool);
impl Future for YieldOnce {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
if self.0 {
Poll::Ready(())
} else {
self.0 = true;
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
impl<A, E, R> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
pub fn from_ctx<F, Fut>(f: F) -> Self
where
F: FnOnce(Ctx<R>) -> Fut + Send + 'static,
Fut: Future<Output = Result<A, E>> + Send + 'static,
{
Self::from_instr(Instr::Async(Box::new(move |env, fiber| {
let ctx = Ctx::new(downcast_env_arc::<R>(env), Arc::clone(fiber));
let fut = f(ctx);
Box::pin(async move { erase_exit(fut.await.map_err(Cause::Fail)) })
})))
}
}