Skip to main content

Module tokio_adapter

Module tokio_adapter 

Source
Available on crate features std and tokio-runtime only.
Expand description

Tokio runtime adapters.

This module provides a Pollable wrapper that runs a Future on the Tokio runtime and exposes its completion through the poll-based PK Command interface.

§Example

use pk_command::{PkHashmapMethod, tokio_adapter::TokioFuturePollable};

// use current_thread flavor just for simplifying the dependencies,
// use whatever you want in your actual application
#[tokio::main(flavor = "current_thread")]
async fn main() {
    tokio::spawn(async {
        let method_accessor = PkHashmapMethod::new(vec![(
            String::from("ECHOO"),
            Box::new(move |param: Option<Vec<u8>>| {
                TokioFuturePollable::from_future(async move {
                    // Note the `async` here     ^^^^^
                    Ok(param) // Echo back the input
                })
            }),
        )]);
        // let pk=....;
        loop {
            // now you can call pk.poll() as usual.
        }
    });

    // The async tasks you registered above, when called by the other side,
    // will run in the tokio runtime.
}

Structs§

TokioFuturePollable
A Pollable adapter that spawns a Future onto the Tokio runtime and exposes its completion through the Pollable interface.