pub struct Wrapper<'a, R: Runtime, T> { /* private fields */ }
Available on crate feature wrapper only.
Expand description

A wrapper that wraps a future, a stream or an async reader/writer and resets the timeout upon a new event.

WARNING: THIS WILL NOT TIME OUT AUTOMATICALLY. THE TIMEOUT MUST BE AWAITED SOMEWHERE ELSE. See example below.

  • In case of a future, timeout will be reset upon future completion
  • In case of an AsyncRead object, timeout will be reset upon a successful read or seek.
  • In case of an AsyncWrite object, timeout will be reset upon a successful write. It will not be reset upon a shutdown or a flush, please notify me if you think this should be changed!
  • In case of a Stream object, timeout will be reset upon stream advancement.

Since Wrapper::new accepts a shared reference to Timeout, you can make multiple objects use a single timeout. This means the timeout will only expire when all objects stopped having new events.

Example

// Proxy with timeout
use std::{io, time::Duration};
use async_shared_timeout::{runtime, Timeout, Wrapper};

let runtime = runtime::Tokio::new();
let timeout_dur = Duration::from_secs(10);
let timeout = Timeout::new(runtime, timeout_dur);
let mut remote_stream = Wrapper::new(remote_stream, &timeout);
let mut local_stream = Wrapper::new(local_stream, &timeout);
let (copied_a_to_b, copied_b_to_a) = tokio::select! {
    _ = timeout.wait() => {
        return Err(io::Error::new(io::ErrorKind::TimedOut, "stream timeout"))
    }
    x = tokio::io::copy_bidirectional(&mut remote_stream, &mut local_stream) => {
        x?
    }
};

Implementations§

Create a wrapper around an object that will update the given timeout upon successful operations

Arguments
  • inner - the object to be wrapped
  • timeout - a reference to the timeout to be used for operations on inner
  • default_timeout - on a successful operation, timeout will be reset to this value
Available on crate feature std only.

Create a wrapper using a timeout behind an Arc pointer rather than a shared reference. See Wrapper::new for more info.

The timeout reference

A reference to the underlying object

A mutable reference to the underlying object

Trait Implementations§

Converts this type into a mutable reference of the (usually inferred) input type.
Extracts the raw file descriptor. Read more
Converts this type into a shared reference of the (usually inferred) input type.
Tells this buffer that amt bytes have been consumed from the buffer, so they should no longer be returned in calls to poll_read. Read more
Attempts to return the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more
Tells this buffer that amt bytes have been consumed from the buffer, so they should no longer be returned in calls to poll_read. Read more
Attempt to return the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more
Attempts to read from the AsyncRead into buf. Read more
Attempt to read from the AsyncRead into buf. Read more
Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more
Attempts to seek to an offset, in bytes, in a stream. Read more
Waits for a seek operation to complete. Read more
Attempt to seek to an offset, in bytes, in a stream. Read more
Determines if this writer has an efficient poll_write_vectored implementation. Read more
Attempts to flush the object, ensuring that any buffered data reach their destination. Read more
Initiates or attempts to shut down this writer, returning success when the I/O connection has completely shut down. Read more
Attempt to write bytes from buf into the object. Read more
Like poll_write, except that it writes from a slice of buffers. Read more
Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
Attempt to close the object. Read more
Attempt to write bytes from buf into the object. Read more
Attempt to write bytes from bufs into the object using vectored IO operations. Read more
The type of value produced on completion.
Attempt to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more
Values yielded by the stream.
Returns the bounds on the remaining length of the stream. Read more
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

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Returns the contents of the internal buffer, filling it with more data if empty. Read more
Consumes amt buffered bytes. Read more
Reads all bytes and appends them into buf until the delimiter byte or EOF is found. Read more
Reads all bytes and appends them into buf until a newline (the 0xA byte) or EOF is found. Read more
Returns a stream over the lines of this byte stream. Read more
Returns a stream over the contents of this reader split on the specified byte. Read more
Reads some bytes from the byte stream. Read more
Like [read()][AsyncReadExt::read()], except it reads into a slice of buffers. Read more
Reads the entire contents and appends them to a Vec. Read more
Reads the entire contents and appends them to a String. Read more
Reads the exact number of bytes required to fill buf. Read more
Creates an adapter which will read at most limit bytes from it. Read more
Converts this AsyncRead into a Stream of bytes. Read more
Creates an adapter which will chain this stream with another. Read more
Boxes the reader and changes its type to dyn AsyncRead + Send + 'a. Read more
Seeks to a new position in a byte stream. Read more
Writes some bytes into the byte stream. Read more
Like [write()][AsyncWriteExt::write()], except that it writes a slice of buffers. Read more
Writes an entire buffer into the byte stream. Read more
Flushes the stream to ensure that all buffered contents reach their destination. Read more
Closes the writer. Read more
Boxes the writer and changes its type to dyn AsyncWrite + Send + 'a. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

A convenience for calling Future::poll() on !Unpin types.
Returns the result of self or other future, preferring self if both are ready. Read more
Returns the result of self or other future, with no preference if both are ready. Read more
Catches panics while polling the future. Read more
Boxes the future and changes its type to dyn Future + Send + 'a. Read more
Boxes the future and changes its type to dyn Future + 'a. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The output that the future will produce on completion.
Which kind of future are we turning this into?
Creates a future from a value. Read more
A convenience for calling Stream::poll_next() on !Unpin types.
Retrieves the next item in the stream. Read more
Retrieves the next item in the stream. Read more
Counts the number of items in the stream. Read more
Maps items of the stream to new values using a closure. Read more
Maps items to streams and then concatenates them. Read more
Concatenates inner streams. Read more
Maps items of the stream to new values using an async closure. Read more
Keeps items of the stream for which predicate returns true. Read more
Filters and maps items of the stream using a closure. Read more
Takes only the first n items of the stream. Read more
Takes items while predicate returns true. Read more
Skips the first n items of the stream. Read more
Skips items while predicate returns true. Read more
Yields every stepth item. Read more
Appends another stream to the end of this one. Read more
Clones all items. Read more
Copies all items. Read more
Collects all items in the stream into a collection. Read more
Collects all items in the fallible stream into a collection. Read more
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
Accumulates a computation over the stream. Read more
Accumulates a fallible computation over the stream. Read more
Maps items of the stream to new values using a state value and a closure. Read more
Fuses the stream so that it stops yielding items after the first None. Read more
Repeats the stream from beginning to end, forever. Read more
Enumerates items, mapping them to (index, item). Read more
Calls a closure on each item and passes it on. Read more
Gets the nth item of the stream. Read more
Returns the last item in the stream. Read more
Finds the first item of the stream for which predicate returns true. Read more
Applies a closure to items in the stream and returns the first Some result. Read more
Finds the index of the first item of the stream for which predicate returns true. Read more
Tests if predicate returns true for all items in the stream. Read more
Tests if predicate returns true for any item in the stream. Read more
Calls a closure on each item of the stream. Read more
Calls a fallible closure on each item of the stream, stopping on first error. Read more
Zips up two streams into a single stream of pairs. Read more
Collects a stream of pairs into a pair of collections. Read more
Merges with other stream, preferring items from self whenever both streams are ready. Read more
Merges with other stream, with no preference for either stream when both are ready. Read more
Boxes the stream and changes its type to dyn Stream + Send + 'a. Read more
Boxes the stream and changes its type to dyn Stream + 'a. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type of successful values yielded by this future
The type of failures yielded by this future
Poll this TryFuture as if it were a Future. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type of successful values yielded by this future
The type of failures yielded by this future
Poll this TryStream as if it were a Stream. Read more