Struct Timeout

Source
pub struct Timeout<R: Runtime> { /* private fields */ }
Expand description

A shared timeout.

§Example

use std::time::Duration;

let timeout_secs = Duration::from_secs(10);
// Use the tokio runtime
let runtime = async_shared_timeout::runtime::Tokio::new();
let timeout = async_shared_timeout::Timeout::new(runtime, timeout_secs);
tokio::select! {
    _ = timeout.wait() => {
        println!("timeout expired!");
    }
    _ = async {
        while let Some(cmd) = read_command().await {
            println!("command received: {:?}", cmd);
            timeout.reset();
        }
    } => {
        println!("no more commands!");
    }
}

Implementations§

Source§

impl Timeout<Runtime>

Source

pub fn new_tokio(default_timeout: Duration) -> Self

Create a new timeout that expires after default_timeout, creating a runtime with runtime::Tokio::new

§Panics

Panics if default_timeout is longer than ~584 years

Source§

impl<R: Runtime> Timeout<R>

Source

pub fn new(runtime: R, default_timeout: Duration) -> Self

Create a new timeout that expires after default_timeout

§Panics

Panics if default_timeout is longer than ~584 years

Source

pub fn reset(&self)

Reset the timeout to the default time.

This function is cheap to call.

§Panics

Panics if over ~584 years have elapsed since the timer started.

Source

pub fn default_timeout(&self) -> Duration

The default timeout. Timeout will be reset to this value upon a successful operation.

Source

pub fn set_default_timeout(&self, default_timeout: Duration)

Change the default timeout.

Warning: if this timeout is shorter than previous one, it will only update after the previous timeout has expired!

Additionally, this won’t automatically reset the timeout - it will only affect the next reset.

§Panics

Panics if default_timeout is longer than ~584 years

Source

pub async fn wait(&self)

Wait for the timeout to expire

This is a function that’s expensive to start, so for best performance, only call it once per timer - launch it separately and call reset from the other futures (see the example in top-level documentation).

Trait Implementations§

Source§

impl<R: Debug + Runtime> Debug for Timeout<R>
where R::Instant: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R> !Freeze for Timeout<R>

§

impl<R> RefUnwindSafe for Timeout<R>

§

impl<R> Send for Timeout<R>
where R: Send, <R as Runtime>::Instant: Send,

§

impl<R> Sync for Timeout<R>
where R: Sync, <R as Runtime>::Instant: Sync,

§

impl<R> Unpin for Timeout<R>
where R: Unpin, <R as Runtime>::Instant: Unpin,

§

impl<R> UnwindSafe for Timeout<R>
where R: UnwindSafe, <R as Runtime>::Instant: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.