use crate::{
any::TypeName,
effect::{Effect, Effective, ErasedEffective},
higher_ranked_type,
hkt::Marker,
protocol::{
walker::hint::{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<DynRecoverableScope<'a, 'ctx, E>>, E>;
}
pub struct RecoverableProto<E: Effect>(Marker<E>);
higher_ranked_type! {
impl TypeName {
impl['a, 'ctx, E] type T['a, 'ctx] for RecoverableProto<E> =
dyn Recoverable<'ctx, E> + Send + Sync + 'a
where {
E: Effect
};
impl['a, 'ctx, E] type HigherRanked['a, 'ctx] for dyn Recoverable<'ctx, E> + Send + Sync + 'a =
RecoverableProto<E>
where {
E: Effect
};
}
}
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;
higher_ranked_type! {
impl Meta {
impl['a, 'ctx] type T['a, 'ctx] for RecoverableKnown =
RecoverableKnown;
impl['a, 'ctx] type HigherRanked['a, 'ctx] for RecoverableKnown =
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<DynRecoverableScope<'a, 'ctx, E>>, 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::ready(VisitResult::Skipped(scope)).into_erased()
}
}