[][src]Struct tokio::timer::Timeout

#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Timeout<T> { /* fields omitted */ }

Allows a Future or Stream to execute for a limited amount of time.

If the future or stream completes before the timeout has expired, then Timeout returns the completed value. Otherwise, Timeout returns an Error.

Futures and Streams

The exact behavor depends on if the inner value is a Future or a Stream. In the case of a Future, Timeout will require the future to complete by a fixed deadline. In the case of a Stream, Timeout will allow each item to take the entire timeout before returning an error.

In order to set an upper bound on the processing of the entire stream, then a timeout should be set on the future that processes the stream. For example:

use tokio::prelude::*;
use tokio::sync::mpsc;

use std::thread;
use std::time::Duration;

let (mut tx, rx) = mpsc::unbounded_channel();

thread::spawn(move || {
    tx.try_send(()).unwrap();
    thread::sleep(Duration::from_millis(10));
    tx.try_send(()).unwrap();
});

let process = rx.for_each(|item| {
    // do something with `item`
});

// Wrap the future with a `Timeout` set to expire in 10 milliseconds.
process.timeout(Duration::from_millis(10)).await?;

Cancelation

Cancelling a Timeout is done by dropping the value. No additional cleanup or other work is required.

The original future or stream may be obtained by calling Timeout::into_inner. This consumes the Timeout.

Methods

impl<T> Timeout<T>[src]

Important traits for Timeout<T>
pub fn new(value: T, timeout: Duration) -> Timeout<T>[src]

Create a new Timeout that allows value to execute for a duration of at most timeout.

The exact behavior depends on if value is a Future or a Stream.

See type level documentation for more details.

Examples

Create a new Timeout set to expire in 10 milliseconds.

use tokio::timer::Timeout;
use tokio::sync::oneshot;

use std::time::Duration;

let (tx, rx) = oneshot::channel();

// Wrap the future with a `Timeout` set to expire in 10 milliseconds.
Timeout::new(rx, Duration::from_millis(10)).await??;

Important traits for &'_ mut F
pub fn get_ref(&self) -> &T[src]

Gets a reference to the underlying value in this timeout.

Important traits for &'_ mut F
pub fn get_mut(&mut self) -> &mut T[src]

Gets a mutable reference to the underlying value in this timeout.

pub fn into_inner(self) -> T[src]

Consumes this timeout, returning the underlying value.

impl<T> Timeout<T> where
    T: Future
[src]

Important traits for Timeout<T>
pub fn new_at(future: T, deadline: Instant) -> Timeout<T>[src]

Create a new Timeout that completes when future completes or when deadline is reached.

This function differs from new in that:

  • It only accepts Future arguments.
  • It sets an explicit Instant at which the timeout expires.

Trait Implementations

impl<T> Debug for Timeout<T> where
    T: Debug
[src]

impl<T> Future for Timeout<T> where
    T: Future
[src]

type Output = Result<<T as Future>::Output, Elapsed>

The type of value produced on completion.

impl<T> Stream for Timeout<T> where
    T: Stream
[src]

type Item = Result<<T as Stream>::Item, Elapsed>

Values yielded by the stream.

Auto Trait Implementations

impl<T> Send for Timeout<T> where
    T: Send

impl<T> Unpin for Timeout<T> where
    T: Unpin

impl<T> Sync for Timeout<T> where
    T: Sync

impl<T> !UnwindSafe for Timeout<T>

impl<T> !RefUnwindSafe for Timeout<T>

Blanket Implementations

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

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

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

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

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.

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.

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

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

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

impl<F, T, E> TryFuture for F where
    F: Future<Output = Result<T, E>> + ?Sized
[src]

type Ok = T

The type of successful values yielded by this future

type Error = E

The type of failures yielded by this future

impl<S, T, E> TryStream for S where
    S: Stream<Item = Result<T, E>> + ?Sized
[src]

type Ok = T

The type of successful values yielded by this future

type Error = E

The type of failures yielded by this future

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

impl<Fut> TryFutureExt for Fut where
    Fut: TryFuture + ?Sized
[src]

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

impl<S> TryStreamExt for S where
    S: TryStream + ?Sized
[src]