[][src]Trait lifeline::Bus

pub trait Bus: Default + Debug + Sized {
    fn capacity<Msg>(&self, capacity: usize) -> Result<(), AlreadyLinkedError>
    where
        Msg: Message<Self> + 'static
;
fn rx<Msg>(
        &self
    ) -> Result<LifelineReceiver<Msg, <Msg::Channel as Channel>::Rx>, TakeChannelError>
    where
        Msg: Message<Self> + 'static
;
fn tx<Msg>(
        &self
    ) -> Result<LifelineSender<Msg, <Msg::Channel as Channel>::Tx>, TakeChannelError>
    where
        Msg: Message<Self> + 'static
;
fn resource<Res>(&self) -> Result<Res, TakeResourceError>
    where
        Res: Resource<Self>
; }

Stores and distributes channel endpoints (Senders and Receivers), as well as Resource values.

The bus allows you to write loosely-coupled applications, with adjacent lifeline Services that do not depend on each other, but can communicate with each other via shared message types.

Most Bus implementations are defined using the lifeline_bus! macro.

Example:

use lifeline::lifeline_bus;

lifeline_bus!(pub struct ExampleBus);

Required methods

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

Configures the channel capacity, if the linked channel implementation takes a capacity during initialization

Returns an AlreadyLinkedError, if the channel has already been initalized from another call to capacity, rx, or tx.

Example:

use lifeline::prelude::*;
use tokio::sync::mpsc;
lifeline_bus!(pub struct ExampleBus);
  
#[derive(Debug)]
struct ExampleMessage {}
impl Message<ExampleBus> for ExampleMessage {
    type Channel = mpsc::Sender<Self>;
}

fn main() {
    let bus = ExampleBus::default();
    bus.capacity::<ExampleMessage>(1024);
    let rx = bus.rx::<ExampleMessage>();
}

fn rx<Msg>(
    &self
) -> Result<LifelineReceiver<Msg, <Msg::Channel as Channel>::Rx>, TakeChannelError> where
    Msg: Message<Self> + 'static, 

Takes (or clones) the channel Receiver. The message type must implement Message<Bus>, which defines the channel type.

Returns the Receiver, or a TakeChannelError if the channel endpoint is not clonable, and has already been taken.

  • For mpsc channels, the Receiver is taken.
  • For broadcast channels, the Receiver is cloned.
  • For watch channels, the Receiver is cloned.

Example:

use lifeline::prelude::*;
use tokio::sync::mpsc;
lifeline_bus!(pub struct ExampleBus);
  
#[derive(Debug)]
struct ExampleMessage {}
impl Message<ExampleBus> for ExampleMessage {
    type Channel = mpsc::Sender<Self>;
}

fn main() {
    let bus = ExampleBus::default();
    let rx = bus.rx::<ExampleMessage>();
}

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

Takes (or clones) the channel Sender. The message type must implement Message<Bus>, which defines the channel type.

Returns the sender, or a TakeChannelError if the channel endpoint is not clonable, and has already been taken.

  • For mpsc channels, the Sender is cloned.
  • For broadcast channels, the Sender is cloned.
  • For watch channels, the Sender is taken.

Example:

use lifeline::prelude::*;
use tokio::sync::mpsc;
lifeline_bus!(pub struct ExampleBus);
  
#[derive(Debug)]
struct ExampleMessage {}
impl Message<ExampleBus> for ExampleMessage {
    type Channel = mpsc::Sender<Self>;
}

fn main() {
    let bus = ExampleBus::default();
    let tx = bus.tx::<ExampleMessage>();
}

fn resource<Res>(&self) -> Result<Res, TakeResourceError> where
    Res: Resource<Self>, 

Takes (or clones) the Resource.

Returns the resource, or a TakeResourceError if the resource is not clonable, and has already been taken.

Example:

use lifeline::prelude::*;
use lifeline::impl_storage_clone;
use tokio::sync::mpsc;
lifeline_bus!(pub struct ExampleBus);

#[derive(Debug, Clone)]
struct ExampleResource {}
impl_storage_clone!(ExampleResource);
impl Resource<ExampleBus> for ExampleResource {}

fn main() {
    let bus = ExampleBus::default();
    let resource = bus.resource::<ExampleResource>();
}
Loading content...

Implementors

impl<T> Bus for T where
    T: DynBus
[src]

Loading content...