rustasync / runtime

Empowering everyone to build asynchronous software
https://docs.rs/runtime
Apache License 2.0
862 stars 28 forks source link

How can I use runtime in main without async #51

Closed SephVelut closed 5 years ago

SephVelut commented 5 years ago
// Cargo.toml
// [dependencies]
// runtime = { version = "0.3.0-alpha.5", feature = ["native"] }
// futures-preview = "0.3.0-alpha.16"

#![feature(async_await)]
use futures::prelude::*;
use runtime::net::TcpListener;
use futures::executor::block_on;

async fn listener() -> std::io::Result<()> {
    let mut listener = TcpListener::bind("127.0.0.1:8080")?;

    let mut sockets = listener.incoming();
    while let Some(socket) = sockets.next().await {
        match socket {
            Ok(s) => {
                runtime::spawn(async move {
                    let (reader, writer) = &mut s.split();
                    reader.copy_into(writer).await?;

                    Ok::<(), std::io::Error>(())
                }).await?;
            },
            Err(_) => (),
        }
    }

    Ok::<(), std::io::Error>(())
}

fn main() {
    block_on(listener());
}

thread 'main' panicked at 'the runtime has not been set', src/libcore/option.rs:1034:5

sdroege commented 5 years ago

Related to https://github.com/rustasync/runtime/issues/42 . There's currently no API to explicitly run a runtime.

SephVelut commented 5 years ago

Ah okay thank you