use crate::{
any::{TypeName, WithContextLt},
bijective_higher_ranked_type,
effect::{Effect, Future},
protocol::{Visitor, Walker},
};
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: Walker<'a, 'ctx>,
) -> Future<'a, VisitResult<Walker<'a, 'ctx>>, E>;
}
bijective_higher_ranked_type! {
pub type DynRequestHint['ctx][E]: WithContextLt['ctx][]
for<'a>
(dyn RequestHint<'ctx, E> + Send + 'a)
where {
E: Effect
}
}
bijective_higher_ranked_type! {
pub type [][E]: TypeName[][]
for<'ctx>
(DynRequestHint<'ctx, E>)
where {
E: Effect
}
}
/// 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 visit_request_hint<'a, 'ctx, E: Effect>(
visitor: Visitor<'a, 'ctx>,
walker: Walker<'a, 'ctx>,
) -> Future<'a, VisitResult<Walker<'a, 'ctx>>, E> {
if let Some(object) = visitor.upcast_mut::<DynRequestHint<'ctx, 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.
E::ready(VisitResult::Skipped(walker))
}
}