[][src]Crate smol

A small and fast async runtime.

Examples

Connect to an HTTP website, make a GET request, and pipe the response to the standard output:

use async_net::TcpStream;
use smol::{io, prelude::*, Unblock};

fn main() -> io::Result<()> {
    smol::run(async {
        let mut stream = TcpStream::connect("example.com:80").await?;
        let req = b"GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n";
        stream.write_all(req).await?;

        let mut stdout = Unblock::new(std::io::stdout());
        io::copy(&stream, &mut stdout).await?;
        Ok(())
    })
}

This example uses async-net for networking, but you can also use the primitive Async type. See the full code.

Look inside the examples directory for more.

Compatibility

All async libraries work with smol out of the box.

The only exception is tokio, which is traditionally incompatible with futures and crashes when called from other executors. Fortunately, there are ways around it.

Enable the tokio02 feature flag and smol::run() will create a minimal tokio runtime for its libraries:

[dependencies]
smol = { version = "0.3", features = ["tokio02"] }

Modules

future

Combinators for the Future trait.

io

Tools and combinators for I/O.

prelude

Async traits and their extensions.

stream

Combinators for the Stream trait.

Macros

pin

Pins a variable of type T on the stack and rebinds it as Pin<&mut T>.

ready

Unwraps Poll<T> or returns Pending.

unblock

Runs blocking code on a thread pool.

Structs

Async

Async I/O.

Task

A spawned future.

Timer

Fires at the chosen point in time.

Unblock

Runs blocking I/O on a thread pool.

Functions

run

Starts executors and runs the future.