sound_flow 0.3.0

Execute graphs of functions in real time
Documentation
use crate::graph::BufferAddress;
use std::any::TypeId;

pub struct Sockets<'a, Data> {
    pub(crate) socket_inputs: &'a [BufferAddress],
    pub(crate) socket_outputs: &'a [BufferAddress],
    pub(crate) buffers: &'a mut [Data],
}

impl<'a, Data> Sockets<'a, Data> {
    pub fn input<'b, T: 'static>(&'b self, index: usize) -> &'b T
    where
        &'b Data: TryInto<&'b T>,
    {
        let address = self
            .socket_inputs
            .get(index)
            .expect("requested input socket out of bounds");

        assert_eq!(address.type_, TypeId::of::<T>());

        let Ok(i) = (&self.buffers[address.index]).try_into() else {
            panic!("Could not convert buffer into socket type")
        };
        i
    }

    pub fn output_mut<'b, T: 'static>(&'b mut self, index: usize) -> &'b mut T
    where
        &'b mut Data: TryInto<&'b mut T>,
    {
        let address = self
            .socket_outputs
            .get(index)
            .expect("requested input socket out of bounds");

        assert_eq!(address.type_, TypeId::of::<T>());

        let Ok(i) = (&mut self.buffers[address.index]).try_into() else {
            panic!("Could not convert buffer into socket type")
        };
        i
    }
}