Trait lifeline::prelude::Service [−][src]
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.
- Simple implementations can return the Lifeline value, a handle returned by Task::task.
- Implementations which have fallible spawns can return
anyhow::Result<Lifeline>
. - Implementations which spawn multiple tasks can store lifelines for each task in self, and return
anyhow::Result<Self>
.
Example
use lifeline::prelude::*; use tokio::sync::mpsc; lifeline_bus!(pub struct ExampleBus); #[derive(Debug, Clone)] struct ExampleMessage {} impl Message<ExampleBus> for ExampleMessage { type Channel = mpsc::Sender<Self>; } struct ExampleService { _run: Lifeline } impl Service for ExampleService { type Bus = ExampleBus; type Lifeline = anyhow::Result<Self>; fn spawn(bus: &ExampleBus) -> anyhow::Result<Self> { let mut rx = bus.rx::<ExampleMessage>()?; let _run = Self::task("run", async move { while let Some(msg) = rx.recv().await { log::info!("got message: {:?}", msg); } }); Ok(Self { _run }) } } async fn run() { let bus = ExampleBus::default(); let _service = ExampleService::spawn(&bus); }
Associated Types
type Bus: Bus
[src]
The bus, which must be provided to spawn the task
type Lifeline
[src]
The service lifeline. When dropped, all spawned tasks are immediately cancelled.
Required methods
fn spawn(bus: &Self::Bus) -> Self::Lifeline
[src]
Spawns the service with all sub-tasks, and returns a lifeline value. When the lifeline is dropped, all spawned tasks are immediately cancelled.
Implementations should synchronously take channels from the bus, and then use them asynchronously. This makes errors occur as early and predictably as possible.