pub struct Timer { /* private fields */ }
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
sourceimpl Timer
impl Timer
sourcepub fn after(duration: Duration) -> TimerⓘNotable traits for Timerimpl Future for Timer type Output = Instant;
pub fn after(duration: Duration) -> TimerⓘNotable traits for Timerimpl Future for Timer type Output = Instant;
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;
sourcepub fn at(instant: Instant) -> TimerⓘNotable traits for Timerimpl Future for Timer type Output = Instant;
pub fn at(instant: Instant) -> TimerⓘNotable traits for Timerimpl Future for Timer type Output = Instant;
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;
sourcepub fn interval(period: Duration) -> TimerⓘNotable traits for Timerimpl Future for Timer type Output = Instant;
pub fn interval(period: Duration) -> TimerⓘNotable traits for Timerimpl Future for Timer type Output = Instant;
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;
sourcepub fn interval_at(start: Instant, period: Duration) -> TimerⓘNotable traits for Timerimpl Future for Timer type Output = Instant;
pub fn interval_at(start: Instant, period: Duration) -> TimerⓘNotable traits for Timerimpl Future for Timer type Output = Instant;
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;
sourcepub fn set_after(&mut self, duration: Duration)
pub fn set_after(&mut self, duration: Duration)
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));
sourcepub fn set_at(&mut self, instant: Instant)
pub fn set_at(&mut self, instant: Instant)
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);
sourcepub fn set_interval(&mut self, period: Duration)
pub fn set_interval(&mut self, period: Duration)
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);
sourcepub fn set_interval_at(&mut self, start: Instant, period: Duration)
pub fn set_interval_at(&mut self, start: Instant, period: Duration)
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
sourceimpl Stream for Timer
impl Stream for Timer
Auto Trait Implementations
impl RefUnwindSafe for Timer
impl Send for Timer
impl Sync for Timer
impl Unpin for Timer
impl UnwindSafe for Timer
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<F> FutureExt for F where
F: Future + ?Sized,
impl<F> FutureExt for F where
F: Future + ?Sized,
sourcefn poll(&mut self, cx: &mut Context<'_>) -> Poll<Self::Output> where
Self: Unpin,
fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Self::Output> where
Self: Unpin,
A convenience for calling Future::poll()
on !
Unpin
types.
sourcefn or<F>(self, other: F) -> Or<Self, F> where
F: Future<Output = Self::Output>,
fn or<F>(self, other: F) -> Or<Self, F> where
F: Future<Output = Self::Output>,
Returns the result of self
or other
future, preferring self
if both are ready. Read more
sourcefn race<F>(self, other: F) -> Race<Self, F> where
F: Future<Output = Self::Output>,
fn race<F>(self, other: F) -> Race<Self, F> where
F: Future<Output = Self::Output>,
Returns the result of self
or other
future, with no preference if both are ready. Read more
sourcefn catch_unwind(self) -> CatchUnwind<Self> where
Self: UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self> where
Self: UnwindSafe,
Catches panics while polling the future. Read more
sourceimpl<F> IntoFuture for F where
F: Future,
impl<F> IntoFuture for F where
F: Future,
type Output = <F as Future>::Output
type Output = <F as Future>::Output
into_future
)The output that the future will produce on completion.
type IntoFuture = F
type IntoFuture = F
into_future
)Which kind of future are we turning this into?
sourcefn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
into_future
)Creates a future from a value.
sourceimpl<S> StreamExt for S where
S: Stream + ?Sized,
impl<S> StreamExt for S where
S: Stream + ?Sized,
sourcefn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> where
Self: Unpin,
fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> where
Self: Unpin,
A convenience for calling Stream::poll_next()
on !
Unpin
types.
sourcefn next(&mut self) -> NextFuture<'_, Self> where
Self: Unpin,
fn next(&mut self) -> NextFuture<'_, Self> where
Self: Unpin,
Retrieves the next item in the stream. Read more
sourcefn 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
sourcefn count(self) -> CountFuture<Self>
fn count(self) -> CountFuture<Self>
Counts the number of items in the stream. Read more
sourcefn map<T, F>(self, f: F) -> Map<Self, F> where
F: FnMut(Self::Item) -> T,
fn map<T, F>(self, f: F) -> Map<Self, F> where
F: FnMut(Self::Item) -> T,
Maps items of the stream to new values using a closure. Read more
sourcefn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F> where
U: Stream,
F: FnMut(Self::Item) -> U,
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F> where
U: Stream,
F: FnMut(Self::Item) -> U,
Maps items to streams and then concatenates them. Read more
sourcefn flatten(self) -> Flatten<Self> where
Self::Item: Stream,
fn flatten(self) -> Flatten<Self> where
Self::Item: Stream,
Concatenates inner streams. Read more
sourcefn then<F, Fut>(self, f: F) -> Then<Self, F, Fut> where
F: FnMut(Self::Item) -> Fut,
Fut: Future,
fn then<F, Fut>(self, f: F) -> Then<Self, F, Fut> where
F: FnMut(Self::Item) -> Fut,
Fut: Future,
Maps items of the stream to new values using an async closure. Read more
sourcefn filter<P>(self, predicate: P) -> Filter<Self, P> where
P: FnMut(&Self::Item) -> bool,
fn filter<P>(self, predicate: P) -> Filter<Self, P> where
P: FnMut(&Self::Item) -> bool,
Keeps items of the stream for which predicate
returns true
. Read more
sourcefn filter_map<T, F>(self, f: F) -> FilterMap<Self, F> where
F: FnMut(Self::Item) -> Option<T>,
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F> where
F: FnMut(Self::Item) -> Option<T>,
Filters and maps items of the stream using a closure. Read more
sourcefn 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
sourcefn 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
sourcefn chain<U>(self, other: U) -> Chain<Self, U> where
U: Stream<Item = Self::Item>,
fn chain<U>(self, other: U) -> Chain<Self, U> where
U: Stream<Item = Self::Item>,
Appends another stream to the end of this one. Read more
sourcefn cloned<'a, T>(self) -> Cloned<Self> where
Self: Stream<Item = &'a T>,
T: 'a + Clone,
fn cloned<'a, T>(self) -> Cloned<Self> where
Self: Stream<Item = &'a T>,
T: 'a + Clone,
Clones all items. Read more
sourcefn copied<'a, T>(self) -> Copied<Self> where
Self: Stream<Item = &'a T>,
T: 'a + Copy,
fn copied<'a, T>(self) -> Copied<Self> where
Self: Stream<Item = &'a T>,
T: 'a + Copy,
Copies all items. Read more
sourcefn collect<C>(self) -> CollectFuture<Self, C> where
C: Default + Extend<Self::Item>,
fn collect<C>(self) -> CollectFuture<Self, C> where
C: Default + Extend<Self::Item>,
Collects all items in the stream into a collection. Read more
sourcefn 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
sourcefn partition<B, P>(self, predicate: P) -> PartitionFuture<Self, P, B> where
B: Default + Extend<Self::Item>,
P: FnMut(&Self::Item) -> bool,
fn partition<B, P>(self, predicate: P) -> PartitionFuture<Self, P, B> where
B: Default + Extend<Self::Item>,
P: FnMut(&Self::Item) -> bool,
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
sourcefn 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
sourcefn try_fold<T, E, F, B>(
&mut self,
init: B,
f: F
) -> TryFoldFuture<'_, Self, F, B> where
Self: Stream<Item = Result<T, E>> + Unpin,
F: FnMut(B, T) -> Result<B, E>,
fn try_fold<T, E, F, B>(
&mut self,
init: B,
f: F
) -> TryFoldFuture<'_, Self, F, B> where
Self: Stream<Item = Result<T, E>> + Unpin,
F: FnMut(B, T) -> Result<B, E>,
Accumulates a fallible computation over the stream. Read more
sourcefn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F> where
F: FnMut(&mut St, Self::Item) -> Option<B>,
fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F> where
F: FnMut(&mut St, Self::Item) -> Option<B>,
Maps items of the stream to new values using a state value and a closure. Read more
sourcefn cycle(self) -> Cycle<Self> where
Self: Clone,
fn cycle(self) -> Cycle<Self> where
Self: Clone,
Repeats the stream from beginning to end, forever. Read more
sourcefn enumerate(self) -> Enumerate<Self>
fn enumerate(self) -> Enumerate<Self>
Enumerates items, mapping them to (index, item)
. Read more
sourcefn inspect<F>(self, f: F) -> Inspect<Self, F> where
F: FnMut(&Self::Item),
fn inspect<F>(self, f: F) -> Inspect<Self, F> where
F: FnMut(&Self::Item),
Calls a closure on each item and passes it on. Read more
sourcefn nth(&mut self, n: usize) -> NthFuture<'_, Self> where
Self: Unpin,
fn nth(&mut self, n: usize) -> NthFuture<'_, Self> where
Self: Unpin,
Gets the n
th item of the stream. Read more
sourcefn last(self) -> LastFuture<Self>
fn last(self) -> LastFuture<Self>
Returns the last item in the stream. Read more
sourcefn find<P>(&mut self, predicate: P) -> FindFuture<'_, Self, P> where
Self: Unpin,
P: FnMut(&Self::Item) -> bool,
fn find<P>(&mut self, predicate: P) -> FindFuture<'_, Self, P> where
Self: Unpin,
P: FnMut(&Self::Item) -> bool,
Finds the first item of the stream for which predicate
returns true
. Read more
sourcefn find_map<F, B>(&mut self, f: F) -> FindMapFuture<'_, Self, F> where
Self: Unpin,
F: FnMut(Self::Item) -> Option<B>,
fn find_map<F, B>(&mut self, f: F) -> FindMapFuture<'_, Self, F> where
Self: Unpin,
F: FnMut(Self::Item) -> Option<B>,
sourcefn position<P>(&mut self, predicate: P) -> PositionFuture<'_, Self, P> where
Self: Unpin,
P: FnMut(Self::Item) -> bool,
fn position<P>(&mut self, predicate: P) -> PositionFuture<'_, Self, P> where
Self: Unpin,
P: FnMut(Self::Item) -> bool,
Finds the index of the first item of the stream for which predicate
returns true
. Read more
sourcefn all<P>(&mut self, predicate: P) -> AllFuture<'_, Self, P> where
Self: Unpin,
P: FnMut(Self::Item) -> bool,
fn all<P>(&mut self, predicate: P) -> AllFuture<'_, Self, P> where
Self: Unpin,
P: FnMut(Self::Item) -> bool,
Tests if predicate
returns true
for all items in the stream. Read more
sourcefn any<P>(&mut self, predicate: P) -> AnyFuture<'_, Self, P> where
Self: Unpin,
P: FnMut(Self::Item) -> bool,
fn any<P>(&mut self, predicate: P) -> AnyFuture<'_, Self, P> where
Self: Unpin,
P: FnMut(Self::Item) -> bool,
Tests if predicate
returns true
for any item in the stream. Read more
sourcefn for_each<F>(self, f: F) -> ForEachFuture<Self, F> where
F: FnMut(Self::Item),
fn for_each<F>(self, f: F) -> ForEachFuture<Self, F> where
F: FnMut(Self::Item),
Calls a closure on each item of the stream. Read more
sourcefn 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
sourcefn zip<U>(self, other: U) -> Zip<Self, U> where
U: Stream,
fn zip<U>(self, other: U) -> Zip<Self, U> where
U: Stream,
Zips up two streams into a single stream of pairs. Read more
sourcefn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB> where
FromA: Default + Extend<A>,
FromB: Default + Extend<B>,
Self: Stream<Item = (A, B)>,
fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB> where
FromA: Default + Extend<A>,
FromB: Default + Extend<B>,
Self: Stream<Item = (A, B)>,
Collects a stream of pairs into a pair of collections. Read more
sourcefn or<S>(self, other: S) -> Or<Self, S> where
S: Stream<Item = Self::Item>,
fn or<S>(self, other: S) -> Or<Self, S> where
S: Stream<Item = Self::Item>,
Merges with other
stream, preferring items from self
whenever both streams are ready. Read more
sourcefn race<S>(self, other: S) -> Race<Self, S> where
S: Stream<Item = Self::Item>,
fn race<S>(self, other: S) -> Race<Self, S> where
S: Stream<Item = Self::Item>,
Merges with other
stream, with no preference for either stream when both are ready. Read more