use crate::{
any::TypeName,
effect::{Effect, ErasedEffective, ReadyExt as _},
hkt::Marker,
protocol::{DynVisitor, DynWalker},
};
use super::VisitResult;
/// Protocol for requesting a hint from a visitor.
pub trait RequestHint<'ctx, E: Effect> {
/// Call this to request a hint.
///
/// `walker` is what the visitor (`self`) will call to give a hint using the
/// [`Hint`][crate::builtins::walker::Hint] protocol.
fn request_hint<'a>(
&'a mut self,
walker: DynWalker<'a, 'ctx>,
) -> ErasedEffective<'a, VisitResult<DynWalker<'a, 'ctx>>, E>;
}
pub struct RequestHintProto<E: Effect>(Marker<E>);
impl<'a, 'ctx, E> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> for RequestHintProto<E>
where
E: Effect,
{
type T = dyn RequestHint<'ctx, E> + Send + Sync + 'a;
}
impl<'a, 'ctx, E> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>
for dyn RequestHint<'ctx, E> + Send + Sync + 'a
where
E: Effect,
{
type Higher = RequestHintProto<E>;
}
/// Visit using the [`RequestHint`] protocol.
///
/// If [`Flow::Continue`] is returned then the visitor wants/needs more information it didn't get
/// from the hints.
/// If [`Flow::Done`] is returned then the visitor doesn't need any more information and the walker
/// should stop walking.
/// If [`Flow::Break`] is returned then there was an error and the walker should stop walking.
pub fn request_hint<'a, 'ctx, E: Effect>(
visitor: DynVisitor<'a, 'ctx>,
walker: DynWalker<'a, 'ctx>,
) -> ErasedEffective<'a, VisitResult<DynWalker<'a, 'ctx>>, E> {
if let Some(object) = visitor.0.upcast_mut::<RequestHintProto<E>>() {
// Allow the visitor to give a hint if it wants.
object.request_hint(walker)
} else {
// If the visitor doesn't support request hint then we continue.
VisitResult::Skipped(walker).ready()
}
}