[][src]Crate lifeline

Lifeline is a dependency injection library for message-based applications. Lifeline produces applications which are:

  • Clean: Bus implementations provide a high-level overview of the application, and services clearly define the messages they send and receive.
  • Decoupled: Services and tasks have no dependency on their peers, as they only depend on the message types they are sending or receiving.
  • Stoppable: Services and tasks are trivially cancellable. For example, you can terminate all tasks associated with a connection when a client disconnects.
  • Greppable: The impact/reach of a message can be easily understood by searching for the type in the source code.
  • Testable: Lifeline applications communicate via messages, which makes unit testing easy. Spawn the service, send a message, and expect a received message.

In order to achieve these goals, lifeline provides patterns, traits, and implementations:

  • The Bus, which constructs & distributes channel Senders/Receivers, and Resources.
  • The Carrier, which translates messages between two Bus instances. Carriers are critical when building large applications, and help minimize the complexity of the messages on each bus.
  • The Service, which takes channels from the bus, and spawns tasks which send and receive messages.
  • The Task, an async future which returns a lifeline when spawned. When the lifeline is dropped, the future is immedately cancelled.
  • The Resource, a struct which can be stored in the bus, and taken (or cloned) when services spawn.

For a quick introduction, see the hello.rs example. For a full-scale application see tab-rs.


Lifeline uses tokio as it's default runtime. Tokio provides a rich set of async channels.

lifeline = "0.3"

Lifeline also supports the async-std runtime, and it's mpsc channel implementation:

lifeline = { version = "0.3", features = ["dyn-bus", "async-std-executor", "async-std-channels"] }

The Bus

The Bus carries channels and resources, and allows you to write loosely coupled Service implementations which communicate over messages.

Channels can be taken from the bus. If the channel endpoint is clonable, it will remain available for other services.
If the channel is not clonable, future calls will receive an Err value. The Rx/Tx type parameters are type-safe, and will produce a compile error if you attempt to take a channel for an message type which the bus does not carry.

Lifeline provides a lifeline_bus! macro which stores channels and resources in Box<dyn> slots:

use lifeline::lifeline_bus;
lifeline_bus!(pub struct MainBus);

The Carrier

Carriers provide a way to move messages between busses. Carriers can translate, ignore, or collect information, providing each bus with the messages that it needs.

Large applications have a tree of Busses. This is good, it breaks your app into small chunks.

- MainBus
  | ConnectionListenerBus
  |  | ConnectionBus
  | DomainSpecificBus
  |  | ...

Carriers allow each bus to define messages that minimally represent the information it's services need to function, and prevent an explosion of messages which are copied to all busses.

Carriers centralize the communication between busses, making large applications easier to reason about.

The Service

The Service synchronously takes channels from the Bus, and spawns a tree of async tasks (which send & receive messages). When spawned, the service returns one or more Lifeline values. When a Lifeline is dropped, the associated task is immediately cancelled.

It's common for Service::spawn to return a Result. Taking channel endpoints is a fallible operation. Depending on the channel type, the endpoint may not be clonable. Lifeline clones endpoints when it can (e.g. for mpsc::Sender, broadcast::*, and watch::Receiver). Other endpoints are taken, removed, and future calls will return an Err.

Service::spawn takes channels from the bus synchronously, which makes errors occur predictably and early. If you get an Err on an mpsc::Receiver, change it's binding in the bus to broadcast::Sender.

The Task

The Task executes an Future, and returns a Lifeline when spawned. When the lifeline is dropped, the future is immediately cancelled.

Task trait is implemented for all types - you can import it and use Self::task in any type. In lifeline, it's most commonly used in Service implementations.

The Resource

Resources can be stored on the bus. This is very useful for configuration (e.g MainConfig), or connections (e.g. a TcpStream).

Resources implement the Storage trait, which is easy with the impl_storage_clone! and impl_storage_take! macros.



The DynBus implementation used by lifeline_bus!, and TypeId-based slot storage.


All the lifeline error types.


Prelude, including all the traits and types required for typical lifeline usage.


A request/response helper type, which can be sent over messages.


Helpers which assist in testing applications based on lifeline.



Asserts that the expression completes within a given number of milliseconds.


Asserts that the expression does not complete within a given number of milliseconds.


Specifies that this channel endpoint (Sender or Receiver) is clonable. Provides a generic type T with the bounds required for the implementation.


Specifies that this channel endpoint (Sender or Receiver) is taken. Provides a generic type T with the bounds required for the implementation.


Specifies that this resource is cloned.


Specifies that this resource is taken, and is !Clone.


Defines a lifeline bus: it's struct, Bus impl, and DynBus impl.



A lifeline value, associated with a future spawned via the Task trait. When the lifeline is dropped, the associated future is immediately cancelled.


A wrapper which provides a stable Receiver implementation, returned by bus.rx::<Msg>(). Can be unwrapped with into_inner()


A wrapper which provides a stable Sender implementation, returned by bus.tx::<Msg>(). Can be unwrapped with into_inner()



Represents the Sender, Receiver, or Both. Used in error types.



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


Carries messages between two bus instances. A variant of the Service.


The receprocial of the CarryFrom trait. Implemented for all types on which CarryFrom is implemented.


A channel's (Sender, Receiver) pair. Defines how the bus constructs and retrieves the values.


Constructs two bus types, and spawns the carrier between them. Returns both busses, and the carrier's lifeline.


Constructs the bus, spawns the service, and returns both.


Attaches a channel to the Bus, carrying Self as a message.


The receiver half of an asynchronous channel, which may be bounded/unbounded, mpsc/broadcast/oneshot, etc.


Attaches a resource to the Bus. This resource can accessed from the bus using bus.resource::<Self>().


The sender half of an asynchronous channel, which may be bounded/unbounded, mpsc/broadcast/oneshot, etc.


Takes channels from the Bus, and spawns a tree of tasks. Returns one or more Lifeline values.
When the Lifeline is dropped, the task tree is immediately cancelled.


Defines a resource (or channel endpoint) which can be stored on the bus, and how it is taken or cloned.


Provides the Self::task and Self::try_task associated methods for all types.