Struct async_io::Timer [−][src]
pub struct Timer { /* fields omitted */ }
Expand description
A future or stream that emits timed events.
Timers are futures that output a single Instant
when they fire.
Timers are also streams that can output Instant
s periodically.
Examples
Sleep for 1 second:
use async_io::Timer; use std::time::Duration; Timer::after(Duration::from_secs(1)).await;
Timeout after 1 second:
use async_io::Timer; use futures_lite::FutureExt; use std::time::Duration; let addrs = async_net::resolve("google.com:80") .or(async { Timer::after(Duration::from_secs(10)).await; Err(std::io::ErrorKind::TimedOut.into()) }) .await?;
Implementations
Creates a timer that emits an event once after the given duration of time.
Examples
use async_io::Timer; use std::time::Duration; Timer::after(Duration::from_secs(1)).await;
Creates a timer that emits an event once at the given time instant.
Examples
use async_io::Timer; use std::time::{Duration, Instant}; let now = Instant::now(); let when = now + Duration::from_secs(1); Timer::at(when).await;
Creates a timer that emits events periodically.
Examples
use async_io::Timer; use futures_lite::StreamExt; use std::time::{Duration, Instant}; let period = Duration::from_secs(1); Timer::interval(period).next().await;
Creates a timer that emits events periodically, starting at start
.
Examples
use async_io::Timer; use futures_lite::StreamExt; use std::time::{Duration, Instant}; let start = Instant::now(); let period = Duration::from_secs(1); Timer::interval_at(start, period).next().await;
Sets the timer to emit an en event once after the given duration of time.
Note that resetting a timer is different from creating a new timer because
set_after()
does not remove the waker associated with the task
that is polling the timer.
Examples
use async_io::Timer; use std::time::Duration; let mut t = Timer::after(Duration::from_secs(1)); t.set_after(Duration::from_millis(100));
Sets the timer to emit an event once at the given time instant.
Note that resetting a timer is different from creating a new timer because
set_at()
does not remove the waker associated with the task
that is polling the timer.
Examples
use async_io::Timer; use std::time::{Duration, Instant}; let mut t = Timer::after(Duration::from_secs(1)); let now = Instant::now(); let when = now + Duration::from_secs(1); t.set_at(when);
Sets the timer to emit events periodically.
Note that resetting a timer is different from creating a new timer because
set_interval()
does not remove the waker associated with the
task that is polling the timer.
Examples
use async_io::Timer; use futures_lite::StreamExt; use std::time::{Duration, Instant}; let mut t = Timer::after(Duration::from_secs(1)); let period = Duration::from_secs(2); t.set_interval(period);
Sets the timer to emit events periodically, starting at start
.
Note that resetting a timer is different from creating a new timer because
set_interval_at()
does not remove the waker associated with
the task that is polling the timer.
Examples
use async_io::Timer; use futures_lite::StreamExt; use std::time::{Duration, Instant}; let mut t = Timer::after(Duration::from_secs(1)); let start = Instant::now(); let period = Duration::from_secs(2); t.set_interval_at(start, period);
Trait Implementations
Attempt to pull out the next value of this stream, registering the
current task for wakeup if the value is not yet available, and returning
None
if the stream is exhausted. Read more
Auto Trait Implementations
impl RefUnwindSafe for Timer
impl UnwindSafe for Timer
Blanket Implementations
Mutably borrows from an owned value. Read more
A convenience for calling Future::poll()
on !
Unpin
types.
Returns the result of self
or other
future, preferring self
if both are ready. Read more
Returns the result of self
or other
future, with no preference if both are ready. Read more
Catches panics while polling the future. Read more
Boxes the future and changes its type to dyn Future + Send + 'a
. Read more
into_future
)The output that the future will produce on completion.
type Future = F
type Future = F
into_future
)Which kind of future are we turning this into?
into_future
)Creates a future from a value.
A convenience for calling [Stream::poll_next()
] on !
Unpin
types.
Retrieves the next item in the stream. Read more
fn try_next<T, E>(&mut self) -> TryNextFuture<'_, Self> where
Self: Stream<Item = Result<T, E>> + Unpin,
fn try_next<T, E>(&mut self) -> TryNextFuture<'_, Self> where
Self: Stream<Item = Result<T, E>> + Unpin,
Retrieves the next item in the stream. Read more
Counts the number of items in the stream. Read more
Maps items of the stream to new values using a closure. Read more
Maps items to streams and then concatenates them. Read more
Concatenates inner streams. Read more
Maps items of the stream to new values using an async closure. Read more
Keeps items of the stream for which predicate
returns true
. Read more
Filters and maps items of the stream using a closure. Read more
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P> where
P: FnMut(&Self::Item) -> bool,
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P> where
P: FnMut(&Self::Item) -> bool,
Takes items while predicate
returns true
. Read more
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P> where
P: FnMut(&Self::Item) -> bool,
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P> where
P: FnMut(&Self::Item) -> bool,
Skips items while predicate
returns true
. Read more
Appends another stream to the end of this one. Read more
Clones all items. Read more
Copies all items. Read more
Collects all items in the stream into a collection. Read more
fn try_collect<T, E, C>(self) -> TryCollectFuture<Self, C> where
Self: Stream<Item = Result<T, E>>,
C: Default + Extend<T>,
fn try_collect<T, E, C>(self) -> TryCollectFuture<Self, C> where
Self: Stream<Item = Result<T, E>>,
C: Default + Extend<T>,
Collects all items in the fallible stream into a collection. Read more
Partitions items into those for which predicate
is true
and those for which it is
false
, and then collects them into two collections. Read more
fn fold<T, F>(self, init: T, f: F) -> FoldFuture<Self, F, T> where
F: FnMut(T, Self::Item) -> T,
fn fold<T, F>(self, init: T, f: F) -> FoldFuture<Self, F, T> where
F: FnMut(T, Self::Item) -> T,
Accumulates a computation over the stream. Read more
Accumulates a fallible computation over the stream. Read more
Maps items of the stream to new values using a state value and a closure. Read more
Repeats the stream from beginning to end, forever. Read more
Enumerates items, mapping them to (index, item)
. Read more
Calls a closure on each item and passes it on. Read more
Gets the n
th item of the stream. Read more
Returns the last item in the stream. Read more
Finds the first item of the stream for which predicate
returns true
. Read more
Finds the index of the first item of the stream for which predicate
returns true
. Read more
Tests if predicate
returns true
for all items in the stream. Read more
Tests if predicate
returns true
for any item in the stream. Read more
Calls a closure on each item of the stream. Read more
fn try_for_each<F, E>(&mut self, f: F) -> TryForEachFuture<'_, Self, F> where
Self: Unpin,
F: FnMut(Self::Item) -> Result<(), E>,
fn try_for_each<F, E>(&mut self, f: F) -> TryForEachFuture<'_, Self, F> where
Self: Unpin,
F: FnMut(Self::Item) -> Result<(), E>,
Calls a fallible closure on each item of the stream, stopping on first error. Read more
Zips up two streams into a single stream of pairs. Read more
Collects a stream of pairs into a pair of collections. Read more
Merges with other
stream, preferring items from self
whenever both streams are ready. Read more
Merges with other
stream, with no preference for either stream when both are ready. Read more
Boxes the stream and changes its type to dyn Stream + Send + 'a
. Read more