Crate abscissa_tokio[][src]

Support for launching Tokio runtimes within Abscissa applications.

About

Where normally you'd use something like the tokio::main macro to launch the Tokio runtime, in Abscissa the framework is launched by calling abscissa_core::boot from your application's main().

This means Abscissa applications need a slightly different convention for starting the Tokio runtime, and ideally one which allows all application subcomponents to register themselves before the runtime is started.

This crate handles instantiating the Tokio runtime as an Abscissa Component, allowing other application components to express they have a Tokio dependency so Abscissa can inject the Tokio component as a dependency.

Requirements

  • Rust 1.45+
  • Abscissa 0.5
  • Tokio 0.2

Usage

Defining Abscissa components that depends on Tokio

To register an Abscissa component with the Tokio runtime, add TokioComponent as a dependency to be injected when the runtime is available:

use abscissa_core::{Component, FrameworkError};
use abscissa_tokio::TokioComponent;

#[derive(Component, Debug)]
#[component(inject = "init_tokio(abscissa_tokio::TokioComponent)")]
pub struct MyComponent {}

impl MyComponent {
    pub fn new() -> Result<Self, FrameworkError> {
        Ok(Self {})
    }

    /// Called automatically after `TokioComponent` is initialized
    pub fn init_tokio(&mut self, tokio_cmp: &TokioComponent) -> Result<(), FrameworkError> {
        // Register with the Tokio runtime here, e.g.:
        // `tokio_cmp.runtime()?.spawn(async { ... });`
        Ok(())
    }
}

Add TokioComponent to your Abscissa application

Inside of your app's src/application.rs, find the register_components method and add TokioComponent:

use abscissa_tokio::TokioComponent;

fn register_components(&mut self, command: &Self::Cmd) -> Result<(), FrameworkError> {
    let mut components = self.framework_components(command)?;

    // Create `TokioComponent` and add it to your app's components here:
    components.push(Box::new(TokioComponent::new()?));

    self.state.components.register(components)
}

Inside of the Runnable for one of your application's subcommands, call abscissa_tokio::run with a provided Future to launch the Tokio runtime:

use crate::application::APP;

impl Runnable for StartCmd {
   fn run(&self) {
       abscissa_tokio::run(&APP, async {
           println!("now running inside the Tokio runtime");
       });
   }
}

Re-exports

pub use tokio;

Structs

TokioComponent

Component which manages initialization of a Tokio runtime within the Abscissa application lifecycle.

Functions

run

Run a Future on the Runtime for the provided Application.