[][src]Struct smol::Timer

pub struct Timer { /* fields omitted */ }

Fires at the chosen point in time.

Timers are futures that output the Instant at which they fired.

Examples

Sleep for 1 second:

use smol::Timer;
use std::time::Duration;

async fn sleep(dur: Duration) {
    Timer::after(dur).await;
}

sleep(Duration::from_secs(1)).await;

Set a timeout on an I/O operation:

use futures::future::Either;
use futures::io::{self, BufReader};
use futures::prelude::*;
use smol::Timer;
use std::time::Duration;

async fn timeout<T>(
    dur: Duration,
    f: impl Future<Output = io::Result<T>>,
) -> io::Result<T> {
    futures::pin_mut!(f);
    match future::select(f, Timer::after(dur)).await {
        Either::Left((out, _)) => out,
        Either::Right(_) => Err(io::ErrorKind::TimedOut.into()),
    }
}

// Create a buffered stdin reader.
let mut stdin = BufReader::new(smol::reader(std::io::stdin()));

// Read a line within 5 seconds.
let mut line = String::new();
timeout(Duration::from_secs(5), stdin.read_line(&mut line)).await?;

Implementations

impl Timer[src]

pub fn after(dur: Duration) -> Timer[src]

Fires after the specified duration of time.

Examples

use smol::Timer;
use std::time::Duration;

Timer::after(Duration::from_secs(1)).await;

pub fn at(when: Instant) -> Timer[src]

Fires at the specified instant in time.

Examples

use smol::Timer;
use std::time::{Duration, Instant};

let now = Instant::now();
let when = now + Duration::from_secs(1);
Timer::at(when).await;

Trait Implementations

impl Debug for Timer[src]

impl Drop for Timer[src]

impl Future for Timer[src]

type Output = Instant

The type of value produced on completion.

Auto Trait Implementations

impl RefUnwindSafe for Timer

impl Send for Timer

impl Sync for Timer

impl Unpin for Timer

impl UnwindSafe for Timer

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> FutureExt for T where
    T: Future + ?Sized
[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.