1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use crate::{
    effect::{Effect, Future},
    nameable,
    protocol::{Visitor, Walker},
    Flow,
};

/// Protocol for requesting a hint from a visitor.
pub trait RequestHint<'ctx, E: Effect<'ctx>> {
    /// 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, 'ctx, Flow, E>;
}

pub type DynRequestHint<'a, 'ctx, E> = dyn RequestHint<'ctx, E> + Send + 'a;

nameable! {
    pub struct Name['a, 'ctx, E];
    impl [E] for DynRequestHint<'a, 'ctx, E> where {
        E: Effect<'ctx>,
        'ctx: 'a
    }
}

/// 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<'ctx>>(
    visitor: Visitor<'a, 'ctx>,
    walker: Walker<'a, 'ctx>,
) -> Future<'a, 'ctx, Flow, 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(Flow::Continue)
    }
}