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
use uniserde::{transform::from, build::{Builder, Build}, Visitor, ControlFlow, protocol::{ProtocolId, AnyVisit, Visit}, protocols::sequence, impls::core::iterator::IterWalker, Walker};

#[test]
fn demo() {
    let x = ["1", "2", "3"];

    let mut builder = VecBuilder::default();
    IterWalker::new(x.iter()).walk(builder.as_visitor());
    dbg!(builder.build());

    todo!();
}

#[no_mangle]
pub fn example<'a>(x: &'a str) -> &'a str {
    from(x).unwrap()
}

#[derive(Default)]
pub struct VecBuilder(Vec<&'static str>);

impl Builder<'static> for VecBuilder {
    type Error = ();

    type Value = Vec<&'static str>;

    fn as_visitor(&mut self) -> &mut dyn uniserde::Visitor<'static> {
        self
    }

    fn build(self) -> Result<Self::Value, Self::Error> {
        Ok(self.0)
    }
}

impl Visitor<'static> for VecBuilder {
    fn request_hint(
        &mut self,
        _hints: &mut dyn uniserde::WalkerHints<'static>,
        _need_hint: bool,
    ) -> uniserde::ControlFlow {
        ControlFlow::Continue
    }

    fn protocol(&mut self, id: uniserde::protocol::ProtocolId) -> Option<uniserde::protocol::AnyVisit<'_, 'static>> {
        if id == ProtocolId::of::<sequence::Sequence>() {
            Some(AnyVisit::new(self))
        } else {
            None
        }
    }
}

impl Visit<'static, sequence::Sequence> for VecBuilder {
    fn visit(&mut self, accessor: <sequence::Sequence as uniserde::protocol::Protocol>::Accessor<'_, 'static>) -> ControlFlow {
        loop {
            let mut builder = <&'static str as Build<'static>>::Builder::default();
            match accessor.next(builder.as_visitor()) {
                ControlFlow::Continue => {},
                ControlFlow::Done => {},
                ControlFlow::Error => break ControlFlow::Error,
            }

            match builder.build() {
                Ok(value) => self.0.push(value),
                Err(err) => break ControlFlow::Error,
            }
        }
    }
}