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 Instants 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

impl Timer[src]

pub fn after(duration: Duration) -> Timer

Notable traits for Timer

impl Future for Timer type Output = Instant;
[src]

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;

pub fn at(instant: Instant) -> Timer

Notable traits for Timer

impl Future for Timer type Output = Instant;
[src]

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;

pub fn interval(period: Duration) -> Timer

Notable traits for Timer

impl Future for Timer type Output = Instant;
[src]

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;

pub fn interval_at(start: Instant, period: Duration) -> Timer

Notable traits for Timer

impl Future for Timer type Output = Instant;
[src]

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;

pub fn set_after(&mut self, duration: Duration)[src]

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));

pub fn set_at(&mut self, instant: Instant)[src]

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);

pub fn set_interval(&mut self, period: Duration)[src]

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);

pub fn set_interval_at(&mut self, start: Instant, period: Duration)[src]

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

impl Debug for Timer[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl Drop for Timer[src]

fn drop(&mut self)[src]

Executes the destructor for this type. Read more

impl Future for Timer[src]

type Output = Instant

The type of value produced on completion.

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>[src]

Attempt to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more

impl Stream for Timer[src]

type Item = Instant

Values yielded by the stream.

fn poll_next(
    self: Pin<&mut Self>,
    cx: &mut Context<'_>
) -> Poll<Option<Self::Item>>
[src]

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

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the stream. Read more

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<F> FutureExt for F where
    F: Future + ?Sized
[src]

fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Self::Output> where
    Self: Unpin
[src]

A convenience for calling Future::poll() on !Unpin types.

fn or<F>(self, other: F) -> Or<Self, F> where
    F: Future<Output = Self::Output>, 
[src]

Returns the result of self or other future, preferring self if both are ready. Read more

fn race<F>(self, other: F) -> Race<Self, F> where
    F: Future<Output = Self::Output>, 
[src]

Returns the result of self or other future, with no preference if both are ready. Read more

fn catch_unwind(self) -> CatchUnwind<Self> where
    Self: UnwindSafe
[src]

Catches panics while polling the future. Read more

fn boxed<'a>(
    self
) -> Pin<Box<dyn Future<Output = Self::Output> + 'a + Send, Global>> where
    Self: Send + 'a, 
[src]

Boxes the future and changes its type to dyn Future + Send + 'a. Read more

fn boxed_local<'a>(
    self
) -> Pin<Box<dyn Future<Output = Self::Output> + 'a, Global>> where
    Self: 'a, 
[src]

Boxes the future and changes its type to dyn Future + 'a. Read more

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<F> IntoFuture for F where
    F: Future
[src]

type Output = <F as Future>::Output

🔬 This is a nightly-only experimental API. (into_future)

The output that the future will produce on completion.

type Future = F

🔬 This is a nightly-only experimental API. (into_future)

Which kind of future are we turning this into?

pub fn into_future(self) -> <F as IntoFuture>::Future[src]

🔬 This is a nightly-only experimental API. (into_future)

Creates a future from a value.

impl<S> StreamExt for S where
    S: Stream + ?Sized
[src]

fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> where
    Self: Unpin
[src]

A convenience for calling [Stream::poll_next()] on !Unpin types.

fn next(&mut self) -> NextFuture<'_, Self> where
    Self: Unpin
[src]

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
[src]

Retrieves the next item in the stream. Read more

fn count(self) -> CountFuture<Self>[src]

Counts the number of items in the stream. Read more

fn map<T, F>(self, f: F) -> Map<Self, F> where
    F: FnMut(Self::Item) -> T, 
[src]

Maps items of the stream to new values using a closure. Read more

fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F> where
    F: FnMut(Self::Item) -> U,
    U: Stream, 
[src]

Maps items to streams and then concatenates them. Read more

fn flatten(self) -> Flatten<Self> where
    Self::Item: Stream, 
[src]

Concatenates inner streams. Read more

fn then<F, Fut>(self, f: F) -> Then<Self, F, Fut> where
    F: FnMut(Self::Item) -> Fut,
    Fut: Future
[src]

Maps items of the stream to new values using an async closure. Read more

fn filter<P>(self, predicate: P) -> Filter<Self, P> where
    P: FnMut(&Self::Item) -> bool
[src]

Keeps items of the stream for which predicate returns true. Read more

fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F> where
    F: FnMut(Self::Item) -> Option<T>, 
[src]

Filters and maps items of the stream using a closure. Read more

fn take(self, n: usize) -> Take<Self>[src]

Takes only the first n items of the stream. Read more

fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P> where
    P: FnMut(&Self::Item) -> bool
[src]

Takes items while predicate returns true. Read more

fn skip(self, n: usize) -> Skip<Self>[src]

Skips the first n items of the stream. Read more

fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P> where
    P: FnMut(&Self::Item) -> bool
