12 releases

new 0.1.11 Aug 7, 2020
0.1.10 Aug 6, 2020
0.1.9 Jul 31, 2020
0.1.1 Jun 29, 2020
0.1.0 May 18, 2019

#62 in Asynchronous

Download history 2/week @ 2020-04-17 1/week @ 2020-04-24 2/week @ 2020-05-01 1/week @ 2020-05-08 2/week @ 2020-05-15 1/week @ 2020-05-22 1/week @ 2020-05-29 2/week @ 2020-06-05 1/week @ 2020-06-12 34/week @ 2020-06-26 79/week @ 2020-07-03 284/week @ 2020-07-10 1850/week @ 2020-07-17 2759/week @ 2020-07-24 2792/week @ 2020-07-31

2,753 downloads per month
Used in 24 crates (13 directly)

Apache-2.0 OR MIT

76KB
882 lines

async-io

Build License Cargo Documentation

Async I/O and timers.

This crate provides two tools:

  • Async, an adapter for standard networking types (and many other types) to use in async programs.
  • Timer, a future that expires after a duration of time.

For concrete async networking types built on top of this crate, see async-net.

Implementation

The first time Async or Timer is used, a thread named "async-io" will be spawned. The purpose of this thread is to wait for I/O events reported by the operating system, and then wake appropriate futures blocked on I/O or timers when they can be resumed.

To wait for the next I/O event, the "async-io" thread uses epoll on Linux/Android/illumos, kqueue on macOS/iOS/BSD, and wepoll on Windows.

However, note that you can also process I/O events and wake futures manually if using the parking module. The "async-io" thread is therefore just a fallback mechanism processing I/O events in case you forget to or choose not to do that manually.

See the parking module for more details.

Examples

Connect to example.com:80, or time out after 10 seconds.

use async_io::{Async, Timer};
use futures_lite::{future::FutureExt, io};

use std::net::{TcpStream, ToSocketAddrs};
use std::time::Duration;

let addr = "example.com:80".to_socket_addrs()?.next().unwrap();

let stream = Async::<TcpStream>::connect(addr).or(async {
    Timer::new(Duration::from_secs(10)).await;
    Err(io::ErrorKind::TimedOut.into())
})
.await?;

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.

Dependencies

~690KB
~13K SLoC