Go to file
Stjepan Glavina 9cffe01a92 Big refactor 2020-07-23 12:24:16 +02:00
.github Remove cross-compilation 2020-07-14 22:59:52 +02:00
examples Big refactor 2020-07-23 12:24:16 +02:00
src Big refactor 2020-07-23 12:24:16 +02:00
.gitignore Lots of changes 2020-04-21 17:51:24 +02:00
CHANGELOG.md Bump to v0.2.0 2020-07-20 11:40:55 +02:00
CODE_OF_CONDUCT.md Update CODE_OF_CONDUCT.md 2020-05-16 22:34:11 +02:00
Cargo.toml Big refactor 2020-07-23 12:24:16 +02:00
LICENSE-APACHE
LICENSE-MIT
README.md Big refactor 2020-07-23 12:24:16 +02:00

README.md

smol

Build License Cargo Documentation Chat

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 a special case due to its generally hostile attitude towards non-tokio libraries, insistence on non-standard I/O traits, and lack of 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.2", features = ["tokio02"] }

TLS certificate

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

License

Licensed under either of

at your option.

Contribution

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.