AsyncTime

Trait AsyncTime 

Source
pub trait AsyncTime {
    type Interval: TimeInterval;

    // Required methods
    fn sleep(d: Duration) -> impl Future + Send;
    fn tick(d: Duration) -> Self::Interval;

    // Provided method
    fn timeout<F>(
        d: Duration,
        func: F,
    ) -> impl Future<Output = Result<F::Output, ()>> + Send
       where F: Future + Send { ... }
}
Expand description

Trait for async time-related operations.

This trait defines the interface for time-related operations such as sleeping, creating intervals, and applying timeouts to futures.

§Associated Types

  • Interval - The type used for periodic timers

Required Associated Types§

Source

type Interval: TimeInterval

The type used for periodic timers.

Required Methods§

Source

fn sleep(d: Duration) -> impl Future + Send

Sleep for the specified duration.

This method returns a future that completes after the specified duration has elapsed.

§Parameters
  • d - The duration to sleep
§Returns

A future that completes after the specified duration

Source

fn tick(d: Duration) -> Self::Interval

Create a periodic timer that ticks at the specified interval.

This method creates a timer that repeatedly fires at the specified interval, useful for implementing periodic tasks.

§Parameters
  • d - The interval between ticks
§Returns

An interval object that implements TimeInterval

Provided Methods§

Source

fn timeout<F>( d: Duration, func: F, ) -> impl Future<Output = Result<F::Output, ()>> + Send
where F: Future + Send,

Apply a timeout to a future.

This method returns a future that completes either when the provided future completes or when the specified timeout duration elapses, whichever happens first.

§Parameters
  • d - The timeout duration
  • func - The future to apply the timeout to
§Returns

A future that resolves to Ok with the result of the original future if it completes before the timeout, or Err(()) if the timeout elapses first.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<F: Deref<Target = T>, T: AsyncTime> AsyncTime for F