sound_flow/graph/
sockets.rs

1use crate::graph::BufferAddress;
2use std::any::TypeId;
3
4pub struct Sockets<'a, Data> {
5    pub(crate) socket_inputs: &'a [BufferAddress],
6    pub(crate) socket_outputs: &'a [BufferAddress],
7    pub(crate) buffers: &'a mut [Data],
8}
9
10impl<'a, Data> Sockets<'a, Data> {
11    pub fn input<'b, T: 'static>(&'b self, index: usize) -> &'b T
12    where
13        &'b Data: TryInto<&'b T>,
14    {
15        let address = self
16            .socket_inputs
17            .get(index)
18            .expect("requested input socket out of bounds");
19
20        assert_eq!(address.type_, TypeId::of::<T>());
21
22        let Ok(i) = (&self.buffers[address.index]).try_into() else {
23            panic!("Could not convert buffer into socket type")
24        };
25        i
26    }
27
28    pub fn output_mut<'b, T: 'static>(&'b mut self, index: usize) -> &'b mut T
29    where
30        &'b mut Data: TryInto<&'b mut T>,
31    {
32        let address = self
33            .socket_outputs
34            .get(index)
35            .expect("requested input socket out of bounds");
36
37        assert_eq!(address.type_, TypeId::of::<T>());
38
39        let Ok(i) = (&mut self.buffers[address.index]).try_into() else {
40            panic!("Could not convert buffer into socket type")
41        };
42        i
43    }
44}