跳到主要内容

Instant

搜索

结构体 Instant 

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

单调递增时钟的度量值。 类型不透明,只能与 Duration 配合使用。

Instant 在创建时始终保证不小于任何先前已测量的 instant,常用于度量基准性能或计时某个操作所耗时长等任务。

但请注意,instant 不保证是稳定的。换言之,底层时钟的每一跳长度可能并不相同(例如某些秒可能比其他秒更长)。一个 instant 可能会向前跳跃或发生时间膨胀(变慢或变快),但绝不会倒退。

Instant 是不透明类型,只能彼此之间进行比较。没有方法可以从 instant 中获取“秒数”。它仅允许度量两个 instant 之间的时长(或比较两个 instant)。

Instant 结构体的大小可能因目标操作系统不同而有所不同。

§Note

此类型是对内部 std 类型的封装,用于对齐 Tokio 时钟以供 now() 调用。这在测试场景下非常有用,因为你可以使用 time::pause()time::advance()

实现§

Source§

impl Instant

Source

pub fn now() -> Instant

返回对应“当前”时刻的 instant。

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

let now = Instant::now();
Source

pub fn from_std(std: Instant) -> Instant

std::time::Instant 创建一个 tokio::time::Instant

Source

pub fn into_std(self) -> Instant

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

Source

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

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

Source

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
Source

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

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

§示例
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
}
Source

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);
Source

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

如果 t 可以表示为 Instant(即它处于底层数据结构的表示范围内),则返回 Some(t),其中 t 是时间 self + duration;否则返回 None

Source

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

如果 t 可以表示为 Instant(即它处于底层数据结构的表示范围内),则返回 Some(t),其中 t 是时间 self - duration;否则返回 None

Trait 实现§

Source§

impl Add<Duration> for Instant

Source§

type Output = Instant

应用 + 运算符后得到的类型。
Source§

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

Performs the + operation. 更多信息
Source§

impl AddAssign<Duration> for Instant

Source§

fn add_assign(&mut self, rhs: Duration)

Performs the += operation. 更多信息
Source§

impl Clone for Instant

Source§

fn clone(&self) -> Instant

返回值的副本。 更多信息
1.0.0 · Source§

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

Performs copy-assignment from source. 更多信息
Source§

impl Debug for Instant

Source§

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

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

impl From<Instant> for Instant

Source§

fn from(time: Instant) -> Instant

从输入类型转换为此类型。
Source§

impl From<Instant> for Instant

Source§

fn from(time: Instant) -> Instant

从输入类型转换为此类型。
Source§

impl Hash for Instant

Source§

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

Feeds this value into the given Hasher. 更多信息
1.3.0 · Source§

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

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

impl Ord for Instant

Source§

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

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

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

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

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

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

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

Restrict a value to a certain interval. 更多信息
Source§

impl PartialEq for Instant

Source§

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

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

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

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

impl PartialOrd for Instant

Source§

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

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

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

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

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 · Source§

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

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

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

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

impl Sub<Duration> for Instant

Source§

type Output = Instant

应用 - 运算符后得到的类型。
Source§

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

Performs the - operation. 更多信息
Source§

impl Sub for Instant

Source§

type Output = Duration

应用 - 运算符后得到的类型。
Source§

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

Performs the - operation. 更多信息
Source§

impl SubAssign<Duration> for Instant

Source§

fn sub_assign(&mut self, rhs: Duration)

Performs the -= operation. 更多信息
Source§

impl Copy for Instant

Source§

impl Eq for Instant

Source§

impl StructuralPartialEq for Instant

自动 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> CloneToUninit for T
where T: Clone,

Source§

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. 更多信息
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

获得所有权后的类型。
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. 更多信息
Source§

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

Uses borrowed data to replace owned data, usually by cloning. 更多信息
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>

执行转换。