跳到主要内容

Instant

搜索

结构体 Instant 

源代码
pub struct Instant { /* 私有字段 */ }
展开描述

单调不减时钟的测量值。不透明,仅与 Duration 配合使用时有用。

在创建时,Instant 始终保证不小于任何先前测量的 Instant,并且通常用于测量基准测试或操作耗时之类的任务。

但请注意,Instant 不保证是稳定的。换句话说,底层时钟的每个 tick 长度可能不同(例如,某些秒可能比其他秒长)。Instant 可能会向前跳跃或经历时间膨胀(变慢或变快),但绝不会向后走。

Instant 是不透明类型,只能相互比较。没有方法可以从 Instant 获取"秒数"。相反,它只允许测量两个 Instant 之间的持续时间(或比较两个 Instant)。

Instant 结构体的大小可能因目标操作系统而异。

§注意

此类型包装内部的 std 变体,用于对齐 Tokio 时钟以用于 now()。这对于测试非常有用,你可以利用 time::pause()time::advance()

实现§

源代码§

impl Instant

源代码

pub fn now() -> Instant

返回对应于"现在"的 Instant。

§示例
use tokio::time::Instant;

let now = Instant::now();
源代码

pub fn from_std(std: Instant) -> Instant

std::time::Instant 创建 tokio::time::Instant

源代码

pub fn into_std(self) -> Instant

将该值转换为 std::time::Instant

源代码

pub fn duration_since(&self, earlier: Instant) -> Duration

返回从另一时刻到此刻所经过的时间量,如果该时刻晚于此刻,则返回零时长。

源代码

pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration>

返回从另一个 Instant 到此 Instant 已经过去的时间量;如果那个 Instant 比此 Instant 晚,则返回 None。

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

let now = Instant::now();
sleep(Duration::new(1, 0)).await;
let new_now = Instant::now();
println!("{:?}", new_now.checked_duration_since(now));
println!("{:?}", now.checked_duration_since(new_now)); // None
源代码

pub fn saturating_duration_since(&self, earlier: Instant) -> Duration

返回从另一时刻到此刻所经过的时间量,如果该时刻晚于此刻,则返回零时长。

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

let now = Instant::now();
sleep(Duration::new(1, 0)).await;
let new_now = Instant::now();
println!("{:?}", new_now.saturating_duration_since(now));
println!("{:?}", now.saturating_duration_since(new_now)); // 0ns
}
源代码

pub fn elapsed(&self) -> Duration

返回自创建此 Instant 以来已经过去的时间量;如果此 Instant 在未来,则返回零持续时间。

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

let instant = Instant::now();
let three_secs = Duration::from_secs(3);
sleep(three_secs).await;
assert!(instant.elapsed() >= three_secs);
源代码

pub fn checked_add(&self, duration: Duration) -> Option<Instant>

如果 t 可以表示为 Instant(这意味着它位于底层数据结构的边界内),则返回 Some(t),其中 t 是时间 self + duration;否则返回 None

源代码

pub fn checked_sub(&self, duration: Duration) -> Option<Instant>

如果 t 可以表示为 Instant(这意味着它位于底层数据结构的边界内),则返回 Some(t),其中 t 是时间 self - duration;否则返回 None

trait 实现§

源代码§

impl Add<Duration> for Instant

源代码§

type Output = Instant

应用 + 运算符后得到的类型。
源代码§

fn add(self, other: Duration) -> Instant

Performs the + operation. 更多信息
源代码§

impl AddAssign<Duration> for Instant

源代码§

fn add_assign(&mut self, rhs: Duration)

Performs the += operation. 更多信息
源代码§

impl Clone for Instant

源代码§

fn clone(&self) -> Instant

返回值的副本。 更多信息
1.0.0 · 源代码§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 更多信息
源代码§

impl Debug for Instant

源代码§

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

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

impl From<Instant> for Instant

源代码§

fn from(time: Instant) -> Instant

从输入类型转换为此类型。
源代码§

impl From<Instant> for Instant

源代码§

fn from(time: Instant) -> Instant

从输入类型转换为此类型。
源代码§

impl Hash for Instant

源代码§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. 更多信息
1.3.0 · 源代码§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. 更多信息
源代码§

impl Ord for Instant

源代码§

fn cmp(&self, other: &Instant) -> Ordering

This method returns an Ordering between self and other. 更多信息
1.21.0 · 源代码§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. 更多信息
1.21.0 · 源代码§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. 更多信息
1.50.0 · 源代码§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. 更多信息
源代码§

impl PartialEq for Instant

源代码§

fn eq(&self, other: &Instant) -> bool

测试 selfother 值是否相等,供 == 运算符使用。
1.0.0 · 源代码§

fn ne(&self, other: &Rhs) -> bool

测试 != 运算符。默认实现几乎总是够用,除非有非常充分的理由,否则不应被覆盖。
源代码§

impl PartialOrd for Instant

源代码§

fn partial_cmp(&self, other: &Instant) -> Option<Ordering>

若存在,此方法返回 selfother 值之间的排序关系。 更多信息
1.0.0 · 源代码§

fn lt(&self, other: &Rhs) -> bool

测试小于(针对 selfother),供 < 运算符使用。 更多信息
1.0.0 · 源代码§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. 更多信息
1.0.0 · 源代码§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. 更多信息
1.0.0 · 源代码§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. 更多信息
源代码§

impl Sub<Duration> for Instant

源代码§

type Output = Instant

应用 - 运算符后得到的类型。
源代码§

fn sub(self, rhs: Duration) -> Instant

Performs the - operation. 更多信息
源代码§

impl Sub for Instant

源代码§

type Output = Duration

应用 - 运算符后得到的类型。
源代码§

fn sub(self, rhs: Instant) -> Duration

Performs the - operation. 更多信息
源代码§

impl SubAssign<Duration> for Instant

源代码§

fn sub_assign(&mut self, rhs: Duration)

Performs the -= operation. 更多信息
源代码§

impl Copy for Instant

源代码§

impl Eq for Instant

源代码§

impl StructuralPartialEq for Instant

自动 trait 实现§

blanket 实现§

源代码§

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

源代码§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
源代码§

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

源代码§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
源代码§

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

源代码§

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

Mutably borrows from an owned value. 更多信息
源代码§

impl<T> CloneToUninit for T
where T: Clone,

源代码§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 更多信息
源代码§

impl<T> From<T> for T

源代码§

fn from(t: T) -> T

原样返回参数。

源代码§

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

源代码§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换是 From<T> for U 实现选择执行的操作。

源代码§

impl<T> ToOwned for T
where T: Clone,

源代码§

type Owned = T

获得所有权后的类型。
源代码§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. 更多信息
源代码§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 更多信息
源代码§

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

源代码§

type Error = Infallible

转换出错时返回的类型。
源代码§

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

执行转换。
源代码§

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

源代码§

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

转换出错时返回的类型。
源代码§

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

执行转换。