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

#[must_use = "futures do nothing unless polled"]
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:

// import the `timeout` function, usually this is done
// with `use tokio::prelude::*`
use tokio::prelude::FutureExt;
use futures::Stream;
use futures::sync::mpsc;
use std::time::Duration;

let (tx, rx) = mpsc::unbounded();

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

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]

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 futures::Future;
use futures::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))

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

Gets a reference to the underlying value in this timeout.

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]

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 Item = <T as Future>::Item

The type of value that this future will resolved with if it is successful. Read more

type Error = Error<<T as Future>::Error>

The type of error that this future will resolve with if it fails in a normal fashion. Read more

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

type Item = <T as Stream>::Item

The type of item this stream will yield on success.

type Error = Error<<T as Stream>::Error>

The type of error this stream may generate.

Auto Trait Implementations

impl<T> !RefUnwindSafe for Timeout<T>

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

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

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

impl<T> !UnwindSafe for Timeout<T>

Blanket Implementations

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

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

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

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

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

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

type Future = F

The future that this type can be converted into.

type Item = <F as Future>::Item

The item that the future may resolve with.

type Error = <F as Future>::Error

The error that the future may resolve with.

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.