[−][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 |
io | Tools and combinators for I/O. |
prelude | Async traits and their extensions. |
stream | Combinators for the |
Macros
pin | Pins a variable of type |
ready | Unwraps |
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
block_on | Starts a thread-local executor and then runs the future. |
run | Starts a thread-local and a multi-threaded executor and then runs the future. |