A small and fast async runtime.
Connect to an HTTP website, make a GET request, and pipe the response to the standard output:
```rust 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.
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:
toml
[dependencies]
smol = { version = "0.2", features = ["tokio02"] }
Some code examples are using TLS for authentication. The repository contains a self-signed certificate usable for testing, but it should not be used for real-world scenarios. Browsers and tools like curl will show this certificate as insecure.
In browsers, accept the security prompt or use curl -k
on the
command line to bypass security warnings.
The certificate file was generated using minica and openssl:
minica --domains localhost -ip-addresses 127.0.0.1 -ca-cert certificate.pem
openssl pkcs12 -export -out identity.pfx -inkey localhost/key.pem -in localhost/cert.pem
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.