[src]

Skips items while predicate returns true. Read more

fn step_by(self, step: usize) -> StepBy<Self>[src]

Yields every stepth item. Read more

fn chain<U>(self, other: U) -> Chain<Self, U> where
    U: Stream<Item = Self::Item>, 
[src]

Appends another stream to the end of this one. Read more

fn cloned<'a, T>(self) -> Cloned<Self> where
    Self: Stream<Item = &'a T>,
    T: Clone + 'a, 
[src]

Clones all items. Read more

fn copied<'a, T>(self) -> Copied<Self> where
    Self: Stream<Item = &'a T>,
    T: Copy + 'a, 
[src]

Copies all items. Read more

fn collect<C>(self) -> CollectFuture<Self, C> where
    C: Default + Extend<Self::Item>, 
[src]

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>, 
[src]

Collects all items in the fallible stream into a collection. Read more

fn partition<B, P>(self, predicate: P) -> PartitionFuture<Self, P, B> where
    P: FnMut(&Self::Item) -> bool,
    B: Default + Extend<Self::Item>, 
[src]

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, 
[src]

Accumulates a computation over the stream. Read more

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>, 
[src]

Accumulates a fallible computation over the stream. Read more

fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F> where
    F: FnMut(&mut St, Self::Item) -> Option<B>, 
[src]

Maps items of the stream to new values using a state value and a closure. Read more

fn fuse(self) -> Fuse<Self>[src]

Fuses the stream so that it stops yielding items after the first None. Read more

fn cycle(self) -> Cycle<Self> where
    Self: Clone
[src]

Repeats the stream from beginning to end, forever. Read more

fn enumerate(self) -> Enumerate<Self>[src]

Enumerates items, mapping them to (index, item). Read more

fn inspect<F>(self, f: F) -> Inspect<Self, F> where
    F: FnMut(&Self::Item), 
[src]

Calls a closure on each item and passes it on. Read more

fn nth(&mut self, n: usize) -> NthFuture<'_, Self> where
    Self: Unpin
[src]

Gets the nth item of the stream. Read more

fn last(self) -> LastFuture<Self>[src]

Returns the last item in the stream. Read more

fn find<P>(&mut self, predicate: P) -> FindFuture<'_, Self, P> where
    Self: Unpin,
    P: FnMut(&Self::Item) -> bool
[src]

Finds the first item of the stream for which predicate returns true. Read more

fn find_map<F, B>(&mut self, f: F) -> FindMapFuture<'_, Self, F> where
    Self: Unpin,
    F: FnMut(Self::Item) -> Option<B>, 
[src]

Applies a closure to items in the stream and returns the first Some result. Read more

fn position<P>(&mut self, predicate: P) -> PositionFuture<'_, Self, P> where
    Self: Unpin,
    P: FnMut(Self::Item) -> bool
[src]

Finds the index of the first item of the stream for which predicate returns true. Read more

fn all<P>(&mut self, predicate: P) -> AllFuture<'_, Self, P> where
    Self: Unpin,
    P: FnMut(Self::Item) -> bool
[src]

Tests if predicate returns true for all items in the stream. Read more

fn any<P>(&mut self, predicate: P) -> AnyFuture<'_, Self, P> where
    Self: Unpin,
    P: FnMut(Self::Item) -> bool
[src]

Tests if predicate returns true for any item in the stream. Read more

fn for_each<F>(self, f: F) -> ForEachFuture<Self, F> where
    F: FnMut(Self::Item), 
[src]

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>, 
[src]

Calls a fallible closure on each item of the stream, stopping on first error. Read more

fn zip<U>(self, other: U) -> Zip<Self, U> where
    U: Stream, 
[src]

Zips up two streams into a single stream of pairs. Read more

fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB> where
    Self: Stream<Item = (A, B)>,
    FromA: Default + Extend<A>,
    FromB: Default + Extend<B>, 
[src]

Collects a stream of pairs into a pair of collections. Read more

fn or<S>(self, other: S) -> Or<Self, S> where
    S: Stream<Item = Self::Item>, 
[src]

Merges with other stream, preferring items from self whenever both streams are ready. Read more

fn race<S>(self, other: S) -> Race<Self, S> where
    S: Stream<Item = Self::Item>, 
[src]

Merges with other stream, with no preference for either stream when both are ready. Read more

fn boxed<'a>(
    self
) -> Pin<Box<dyn Stream<Item = Self::Item> + 'a + Send, Global>> where
    Self: Send + 'a, 
[src]

Boxes the stream and changes its type to dyn Stream + Send + 'a. Read more

fn boxed_local<'a>(self) -> Pin<Box<dyn Stream<Item = Self::Item> + 'a, Global>> where
    Self: 'a, 
[src]

Boxes the stream and changes its type to dyn Stream + 'a. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.