use crate::{
any::TypeName,
effect::{Effect, ErasedEffective, ReadyExt as _},
hkt::Marker,
protocol::{
walker::hint::{HasProtocol, HintMeta, Meta},
DynVisitor,
},
Status,
};
use super::VisitResult;
pub trait Recoverable<'ctx, E: Effect> {
fn visit<'a>(
&'a mut self,
scope: DynRecoverableScope<'a, 'ctx, E>,
) -> ErasedEffective<'a, VisitResult, E>;
}
pub struct RecoverableProto<E: Effect>(Marker<E>);
impl<'a, 'ctx, E> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> for RecoverableProto<E>
where
E: Effect,
{
type T = dyn Recoverable<'ctx, E> + Send + Sync + 'a;
}
impl<'a, 'ctx, E> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>
for dyn Recoverable<'ctx, E> + Send + Sync + 'a
where
E: Effect,
{
type Higher = RecoverableProto<E>;
}
pub trait RecoverableScope<'ctx, E: Effect> {
fn new_walk<'this: 'effect, 'visitor: 'effect, 'effect>(
&'this mut self,
visitor: DynVisitor<'visitor, 'ctx>,
) -> ErasedEffective<'effect, Status, E>;
}
pub type DynRecoverableScope<'a, 'ctx, E> =
&'a mut (dyn RecoverableScope<'ctx, E> + Send + Sync + 'a);
pub struct RecoverableKnown;
impl<'a, 'ctx> Meta::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> for RecoverableKnown {
type T = RecoverableKnown;
}
impl<'a, 'ctx> Meta::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()> for RecoverableKnown {
type Higher = RecoverableKnown;
}
impl<E: Effect> HintMeta for RecoverableProto<E> {
type Known = RecoverableKnown;
type Hint = ();
type Effect = E;
}
pub fn visit_recoverable<'a, 'ctx, E: Effect>(
visitor: DynVisitor<'a, 'ctx>,
scope: DynRecoverableScope<'a, 'ctx, E>,
) -> ErasedEffective<'a, VisitResult, E> {
if let Some(object) = visitor.0.upcast_mut::<RecoverableProto<E>>() {
// Allow the visitor to give a hint if it wants.
object.visit(scope)
} else {
// If the visitor doesn't support request hint then we continue.
VisitResult::Skipped(()).ready()
}
}
impl<'ctx, T, E: Effect> HasProtocol<RecoverableProto<E>> for T where T: Recoverable<'ctx, E> {}