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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//! [`Protocol`] for giving a visitor an owned value.
//!
//! In some sense, this is the most basic protocol.

use crate::{
    nameable,
    protocol::{walker::HintMeta, ControlFlow},
};

/// Trait object for the [`Value`] protocol.
///
/// Types implementing the [`Value`] protocol will implement this trait.
pub trait Value<T> {
    /// 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(&mut self, value: T) -> ControlFlow;
}

nameable! {
    pub ['a, 'ctx, T]
    dyn Value<T> + 'a where {T: ?Sized}
    dyn Value<T::Nameable> + 'a where {T: ?Sized}
}

// This enrolls the Value protocol into the walker hint system.
impl<'ctx, T> HintMeta<'ctx> for dyn Value<T> + '_ {
    type Known<'a> = () where 'ctx: 'a;

    type Hint = ();
}

#[cfg(test)]
mod test {
    use crate::{
        any::{AnyTrait, Borrowed, BorrowedMut, Owned},
        any_trait,
    };

    use super::*;

    #[test]
    fn visit() {
        struct Visitor(Option<i32>);

        impl Value<Owned<i32>> for Visitor {
            fn visit(&mut self, Owned(value): Owned<i32>) -> ControlFlow<()> {
                self.0 = Some(value);
                ControlFlow::Continue(())
            }
        }

        impl Value<Borrowed<'_, i32>> for Visitor {
            fn visit(&mut self, Borrowed(value): Borrowed<'_, i32>) -> ControlFlow<()> {
                self.0 = Some(*value);
                ControlFlow::Continue(())
            }
        }

        any_trait! {
            impl['a, 'ctx] Visitor = [
                dyn Value<Owned<i32>>,
                dyn Value<Borrowed<'ctx, i32>>,
            ];
        }

        let mut v = Visitor(None);
        let object: &mut dyn AnyTrait<'_> = &mut v;
        object
            .upcast_mut::<dyn Value<Owned<i32>>>()
            .unwrap()
            .visit(Owned(42));

        assert_eq!(v.0, Some(42));

        let object: &mut dyn AnyTrait<'_> = &mut v;
        object
            .upcast_mut::<dyn Value<Borrowed<'_, i32>>>()
            .unwrap()
            .visit(Borrowed(&101));

        assert_eq!(v.0, Some(101));
    }

    #[test]
    fn visit_borrowed() {
        struct Visitor<'ctx>(Option<&'ctx mut String>);

        impl<'ctx> Value<BorrowedMut<'ctx, String>> for Visitor<'ctx> {
            fn visit(&mut self, BorrowedMut(value): BorrowedMut<'ctx, String>) -> ControlFlow<()> {
                self.0 = Some(value);
                ControlFlow::Continue(())
            }
        }

        any_trait! {
            impl['a, 'ctx] Visitor<'ctx> = [
                dyn Value<BorrowedMut<'ctx, String>> + 'a,
            ];
        }

        let mut v = Visitor(None);

        let mut y = String::from("abc");
        let object: &mut dyn AnyTrait<'_> = &mut v;
        object
            .upcast_mut::<dyn Value<_>>()
            .unwrap()
            .visit(BorrowedMut(&mut y));

        v.0.unwrap().push_str("def");
        assert_eq!(y, "abcdef");
    }

    #[test]
    fn visit_borrowed_unsized() {
        struct Visitor<'ctx>(Option<&'ctx str>);

        impl<'ctx> Value<Borrowed<'ctx, str>> for Visitor<'ctx> {
            fn visit(&mut self, Borrowed(value): Borrowed<'ctx, str>) -> ControlFlow<()> {
                self.0 = Some(value);
                ControlFlow::Continue(())
            }
        }

        any_trait! {
            impl['a, 'ctx] Visitor<'ctx> = [
                dyn Value<Borrowed<'ctx, str>> + 'a,
            ];
        }

        let mut v = Visitor(None);

        let y = String::from("abc");
        let object: &mut dyn AnyTrait<'_> = &mut v;
        object
            .upcast_mut::<dyn Value<Borrowed<'_, str>>>()
            .unwrap()
            .visit(Borrowed(&y));

        assert_eq!(v.0, Some("abc"));
    }
}