跳到主要内容

Interval

搜索

结构体 Interval 

Source
pub struct Interval { /* private fields */ }
展开描述

intervalinterval_at 返回的 Interval。

此类型允许你等待一系列时间点,相邻时间点之间具有固定的时长。与在循环中调用 sleep 不同,它还能统计两次 sleep 调用之间所耗费的时间。

可以通过 IntervalStreamInterval 转换为 Stream

实现§

Source§

impl Interval

Source

pub async fn tick(&mut self) -> Instant

当间隔内的下一时间点到达时完成。

§Cancel safety

此方法是 cancel safe 的。如果 ticktokio::select! 中作为某个分支且其他分支先完成,则不会有 tick 被消耗。

§示例
use tokio::time;

use std::time::Duration;

let mut interval = time::interval(Duration::from_millis(10));

interval.tick().await;
// approximately 0ms have elapsed. The first tick completes immediately.
interval.tick().await;
interval.tick().await;

// approximately 20ms have elapsed.
Source

pub fn poll_tick(&mut self, cx: &mut Context<'_>) -> Poll<Instant>

轮询间隔内下一时间点的到达。

此方法可以返回以下值:

  • Poll::Pending if the next instant has not yet been reached.
  • Poll::Ready(instant) if the next instant has been reached.

当此方法返回 Poll::Pending 时,当前任务将被调度,以在该 instant 到达时接收 wakeup。请注意,多次调用 poll_tick 时,只有最近一次调用所传入的 Context 中的 Waker 会被调度接收 wakeup。

Source

pub fn reset(&mut self)

将间隔重置为从当前时间起一个 period 之后完成。

此方法忽略 MissedTickBehavior 策略。

这等效于调用 reset_at(Instant::now() + period)

§示例
use tokio::time;

use std::time::Duration;

let mut interval = time::interval(Duration::from_millis(100));

interval.tick().await;

time::sleep(Duration::from_millis(50)).await;
interval.reset();

interval.tick().await;
interval.tick().await;

// approximately 250ms have elapsed.
Source

pub fn reset_immediately(&mut self)

立即重置该间隔。

此方法忽略 MissedTickBehavior 策略。

这等效于调用 reset_at(Instant::now())

§示例
use tokio::time;

use std::time::Duration;

let mut interval = time::interval(Duration::from_millis(100));

interval.tick().await;

time::sleep(Duration::from_millis(50)).await;
interval.reset_immediately();

interval.tick().await;
interval.tick().await;

// approximately 150ms have elapsed.
Source

pub fn reset_after(&mut self, after: Duration)

在指定的 std::time::Duration 之后重置该间隔。

此方法忽略 MissedTickBehavior 策略。

这等效于调用 reset_at(Instant::now() + after)

§示例
use tokio::time;

use std::time::Duration;

let mut interval = time::interval(Duration::from_millis(100));
interval.tick().await;

time::sleep(Duration::from_millis(50)).await;

let after = Duration::from_millis(20);
interval.reset_after(after);

interval.tick().await;
interval.tick().await;

// approximately 170ms have elapsed.
Source

pub fn reset_at(&mut self, deadline: Instant)

将该间隔重置为一个 crate::time::Instant 截止时间。

将下一个 tick 设置为在给定的 instant 到达时过期。如果该 instant 处于过去,则会使用 MissedTickBehavior 策略进行追赶。如果该 instant 处于未来,则下一个 tick 将在该 instant 完成,即使这意味着它将睡眠超过此 Interval 的 duration。如果在调用此方法之前该 Interval 已有任何错过的 tick,那些 tick 将被丢弃。

§示例
use tokio::time::{self, Instant};

use std::time::Duration;

let mut interval = time::interval(Duration::from_millis(100));
interval.tick().await;

time::sleep(Duration::from_millis(50)).await;

let deadline = Instant::now() + Duration::from_millis(30);
interval.reset_at(deadline);

interval.tick().await;
interval.tick().await;

// approximately 180ms have elapsed.
Source

pub fn missed_tick_behavior(&self) -> MissedTickBehavior

返回当前使用的 MissedTickBehavior 策略。

Source

pub fn set_missed_tick_behavior(&mut self, behavior: MissedTickBehavior)

设置应使用的 MissedTickBehavior 策略。

Source

pub fn period(&self) -> Duration

返回该间隔的 period。

Trait 实现§

Source§

impl Debug for Interval

Source§

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

使用给定的格式化器格式化此值。 更多信息

自动 Trait 实现§

Blanket 实现§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
Source§

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

Source§

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

Mutably borrows from an owned value. 更多信息
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

原样返回传入的参数。

Source§

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

Source§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换的具体行为取决于 From<T> for U 的实现方式。

Source§

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

Source§

type Error = Infallible

转换出错时返回的类型。
Source§

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

执行转换。
Source§

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

Source§

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

转换出错时返回的类型。
Source§

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

执行转换。