2021-12-14 18:40:27 +00:00
|
|
|
// Copyright (C) 2020-2021 François Laignel <fengalin@free.fr>
|
2020-01-02 21:32:52 +00:00
|
|
|
//
|
2021-12-14 18:40:27 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2020-01-02 21:32:52 +00:00
|
|
|
|
|
|
|
//! Wrappers for the underlying runtime specific time related Futures.
|
|
|
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2021-12-14 18:40:27 +00:00
|
|
|
use super::executor::Timer;
|
2021-09-29 16:21:08 +00:00
|
|
|
|
2020-01-02 21:32:52 +00:00
|
|
|
/// Wait until the given `delay` has elapsed.
|
|
|
|
///
|
|
|
|
/// This must be called from within the target runtime environment.
|
2021-09-29 16:21:08 +00:00
|
|
|
///
|
|
|
|
/// When throttling is activated (i.e. when using a non-`0` `wait`
|
|
|
|
/// duration in `Context::acquire`), timer entries are assigned to
|
|
|
|
/// the nearest time frame, meaning that the delay might elapse
|
|
|
|
/// `wait` / 2 ms earlier or later than the expected instant.
|
|
|
|
///
|
|
|
|
/// Use [`delay_for_at_least`] when it's preferable not to return
|
|
|
|
/// before the expected instant.
|
2021-12-14 18:40:27 +00:00
|
|
|
pub fn delay_for(delay: Duration) -> Timer {
|
|
|
|
Timer::after(delay)
|
2020-01-02 21:32:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 16:21:08 +00:00
|
|
|
/// Wait until at least the given `delay` has elapsed.
|
|
|
|
///
|
|
|
|
/// This must be called from within the target runtime environment.
|
|
|
|
///
|
|
|
|
/// See [`delay_for`] for details. This method won't return before
|
|
|
|
/// the expected delay has elapsed.
|
2021-12-14 18:40:27 +00:00
|
|
|
#[track_caller]
|
|
|
|
pub fn delay_for_at_least(delay: Duration) -> Timer {
|
|
|
|
Timer::after_at_least(delay)
|
2021-09-29 16:21:08 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 18:40:27 +00:00
|
|
|
/// Builds a `Stream` that yields at `interval`.
|
2020-01-02 21:32:52 +00:00
|
|
|
///
|
|
|
|
/// This must be called from within the target runtime environment.
|
2021-12-14 18:40:27 +00:00
|
|
|
pub fn interval(interval: Duration) -> Timer {
|
|
|
|
Timer::interval(interval)
|
2021-09-29 16:21:08 +00:00
|
|
|
}
|