[][src]Trait lifeline::Task

pub trait Task {
    fn task<Out>(
        name: &str,
        fut: impl Future<Output = Out> + Send + 'static
    ) -> Lifeline
    where
        Out: Debug + Send + 'static,
        Self: Sized
, { ... }
fn try_task<Out>(
        name: &str,
        fut: impl Future<Output = Result<Out>> + Send + 'static
    ) -> Lifeline
    where
        Out: Debug + 'static,
        Self: Sized
, { ... } }

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

Lifeline supports the following task executors (using feature flags), and will use the first enabled flag:

  • tokio-executor
  • async-std-executor

Fallible tasks can be invoked with Self::try_task. Lifeline will log OK/ERR status when the task finishes.

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 })
    }
}

Provided methods

fn task<Out>(
    name: &str,
    fut: impl Future<Output = Out> + Send + 'static
) -> Lifeline where
    Out: Debug + Send + 'static,
    Self: Sized

Spawns an infallible task using the provided executor, wrapping it in a Lifeline handle. The task will run until it finishes, or until the Lifeline is droped.

fn try_task<Out>(
    name: &str,
    fut: impl Future<Output = Result<Out>> + Send + 'static
) -> Lifeline where
    Out: Debug + 'static,
    Self: Sized

Spawns an fallible task using the provided executor, wrapping it in a Lifeline handle. The task will run until it finishes, or until the Lifeline is droped.

If the task finishes, lifeline will log an 'OK' or 'ERR' message with the return value.

Loading content...

Implementors

impl<T> Task for T[src]

Loading content...