diff options
author | Taiki Endo <te316e89@gmail.com> | 2020-06-12 19:49:39 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-12 19:49:39 +0900 |
commit | 6b6e76080afc92450238df69c4edc12ee5f7518d (patch) | |
tree | 9ce5f612595a3829778df524c24f51a91f155a0e /tokio/src/time | |
parent | 68b4ca9f553bd4c26ea78e1f564e452071cf6474 (diff) |
chore: reduce pin related unsafe code (#2613)
Diffstat (limited to 'tokio/src/time')
-rw-r--r-- | tokio/src/time/timeout.rs | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/tokio/src/time/timeout.rs b/tokio/src/time/timeout.rs index 401856a8..efc3dc5c 100644 --- a/tokio/src/time/timeout.rs +++ b/tokio/src/time/timeout.rs @@ -6,6 +6,7 @@ use crate::time::{delay_until, Delay, Duration, Instant}; +use pin_project_lite::pin_project; use std::fmt; use std::future::Future; use std::pin::Pin; @@ -99,12 +100,16 @@ where } } -/// Future returned by [`timeout`](timeout) and [`timeout_at`](timeout_at). -#[must_use = "futures do nothing unless you `.await` or poll them"] -#[derive(Debug)] -pub struct Timeout<T> { - value: T, - delay: Delay, +pin_project! { + /// Future returned by [`timeout`](timeout) and [`timeout_at`](timeout_at). + #[must_use = "futures do nothing unless you `.await` or poll them"] + #[derive(Debug)] + pub struct Timeout<T> { + #[pin] + value: T, + #[pin] + delay: Delay, + } } /// Error returned by `Timeout`. @@ -146,24 +151,18 @@ where { type Output = Result<T::Output, Elapsed>; - fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> { - // First, try polling the future + fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> { + let me = self.project(); - // Safety: we never move `self.value` - unsafe { - let p = self.as_mut().map_unchecked_mut(|me| &mut me.value); - if let Poll::Ready(v) = p.poll(cx) { - return Poll::Ready(Ok(v)); - } + // First, try polling the future + if let Poll::Ready(v) = me.value.poll(cx) { + return Poll::Ready(Ok(v)); } // Now check the timer - // Safety: X_X! - unsafe { - match self.map_unchecked_mut(|me| &mut me.delay).poll(cx) { - Poll::Ready(()) => Poll::Ready(Err(Elapsed(()))), - Poll::Pending => Poll::Pending, - } + match me.delay.poll(cx) { + Poll::Ready(()) => Poll::Ready(Err(Elapsed(()))), + Poll::Pending => Poll::Pending, } } } |