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
use crate::{build::protocols, implementer, protocol::ImplementerExt, walk, Builder};

use super::IncompleteValue;

pub struct OwnedBuilder<T> {
    value: Option<T>,
}

impl<T> Default for OwnedBuilder<T> {
    fn default() -> Self {
        Self { value: None }
    }
}

impl<'ctx, T: 'static> Builder<'ctx> for OwnedBuilder<T> {
    type Error = IncompleteValue;

    type Value = T;

    fn as_visitor(&mut self) -> &mut dyn crate::protocol::Implementer<'ctx> {
        self
    }

    fn build(self) -> Result<Self::Value, Self::Error> {
        self.value.ok_or(IncompleteValue)
    }
}

implementer! {
    impl['ctx, T: 'static] OwnedBuilder<T> = [
        protocols::owned::Owned<T>,
        protocols::hint::RequestHint,
    ];
}

impl<'ctx, T: 'static> protocols::hint::RequestHintObject<'ctx> for OwnedBuilder<T> {
    fn request_hint(
        &mut self,
        hints: &mut dyn crate::protocol::Implementer<'ctx>,
    ) -> Result<(), ()> {
        if let Some(interface) =
            hints.interface_for::<walk::protocols::hint::Hint<protocols::owned::Owned<T>>>()
        {
            interface.as_object().hint(self, ())
        } else {
            Ok(())
        }
    }
}

impl<'ctx, T: 'static> protocols::owned::Object<'ctx, T> for OwnedBuilder<T> {
    fn visit(&mut self, value: T) -> Result<(), ()> {
        self.value = Some(value);
        Ok(())
    }
}