4
4
//!
5
5
//! * [`Async`], an adapter for standard networking types (and [many other] types) to use in
6
6
//! async programs.
7
- //! * [`Timer`], a future that expires after a duration of time.
7
+ //! * [`Timer`], a future that expires at a point in time.
8
8
//!
9
9
//! For concrete async networking types built on top of this crate, see [`async-net`].
10
10
//!
18
18
//! wake appropriate futures blocked on I/O or timers when they can be resumed.
19
19
//!
20
20
//! To wait for the next I/O event, the "async-io" thread uses [epoll] on Linux/Android/illumos,
21
- //! [kqueue] on macOS/iOS/BSD, [event ports] on illumos/Solaris, and [wepoll] on Windows.
21
+ //! [kqueue] on macOS/iOS/BSD, [event ports] on illumos/Solaris, and [wepoll] on Windows. That
22
+ //! functionality is provided by the [`polling`] crate.
22
23
//!
23
24
//! However, note that you can also process I/O events and wake futures manually if using the
24
25
//! [`parking`] module. The "async-io" thread is therefore just a fallback mechanism processing I/O
30
31
//! [kqueue]: https://en.wikipedia.org/wiki/Kqueue
31
32
//! [event ports]: https://illumos.org/man/port_create
32
33
//! [wepoll]: https://github.com/piscisaureus/wepoll
34
+ //! [`polling`]: https://docs.rs/polling
33
35
//!
34
36
//! # Examples
35
37
//!
46
48
//! let addr = "example.com:80".to_socket_addrs()?.next().unwrap();
47
49
//!
48
50
//! let stream = Async::<TcpStream>::connect(addr).or(async {
49
- //! Timer::new (Duration::from_secs(10)).await;
51
+ //! Timer::after (Duration::from_secs(10)).await;
50
52
//! Err(io::ErrorKind::TimedOut.into())
51
53
//! })
52
54
//! .await?;
@@ -82,7 +84,7 @@ use crate::reactor::{Reactor, Source};
82
84
pub mod parking;
83
85
mod reactor;
84
86
85
- /// A timer that expires after a duration of time.
87
+ /// A future that expires at a point in time.
86
88
///
87
89
/// Timers are futures that output the [`Instant`] at which they fired.
88
90
///
@@ -95,7 +97,7 @@ mod reactor;
95
97
/// use std::time::Duration;
96
98
///
97
99
/// async fn sleep(dur: Duration) {
98
- /// Timer::new (dur).await;
100
+ /// Timer::after (dur).await;
99
101
/// }
100
102
///
101
103
/// # futures_lite::future::block_on(async {
@@ -114,15 +116,48 @@ pub struct Timer {
114
116
}
115
117
116
118
impl Timer {
117
- /// Creates a timer that expires at the specified instant in time.
118
- pub fn at ( when : Instant ) -> Timer {
119
+ /// Creates a timer that expires after the given duration of time.
120
+ ///
121
+ /// # Examples
122
+ ///
123
+ /// ```
124
+ /// use async_io::Timer;
125
+ /// use std::time::Duration;
126
+ ///
127
+ /// # futures_lite::future::block_on(async {
128
+ /// Timer::after(Duration::from_secs(1)).await;
129
+ /// # });
130
+ /// ```
131
+ pub fn after ( duration : Duration ) -> Timer {
132
+ Timer :: at ( Instant :: now ( ) + duration)
133
+ }
134
+
135
+ /// Creates a timer that expires at the given time instant.
136
+ ///
137
+ /// # Examples
138
+ ///
139
+ /// ```
140
+ /// use async_io::Timer;
141
+ /// use std::time::{Duration, Instant};
142
+ ///
143
+ /// # futures_lite::future::block_on(async {
144
+ /// let now = Instant::now();
145
+ /// let when = now + Duration::from_secs(1);
146
+ /// Timer::at(when).await;
147
+ /// # });
148
+ /// ```
149
+ pub fn at ( instant : Instant ) -> Timer {
119
150
Timer {
120
151
id_and_waker : None ,
121
- when,
152
+ when : instant ,
122
153
}
123
154
}
124
155
125
- /// Creates a timer that expires after the given duration of time.
156
+ /// Sets the timer to expire after the new duration of time.
157
+ ///
158
+ /// Note that resetting a timer is different from creating a new timer because
159
+ /// [`set_after()`][`Timer::set_after()`] does not remove the waker associated with the task
160
+ /// that is polling the timer.
126
161
///
127
162
/// # Examples
128
163
///
@@ -131,38 +166,42 @@ impl Timer {
131
166
/// use std::time::Duration;
132
167
///
133
168
/// # futures_lite::future::block_on(async {
134
- /// Timer::new(Duration::from_secs(1)).await;
169
+ /// let mut t = Timer::after(Duration::from_secs(1));
170
+ /// t.set_after(Duration::from_millis(100));
135
171
/// # });
136
172
/// ```
137
- pub fn new ( dur : Duration ) -> Timer {
138
- Timer :: at ( Instant :: now ( ) + dur )
139
- }
173
+ pub fn set_after ( & mut self , duration : Duration ) {
174
+ self . set_at ( Instant :: now ( ) + duration ) ;
175
+ }
140
176
141
- /// Resets the timer to expire after the new duration of time.
177
+ /// Sets the timer to expire at the new time instant .
142
178
///
143
179
/// Note that resetting a timer is different from creating a new timer because
144
- /// [`reset ()`][`Timer::reset ()`] does not remove the waker associated with the task that is
145
- /// polling the timer.
180
+ /// [`set_after ()`][`Timer::set_after ()`] does not remove the waker associated with the task
181
+ /// that is polling the timer.
146
182
///
147
183
/// # Examples
148
184
///
149
185
/// ```
150
186
/// use async_io::Timer;
151
- /// use std::time::Duration;
187
+ /// use std::time::{ Duration, Instant} ;
152
188
///
153
189
/// # futures_lite::future::block_on(async {
154
- /// let mut t = Timer::new(Duration::from_secs(1));
155
- /// t.reset(Duration::from_millis(100));
190
+ /// let mut t = Timer::after(Duration::from_secs(1));
191
+ ///
192
+ /// let now = Instant::now();
193
+ /// let when = now + Duration::from_secs(1);
194
+ /// t.set_at(when);
156
195
/// # });
157
196
/// ```
158
- pub fn reset ( & mut self , dur : Duration ) {
197
+ pub fn set_at ( & mut self , instant : Instant ) {
159
198
if let Some ( ( id, _) ) = self . id_and_waker . as_ref ( ) {
160
199
// Deregister the timer from the reactor.
161
200
Reactor :: get ( ) . remove_timer ( self . when , * id) ;
162
201
}
163
202
164
203
// Update the timeout.
165
- self . when = Instant :: now ( ) + dur ;
204
+ self . when = instant ;
166
205
167
206
if let Some ( ( id, waker) ) = self . id_and_waker . as_mut ( ) {
168
207
// Re-register the timer with the new timeout.
@@ -213,14 +252,14 @@ impl Future for Timer {
213
252
}
214
253
}
215
254
216
- /// Async I/O.
255
+ /// Async adapter for I/O types .
217
256
///
218
- /// This type converts a blocking I/O type into an async type, provided it is supported by
219
- /// [epoll]/[kqueue]/[event ports]/[wepoll].
257
+ /// This type puts an I/O handle into non-blocking mode, registers it in
258
+ /// [epoll]/[kqueue]/[event ports]/[wepoll], and then provides an async interface for it .
220
259
///
221
260
/// **NOTE:** Do not use this type with [`File`][`std::fs::File`], [`Stdin`][`std::io::Stdin`],
222
- /// [`Stdout`][`std::io::Stdout`], or [`Stderr`][`std::io::Stderr`] because they're not
223
- /// supported .
261
+ /// [`Stdout`][`std::io::Stdout`], or [`Stderr`][`std::io::Stderr`] because all of the supported
262
+ /// operating systems have issues with them when put in non-blocking mode .
224
263
///
225
264
/// [epoll]: https://en.wikipedia.org/wiki/Epoll
226
265
/// [kqueue]: https://en.wikipedia.org/wiki/Kqueue
@@ -245,7 +284,7 @@ impl Future for Timer {
245
284
/// # std::io::Result::Ok(()) });
246
285
/// ```
247
286
///
248
- /// You can use predefined async methods or wrap blocking I/O operations in
287
+ /// You can use either predefined async methods or wrap blocking I/O operations in
249
288
/// [`Async::read_with()`], [`Async::read_with_mut()`], [`Async::write_with()`], and
250
289
/// [`Async::write_with_mut()`]:
251
290
///
@@ -387,7 +426,9 @@ impl<T> Async<T> {
387
426
self . io . as_mut ( ) . unwrap ( )
388
427
}
389
428
390
- /// Unwraps the inner non-blocking I/O handle.
429
+ /// Unwraps the inner I/O handle.
430
+ ///
431
+ /// This method will **not** put the I/O handle back into blocking mode.
391
432
///
392
433
/// # Examples
393
434
///
@@ -398,6 +439,9 @@ impl<T> Async<T> {
398
439
/// # futures_lite::future::block_on(async {
399
440
/// let listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 0))?;
400
441
/// let inner = listener.into_inner()?;
442
+ ///
443
+ /// // Put the listener back into blocking mode.
444
+ /// inner.set_nonblocking(false)?;
401
445
/// # std::io::Result::Ok(()) });
402
446
/// ```
403
447
pub fn into_inner ( mut self ) -> io:: Result < T > {
0 commit comments