smol/README.md

101 lines
3.2 KiB
Markdown
Raw Normal View History

2020-04-16 14:09:22 +00:00
# smol
2020-04-26 17:50:44 +00:00
[![Build](https://github.com/stjepang/smol/workflows/Build%20and%20test/badge.svg)](
2020-04-28 15:29:09 +00:00
https://github.com/stjepang/smol/actions)
2020-04-21 17:36:05 +00:00
[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)](
https://github.com/stjepang/smol)
[![Cargo](https://img.shields.io/crates/v/smol.svg)](
https://crates.io/crates/smol)
[![Documentation](https://docs.rs/smol/badge.svg)](
https://docs.rs/smol)
2020-04-27 14:06:56 +00:00
[![Chat](https://img.shields.io/discord/701824908866617385.svg?logo=discord)](
2020-04-27 10:32:50 +00:00
https://discord.gg/x6m5Vvt)
2020-04-16 14:09:22 +00:00
2020-07-23 10:24:16 +00:00
A small and fast async runtime.
2020-02-06 14:09:00 +00:00
2020-07-23 10:24:16 +00:00
## Examples
2020-02-06 14:09:00 +00:00
2020-07-23 10:24:16 +00:00
Connect to an HTTP website, make a GET request, and pipe the response to the standard output:
2020-02-04 10:27:09 +00:00
2020-07-23 10:24:16 +00:00
```rust
use async_net::TcpStream;
use smol::{io, prelude::*, Unblock};
2020-04-21 17:36:05 +00:00
2020-07-23 10:24:16 +00:00
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?;
2020-05-03 16:28:19 +00:00
2020-07-23 10:24:16 +00:00
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][get-request].
2020-04-21 17:36:05 +00:00
2020-07-23 10:24:16 +00:00
Look inside the [examples] directory for more.
2020-04-21 17:36:05 +00:00
2020-07-23 10:24:16 +00:00
[`async-net`]: https://docs.rs/async-net
[examples]: https://github.com/stjepang/smol/tree/master/examples
[get-request]: https://github.com/stjepang/smol/blob/master/examples/get-request.rs
2020-04-21 17:36:05 +00:00
## Compatibility
2020-07-23 10:24:16 +00:00
All async libraries work with smol out of the box.
2020-07-23 21:58:07 +00:00
The only exception is [tokio], which is traditionally incompatible with [futures] and crashes
when called from other executors. Fortunately, there are ways around it.
2020-04-21 17:36:05 +00:00
2020-07-23 10:24:16 +00:00
Enable the `tokio02` feature flag and `smol::run()` will create a minimal
tokio runtime for its libraries:
2020-04-21 17:36:05 +00:00
```toml
[dependencies]
2020-07-23 10:53:22 +00:00
smol = { version = "0.3", features = ["tokio02"] }
2020-04-21 17:36:05 +00:00
```
[tokio]: https://docs.rs/tokio
2020-07-23 21:50:31 +00:00
[futures]: https://docs.rs/futures
2020-04-21 17:36:05 +00:00
## TLS certificate
2020-04-27 23:29:30 +00:00
Some code examples are using TLS for authentication. The repository
2020-04-28 11:09:59 +00:00
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.
2020-04-21 17:36:05 +00:00
The certificate file was generated using
[minica](https://github.com/jsha/minica) and
[openssl](https://www.openssl.org/):
2020-04-20 15:45:14 +00:00
```
2020-04-21 17:36:05 +00:00
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
2020-04-20 15:45:14 +00:00
```
2020-07-23 13:04:13 +00:00
Another useful tool for making certificates is [mkcert].
[mkcert]: https://github.com/FiloSottile/mkcert
2020-02-04 10:27:09 +00:00
## License
2020-04-21 17:36:05 +00:00
Licensed under either of
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
2020-02-04 10:27:09 +00:00
2020-04-21 17:36:05 +00:00
#### Contribution
2020-02-04 10:27:09 +00:00
Unless you explicitly state otherwise, any contribution intentionally submitted
2020-04-21 17:36:05 +00:00
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.