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
//! The DynBus implementation used by `lifeline_bus!`, and TypeId-based slot storage.
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;

/// An extension trait which defines operations on a DynBus, which stores `box dyn` trait objects internally.
///
/// DynBus implementations are created using the `lifeline_bus!` macro.
pub trait DynBus: Bus {
    /// Stores an manually constructed Receiver on the bus, for the provided message type.
    ///
    /// This is useful if you need to link a lifeline bus to other async message-based code.
    ///
    /// If the message channel has already been linked (from a call to `bus.rx`, `bus.tx`, or `bus.capacity`), returns an error.
    fn store_rx<Msg>(&self, rx: <Msg::Channel as Channel>::Rx) -> Result<(), AlreadyLinkedError>
    where
        Msg: Message<Self> + 'static;

    /// Stores an manually constructed Sender on the bus, for the provided message type.
    ///
    /// This is useful if you need to link a lifeline bus to other async message-based code.
    ///
    /// If the message channel has already been linked (from a call to `bus.rx`, `bus.tx`, or `bus.capacity`), returns an error.
    fn store_tx<Msg>(&self, tx: <Msg::Channel as Channel>::Tx) -> Result<(), AlreadyLinkedError>
    where
        Msg: Message<Self> + 'static;

    /// Stores a channel pair on the bus, for the provided message type.
    ///
    /// If the message channel has already been linked (from a call to `bus.rx`, `bus.tx`, or `bus.capacity`), returns an error.
    fn store_channel<Msg>(
        &self,
        rx: <Msg::Channel as Channel>::Rx,
        tx: <Msg::Channel as Channel>::Tx,
    ) -> Result<(), AlreadyLinkedError>
    where
        Msg: Message<Self> + 'static;

    /// Stores a resource on the bus.
    ///
    /// Resources are commonly used for clonable configuration structs, or takeable resources such as websocket connections.
    fn store_resource<R: Resource<Self>>(&self, resource: R);

    /// Returns the `DynBusStorage` struct which manages the trait object slots.
    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>()
    }
}