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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use effectful::{
    effective::Effective,
    environment::{DynBind, EnvConfig, Environment, NativeForm},
    SendSync,
};

use crate::{
    any::TypeName,
    hkt::Marker,
    protocol::{
        walker::hint::{HasProtocol, HintMeta, Meta},
        DynVisitor,
    },
    Flow,
};

use super::VisitResult;

/// Protocol for visiting a sequence.
///
/// This protocol uses a scope to give temporary control to the visitor.
/// The visitor will drive the walker for each item.
pub trait Sequence<'ctx, E: Environment>: DynBind<E> {
    fn visit<'a: 'c, 'b: 'c, 'c>(
        &'a mut self,
        scope: DynSequenceScope<'b, 'ctx, E>,
    ) -> NativeForm<'c, VisitResult, E>
    where
        'ctx: 'a;
}

#[derive(SendSync)]
pub struct SequenceProto<E: Environment>(Marker<E>);

impl<'a, 'ctx, E> TypeName::MemberTypeForLt<'a, 'ctx, E, &'a &'ctx ()> for SequenceProto<E>
where
    E: Environment,
{
    type T = dyn Sequence<'ctx, E> + 'a;
}

impl<'a, 'ctx, E> TypeName::LowerTypeWithBound<'a, 'ctx, E, &'a &'ctx ()>
    for dyn Sequence<'ctx, E> + 'a
where
    E: Environment,
{
    type Higher = SequenceProto<E>;
}

pub trait SequenceScope<'ctx, E: Environment>: DynBind<E> {
    fn size_hint(&mut self) -> NativeForm<'_, (usize, Option<usize>), E>;

    fn next<'a: 'c, 'b: 'c, 'c>(
        &'a mut self,
        visitor: DynVisitor<'b, 'ctx, E>,
    ) -> NativeForm<'c, Flow, E>
    where
        'ctx: 'c + 'a + 'b;
}

pub type DynSequenceScope<'a, 'ctx, E> = &'a mut (dyn SequenceScope<'ctx, E> + 'a);

#[derive(Default, SendSync)]
pub struct SequenceKnown {
    pub len: (usize, Option<usize>),
}

impl<'a, 'ctx, E: EnvConfig> Meta::MemberTypeForLt<'a, 'ctx, E, &'a &'ctx ()> for SequenceKnown {
    type T = SequenceKnown;
}

impl<'a, 'ctx, E: EnvConfig> Meta::LowerTypeWithBound<'a, 'ctx, E, &'a &'ctx ()> for SequenceKnown {
    type Higher = SequenceKnown;
}

#[derive(SendSync)]
pub struct SequenceHint {
    pub len: (usize, Option<usize>),
}

impl<'a, 'ctx, E: EnvConfig> Meta::MemberTypeForLt<'a, 'ctx, E, &'a &'ctx ()> for SequenceHint {
    type T = SequenceHint;
}

impl<'a, 'ctx, E: EnvConfig> Meta::LowerTypeWithBound<'a, 'ctx, E, &'a &'ctx ()> for SequenceHint {
    type Higher = SequenceHint;
}

impl<E: Environment> HintMeta for SequenceProto<E> {
    type Known = SequenceKnown;

    type Hint = SequenceHint;

    type Effect = E;
}

#[inline(always)]
pub fn visit_sequence<'a, 'ctx, E: Environment>(
    visitor: DynVisitor<'a, 'ctx, E>,
    scope: DynSequenceScope<'a, 'ctx, E>,
) -> NativeForm<'a, VisitResult, E> {
    if let Some(object) = visitor.0.upcast_mut::<SequenceProto<E>>() {
        // Allow the visitor to walk the sequence scope.
        object.visit(scope)
    } else {
        // If the visitor doesn't support sequence then we continue.
        E::value(VisitResult::Skipped(())).cast()
    }
}

impl<'ctx, T, E: Environment> HasProtocol<SequenceProto<E>> for T where T: Sequence<'ctx, E> {}