[−][src]Struct tokio_timer::timeout::Timeout
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
.
Implementations
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: Future> Timeout<T>
[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> Debug for Timeout<T>
[src][+]
impl<T> Future for Timeout<T> where
T: Future,
[src][+]
T: Future,
impl<T> Stream for Timeout<T> where
T: Stream,
[src][+]
T: Stream,
Auto Trait Implementations
impl<T> !RefUnwindSafe for Timeout<T>
impl<T> Send for Timeout<T> where
T: Send,
T: Send,
impl<T> Sync for Timeout<T> where
T: Sync,
T: Sync,
impl<T> Unpin for Timeout<T> where
T: Unpin,
T: Unpin,
impl<T> !UnwindSafe for Timeout<T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src][+]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> From<T> for T
[src][+]
impl<T, U> Into<U> for T where
U: From<T>,
[src][+]
U: From<T>,
impl<F> IntoFuture for F where
F: Future,
[src][+]
F: Future,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src][+]
U: Into<T>,
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src][+]
U: TryFrom<T>,