Crate smol

source · []
Expand description

A small and fast async runtime.

This crate simply re-exports other smaller async crates (see the source).

To use tokio-based libraries with smol, apply the async-compat adapter to futures and I/O types.

Examples

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

use smol::{io, net, prelude::*, Unblock};

fn main() -> io::Result<()> {
    smol::block_on(async {
        let mut stream = net::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(())
    })
}

There’s a lot more in the examples directory.

Modules

An async multi-producer multi-consumer channel.

Async filesystem primitives.

Combinators for the Future trait.

Tools and combinators for I/O.

Async synchronization primitives.

Async networking primitives for TCP/UDP/Unix communication.

Traits Future, Stream, AsyncRead, AsyncWrite, AsyncBufRead, AsyncSeek, and their extensions.

Async interface for working with processes.

Combinators for the Stream trait.

Macros

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

Unwraps Poll<T> or returns Pending.

Structs

Async adapter for I/O types.

An async executor.

A thread-local executor.

A spawned task.

A future or stream that emits timed events.

Runs blocking I/O on a thread pool.

Functions

Blocks the current thread on a future, processing I/O events when idle.

Spawns a task onto the global executor (single-threaded by default).

Runs blocking code on a thread pool.