[][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.

However, tokio is generally hostile towards non-tokio libraries, insists on non-standard I/O traits, and deliberately lacks documentation on integration with the larger Rust ecosystem. 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 an executor and runs the future on it.