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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
mod macros;
mod slot;
mod storage;

use crate::{
    bus::{Message, Resource},
    channel::lifeline::{receiver::LifelineReceiver, sender::LifelineSender},
    error::{AlreadyLinkedError, TakeChannelError, TakeResourceError},
    Bus, Channel,
};

pub use storage::DynBusStorage;

pub trait DynBus: Bus {
    fn store_rx<Msg>(&self, rx: <Msg::Channel as Channel>::Rx) -> Result<(), AlreadyLinkedError>
    where
        Msg: Message<Self> + 'static;

    fn store_tx<Msg>(&self, tx: <Msg::Channel as Channel>::Tx) -> Result<(), AlreadyLinkedError>
    where
        Msg: Message<Self> + 'static;

    fn store_channel<Msg>(
        &self,
        rx: <Msg::Channel as Channel>::Rx,
        tx: <Msg::Channel as Channel>::Tx,
    ) -> Result<(), AlreadyLinkedError>
    where
        Msg: Message<Self> + 'static;

    fn store_resource<R: Resource<Self>>(&self, resource: R);

    // fn take_channel<Msg, Source>(&self, other: &Source) -> Result<(), TakeChannelError>
    // where
    //     Msg: Message<Self> + 'static,
    //     Msg: Message<Source, Channel = <Msg as Message<Self>>::Channel>,
    //     Source: DynBus;

    // fn take_rx<Msg, Source>(&self, other: &Source) -> Result<(), TakeChannelError>
    // where
    //     Msg: Message<Self> + 'static,
    //     Msg: Message<Source, Channel = <Msg as Message<Self>>::Channel>,
    //     Source: DynBus;

    // fn take_tx<Msg, Source>(&self, other: &Source) -> Result<(), TakeChannelError>
    // where
    //     Msg: Message<Self> + 'static,
    //     Msg: Message<Source, Channel = <Msg as Message<Self>>::Channel>,
    //     Source: DynBus;

    // fn take_resource<Res, Source>(&self, other: &Source) -> Result<(), TakeResourceError>
    // where
    //     Res: Storage,
    //     Res: Resource<Source>,
    //     Res: Resource<Self>,
    //     Source: DynBus;

    fn storage(&self) -> &DynBusStorage<Self>;
}

impl<T> Bus for T
where
    T: DynBus,
{
    fn rx<Msg>(
        &self,
    ) -> Result<LifelineReceiver<Msg, <Msg::Channel as Channel>::Rx>, TakeChannelError>
    where
        Msg: crate::bus::Message<Self> + 'static,
    {
        self.storage().link_channel::<Msg, Self>();
        let rx = self.storage().clone_rx::<Msg, Self>()?;
        Ok(LifelineReceiver::new(rx))
    }

    fn tx<Msg>(
        &self,
    ) -> Result<LifelineSender<Msg, <Msg::Channel as Channel>::Tx>, TakeChannelError>
    where
        Msg: crate::bus::Message<Self> + 'static,
    {
        self.storage().link_channel::<Msg, Self>();
        let tx = self.storage().clone_tx::<Msg, Self>()?;
        Ok(LifelineSender::new(tx))
    }

    fn capacity<Msg>(&self, capacity: usize) -> Result<(), AlreadyLinkedError>
    where
        Msg: Message<Self> + 'static,
    {
        self.storage().capacity::<Msg>(capacity)
    }

    fn resource<Res>(&self) -> Result<Res, TakeResourceError>
    where
        Res: Resource<Self>,
    {
        self.storage().clone_resource::<Res>()
    }
}