use effectful::{
effective::Effective,
environment::{DynBind, EnvConfig, Environment, NativeForm},
SendSync,
};
use crate::{
any::TypeName,
hkt::Marker,
protocol::{
walker::hint::{HasProtocol, HintMeta, Meta},
DynVisitor,
},
Status,
};
use super::VisitResult;
pub trait Recoverable<'ctx, E: Environment>: DynBind<E> {
fn visit<'a>(
&'a mut self,
scope: DynRecoverableScope<'a, 'ctx, E>,
) -> NativeForm<'a, VisitResult, E>;
}
#[derive(SendSync)]
pub struct RecoverableProto<E: Environment>(Marker<E>);
impl<'a, 'ctx, E> TypeName::MemberTypeForLt<'a, 'ctx, E, &'a &'ctx ()> for RecoverableProto<E>
where
E: Environment,
{
type T = dyn Recoverable<'ctx, E> + 'a;
}
impl<'a, 'ctx, E> TypeName::LowerTypeWithBound<'a, 'ctx, E, &'a &'ctx ()>
for dyn Recoverable<'ctx, E> + 'a
where
E: Environment,
{
type Higher = RecoverableProto<E>;
}
pub trait RecoverableScope<'ctx, E: Environment>: DynBind<E> {
fn new_walk<'this: 'effect, 'visitor: 'effect, 'effect>(
&'this mut self,
visitor: DynVisitor<'visitor, 'ctx, E>,
) -> NativeForm<'effect, Status, E>;
}
pub type DynRecoverableScope<'a, 'ctx, E> = &'a mut (dyn RecoverableScope<'ctx, E> + 'a);
#[derive(SendSync)]
pub struct RecoverableKnown;
impl<'a, 'ctx, E: EnvConfig> Meta::MemberTypeForLt<'a, 'ctx, E, &'a &'ctx ()> for RecoverableKnown {
type T = RecoverableKnown;
}
impl<'a, 'ctx, E: EnvConfig> Meta::LowerTypeWithBound<'a, 'ctx, E, &'a &'ctx ()>
for RecoverableKnown
{
type Higher = RecoverableKnown;
}
impl<E: Environment> HintMeta for RecoverableProto<E> {
type Known = RecoverableKnown;
type Hint = ();
type Effect = E;
}
pub fn visit_recoverable<'a, 'ctx, E: Environment>(
visitor: DynVisitor<'a, 'ctx, E>,
scope: DynRecoverableScope<'a, 'ctx, E>,
) -> NativeForm<'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.
E::value(VisitResult::Skipped(())).cast()
}
}
impl<'ctx, T, E: Environment> HasProtocol<RecoverableProto<E>> for T where T: Recoverable<'ctx, E> {}