[−][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 [into_inner
]. This
consumes the Timeout
.
Methods
impl<T> Timeout<T>
[src]
[−]
impl<T> Timeout<T>
pub fn new(value: T, timeout: Duration) -> Timeout<T>
[src]
[−]
pub fn new(value: T, timeout: Duration) -> Timeout<T>
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]
[−]
pub fn get_ref(&self) -> &T
Gets a reference to the underlying value in this timeout.
pub fn get_mut(&mut self) -> &mut T
[src]
[−]
pub fn get_mut(&mut self) -> &mut T
Gets a mutable reference to the underlying value in this timeout.
pub fn into_inner(self) -> T
[src]
[−]
pub fn into_inner(self) -> T
Consumes this timeout, returning the underlying value.
impl<T: Future> Timeout<T>
[src]
[−]
impl<T: Future> Timeout<T>
pub fn new_at(future: T, deadline: Instant) -> Timeout<T>
[src]
[−]
pub fn new_at(future: T, deadline: Instant) -> Timeout<T>
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: Debug> Debug for Timeout<T>
impl<T> Future for Timeout<T> where
T: Future,
[src]
[+]
impl<T> Future for Timeout<T> where
T: Future,
impl<T> Stream for Timeout<T> where
T: Stream,
[src]
[+]
impl<T> Stream for Timeout<T> where
T: Stream,
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
[−]
impl<T, U> Into for T where
U: From<T>,
impl<T> From for T
[src]
[−]
impl<T> From for T
impl<T, U> TryFrom for T where
T: From<U>,
[src]
[−]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
[−]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
try_from
)Performs the conversion.
impl<T> Borrow for T where
T: ?Sized,
[src]
[−]
impl<T> Borrow for T where
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
[−]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
[−]
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
[−]
impl<T, U> TryInto for T where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
[−]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
try_from
)Performs the conversion.
impl<T> Any for T where
T: 'static + ?Sized,
[src]
[−]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
[−]
fn get_type_id(&self) -> TypeId
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Gets the TypeId
of self
. Read more
impl<F> IntoFuture for F where
F: Future,
[src]
[−]
impl<F> IntoFuture for F where
F: Future,