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
use crate::Sockets;
use std::any::TypeId;

pub struct SocketDescription<T> {
    pub(crate) inputs: Vec<TypeId>,
    pub(crate) outputs: Vec<OutputSocket<T>>,
}

pub(crate) struct OutputSocket<T> {
    pub(crate) type_: TypeId,
    pub(crate) buffer: T,
}

impl<T> OutputSocket<T> {
    pub fn new<O: Into<T> + 'static>(buffer: O) -> Self {
        Self {
            type_: TypeId::of::<O>(),
            buffer: buffer.into(),
        }
    }
}

impl<T> SocketDescription<T> {
    pub fn new() -> Self {
        Self {
            inputs: Vec::new(),
            outputs: Vec::new(),
        }
    }

    pub fn push_input<I: 'static>(&mut self)
    where
        T: TryInto<I>,
    {
        self.inputs.push(TypeId::of::<I>())
    }

    pub fn push_output<O: Into<T> + Default + 'static>(&mut self) {
        self.push_output_with_buffer(O::default())
    }

    pub fn push_output_with_buffer<O: Into<T> + 'static>(&mut self, buffer: O) {
        self.outputs.push(OutputSocket::new(buffer))
    }
}

#[enum_delegate::register]
pub trait Node {
    type Context;
    type Data;

    fn sockets(&self) -> SocketDescription<Self::Data>;

    #[allow(unused_variables)]
    fn rt_transfer_state(&mut self, source: Self)
    where
        Self: Sized,
    {
    }

    fn rt_process(&mut self, context: &Self::Context, sockets: Sockets<Self::Data>);
}