use std::{
pin::Pin,
task::{Context, Poll},
};
use crate::{actor::Actor, fut::future::ActorFuture};
mod and_then;
mod map_err;
mod map_ok;
pub use and_then::AndThen;
pub use map_err::MapErr;
pub use map_ok::MapOk;
mod private_try_act_future {
use super::{Actor, ActorFuture};
pub trait Sealed<A> {}
impl<A, F, T, E> Sealed<A> for F
where
A: Actor,
F: ?Sized + ActorFuture<A, Output = Result<T, E>>,
{
}
}
pub trait ActorTryFuture<A: Actor>: ActorFuture<A> + private_try_act_future::Sealed<A> {
type Ok;
type Error;
fn try_poll(
self: Pin<&mut Self>,
srv: &mut A,
ctx: &mut A::Context,
task: &mut Context<'_>,
) -> Poll<Result<Self::Ok, Self::Error>>;
}
impl<A, F, T, E> ActorTryFuture<A> for F
where
A: Actor,
F: ActorFuture<A, Output = Result<T, E>> + ?Sized,
{
type Ok = T;
type Error = E;
fn try_poll(
self: Pin<&mut Self>,
srv: &mut A,
ctx: &mut A::Context,
task: &mut Context<'_>,
) -> Poll<F::Output> {
self.poll(srv, ctx, task)
}
}
pub trait ActorTryFutureExt<A: Actor>: ActorTryFuture<A> {
fn and_then<Fut, F>(self, f: F) -> AndThen<Self, Fut, F>
where
F: FnOnce(Self::Ok, &mut A, &mut A::Context) -> Fut,
Fut: ActorTryFuture<A, Error = Self::Error>,
Self: Sized,
{
and_then::new(self, f)
}
fn map_ok<T, F>(self, f: F) -> MapOk<Self, F>
where
F: FnOnce(Self::Ok, &mut A, &mut A::Context) -> T,
Self: Sized,
{
MapOk::new(self, f)
}
fn map_err<T, F>(self, f: F) -> MapErr<Self, F>
where
F: FnOnce(Self::Error, &mut A, &mut A::Context) -> T,
Self: Sized,
{
MapErr::new(self, f)
}
}
impl<A, F> ActorTryFutureExt<A> for F
where
A: Actor,
F: ActorTryFuture<A> + ?Sized,
{
}