跳到主要内容

Sleep

搜索

结构体 Sleep 

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

sleepsleep_until 返回的 Future。

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

§示例

等待 100ms 并输出“已过去 100ms”。

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

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

Use with select!. Pinning the Sleep with tokio::pin! is necessary when the same Sleep is selected on multiple times.

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));
        },
    }
}

Use in a struct with boxing. By pinning the Sleep with a Box, the HasSleep struct implements Unpin, even though Sleep does not.

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)
    }
}

Use in a struct with pin projection. This method avoids the Box, but the HasSleep struct will not be Unpin as a consequence.

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)
    }
}

实现§

Source§

impl Sleep

Source

pub fn deadline(&self) -> Instant

返回 future 将完成的 instant。

Source

pub fn is_elapsed(&self) -> bool

如果 Sleep 已过期,则返回 true

当所请求的时长已过去时,Sleep 实例处于已过期状态。

Source

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

将该 Sleep 实例重置为一个新的截止时间。

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

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

要调用此方法,通常需要将调用与 Pin::as_mut 结合使用,从而在不必消耗 Sleep 自身的前提下调用该方法。

§Example
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 实现§

Source§

impl Debug for Sleep

Source§

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

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

impl Future for Sleep

Source§

type Output = ()

Future 完成时产生的值的类型。
Source§

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 实现§

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

Source§

type Output = <F as Future>::Output

Future 完成时产生的输出。
Source§

type IntoFuture = F

我们将要把此值转变成哪种 future?
Source§

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

Creates a future from a value. 更多信息
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>

执行转换。