4 releases (breaking)

0.4.0 Dec 20, 2019
0.3.0 Dec 19, 2019
0.2.0 Apr 17, 2019
0.1.0 Mar 16, 2019
Download history 6/week @ 2020-10-21 28/week @ 2020-10-28 18/week @ 2020-11-04 5/week @ 2020-11-11 5/week @ 2020-11-18 6/week @ 2020-11-25 11/week @ 2020-12-02 6/week @ 2020-12-09 1/week @ 2020-12-16 4/week @ 2020-12-30 5/week @ 2021-01-06 7/week @ 2021-01-13 4/week @ 2021-01-20 3/week @ 2021-01-27 7/week @ 2021-02-03

82 downloads per month
Used in fie-ffi

Apache-2.0

17KB
298 lines

tokio-global

Build Crates.io Documentation

Simple way to create global tokio runtime, available from any place in code

Usage

Start runtime and use AutoRuntime trait to spawn your futures:

use tokio_global::{Runtime, AutoRuntime};
use tokio::io::{AsyncWriteExt, AsyncReadExt};

async fn server() {
    let mut listener = tokio::net::TcpListener::bind("127.0.0.1:8080").await.expect("To bind");

    let (mut socket, _) = listener.accept().await.expect("To accept connection");

    async move {
        let mut buf = [0; 1024];
        loop {
            match socket.read(&mut buf).await {
                // socket closed
                Ok(0) => return,
                Ok(_) => continue,
                Err(_) => panic!("Error :("),
            };
        }
    }.spawn().await.expect("Finish listening");
}

async fn client() {
    let mut stream = tokio::net::TcpStream::connect("127.0.0.1:8080").await.expect("Connect");

    // Write some data.
    stream.write_all(b"hello world!").await.expect("Write");

    //Stop runtime
    Runtime::stop();
}

let _guard = Runtime::default();

let runner = std::thread::spawn(|| {
    Runtime::run();
});

server().spawn();
client().spawn();

Dependencies

~2MB
~27K SLoC