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
112
113
//! [`Protocol`] for giving a visitor an owned value.
//!
//! In some sense, this is the most basic protocol.

use crate::{
    any::TypeName,
    effect::{Effect, ErasedEffective, ReadyExt as _},
    hkt::Marker,
    protocol::{
        walker::hint::{HintMeta, Meta},
        DynVisitor,
    },
};

use super::VisitResult;

/// Trait object for the [`Value`] protocol.
///
/// Types implementing the [`Value`] protocol will implement this trait.
pub trait Value<'ctx, T: ?Sized + TypeName::MemberType, E: Effect> {
    /// Visit a value of type `T`.
    ///
    /// Use this to give a value to a visitor. Its expected that a walker
    /// only calls this once per usage of the trait object, but that is not
    /// forced.
    ///
    /// If a [`ControlFlow::Break`] is returned then the walker
    /// should stop walking as soon as possible as there has likely been
    /// and error.
    fn visit<'a>(
        &'a mut self,
        value: TypeName::T<'a, 'ctx, T>,
    ) -> ErasedEffective<'a, VisitResult<TypeName::T<'a, 'ctx, T>>, E>
    where
        TypeName::T<'a, 'ctx, T>: Send + Sync + Sized,
        'ctx: 'a;
}

pub struct ValueProto<T: ?Sized + TypeName::MemberType, E: Effect>(Marker<(*const T, E)>);

impl<'a, 'ctx, T: ?Sized, E> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> for ValueProto<T, E>
where
    E: Effect,
    T: TypeName::MemberType,
{
    type T = dyn Value<'ctx, T, E> + Send + Sync + 'a;
}

impl<'a, 'ctx, T: ?Sized, E> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>
    for dyn Value<'ctx, T, E> + Send + Sync + 'a
where
    E: Effect,
    T: TypeName::MemberType,
{
    type Higher = ValueProto<T, E>;
}

#[derive(Copy, Clone, PartialEq, Debug)]
pub struct ValueKnown<'a, T: ?Sized> {
    /// A preview of the value.
    ///
    /// This can be used to inspect the value before committing to a visit.
    pub preview: Option<&'a T>,
}

#[derive(Copy, Clone, Debug)]
pub struct ValueKnownHrt<T: ?Sized>(Marker<T>);

impl<'a, 'ctx, T> Meta::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> for ValueKnownHrt<T>
where
    T: ?Sized + TypeName::MemberType,
    // for<'a, 'ctx> TypeName::T<'a, 'ctx, T>: Send + Sync,
{
    type T = ValueKnown<'a, TypeName::T<'a, 'ctx, T>>;
}

impl<'a, 'ctx, T: ?Sized> Meta::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()> for ValueKnown<'a, T>
where
    T: TypeName::LowerType<'a, 'ctx>,
{
    type Higher = ValueKnownHrt<TypeName::HigherRanked<'a, 'ctx, T>>;
}

// This enrolls the Value protocol into the walker hint system.
impl<T: TypeName::MemberType, E: Effect> HintMeta for ValueProto<T, E> {
    type Known = ValueKnownHrt<T>;

    type Hint = ();

    type Effect = E;
}

pub fn visit_value<
    'ctx: 'visitor,
    'visitor: 'e,
    'e,
    T: Send + Sync + TypeName::LowerType<'e, 'ctx>,
    E: Effect,
>(
    visitor: DynVisitor<'visitor, 'ctx>,
    value: T,
) -> ErasedEffective<'e, VisitResult<T>, E> {
    if let Some(object) = visitor
        .0
        .upcast_mut::<ValueProto<TypeName::HigherRanked<'e, 'ctx, T>, E>>()
    {
        // Allow the visitor to give a hint if it wants.
        object.visit(value)
    } else {
        // If the visitor doesn't support request hint then we continue.
        VisitResult::Skipped(value).ready()
    }
}