跳到主要内容

Sleep

搜索

结构体 Sleep 

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

sleepsleep_until 返回的 Future。

此类型不实现 Unpin trait,这意味着如果将其与 select! 一起使用或通过调用 poll 来使用,则必须先将其 pin。如果使用 .await,则不适用。

§示例

等待 100 毫秒并打印 “已过 100 毫秒”。

use tokio::time::{sleep, Duration};

sleep(Duration::from_millis(100)).await;
println!("100 ms have elapsed");

select! 一起使用。当对同一个 Sleep 进行多次选择时,需要使用 tokio::pin! 将其 pin。

use tokio::time::{self, Duration, Instant};

let sleep = time::sleep(Duration::from_millis(10));
tokio::pin!(sleep);

loop {
    tokio::select! {
        () = &mut sleep => {
            println!("timer elapsed");
            sleep.as_mut().reset(Instant::now() + Duration::from_millis(50));
        },
    }
}

在带装箱的结构体中使用。通过使用 BoxSleep pin,HasSleep 结构体实现 Unpin,即使 Sleep 没有实现。

use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::time::Sleep;

struct HasSleep {
    sleep: Pin<Box<Sleep>>,
}

impl Future for HasSleep {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        self.sleep.as_mut().poll(cx)
    }
}

在带 pin 投影的结构体中使用。这种方法避免了 Box,但 HasSleep 结构体因此将不实现 Unpin

use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::time::Sleep;
use pin_project_lite::pin_project;

pin_project! {
    struct HasSleep {
        #[pin]
        sleep: Sleep,
    }
}

impl Future for HasSleep {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        self.project().sleep.poll(cx)
    }
}

实现§

源代码§

impl Sleep

源代码

pub fn deadline(&self) -> Instant

返回 future 将完成的时刻。

源代码

pub fn is_elapsed(&self) -> bool

如果 Sleep 已过去,则返回 true

当所请求的持续时间已过去时,Sleep 实例已过去。

源代码

pub fn reset(self: Pin<&mut Self>, deadline: Instant)

Sleep 实例重置为新的截止时间。

调用此函数允许在不创建新的关联状态的情况下,更改 Sleep future 完成的时间点。

此函数可以在 future 完成之前和之后调用。

要调用此方法,通常需要将调用与 Pin::as_mut 结合使用,这样可以调用方法而无需消费 Sleep 本身。

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

let sleep = tokio::time::sleep(Duration::from_millis(10));
tokio::pin!(sleep);

sleep.as_mut().reset(Instant::now() + Duration::from_millis(20));

另请参阅顶级示例。

trait 实现§

源代码§

impl Debug for Sleep

源代码§

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

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

impl Future for Sleep

源代码§

type Output = ()

Future 完成时产生的值的类型。
源代码§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. 更多信息

自动 trait 实现§

§

impl !Freeze for Sleep

§

impl !RefUnwindSafe for Sleep

§

impl Send for Sleep

§

impl Sync for Sleep

§

impl !UnsafeUnpin for Sleep

§

impl !UnwindSafe for Sleep

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> 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<F> IntoFuture for F
where F: Future,

源代码§

type Output = <F as Future>::Output

Future 完成时产生的输出。
源代码§

type IntoFuture = F

我们将要把此值转变成哪种 future?
源代码§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. 更多信息
源代码§

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>

执行转换。