From 42c44045add8ff19237d29f33861300878961a59 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 14 Nov 2020 05:28:27 +0900 Subject: [PATCH] Update pin-project-lite to 0.2.0 --- Cargo.toml | 2 +- src/future/future/flatten.rs | 53 ++++++++++++++++++++++-------------- src/net/addr.rs | 5 +++- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 25912921..eaa2d028 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,7 +71,7 @@ log = { version = "0.4.8", features = ["kv_unstable"], optional = true } memchr = { version = "2.3.3", optional = true } num_cpus = { version = "1.12.0", optional = true } once_cell = { version = "1.3.1", optional = true } -pin-project-lite = { version = "0.1.4", optional = true } +pin-project-lite = { version = "0.2.0", optional = true } pin-utils = { version = "0.1.0-alpha.4", optional = true } slab = { version = "0.4.2", optional = true } diff --git a/src/future/future/flatten.rs b/src/future/future/flatten.rs index a07b140c..0ea77bbf 100644 --- a/src/future/future/flatten.rs +++ b/src/future/future/flatten.rs @@ -1,26 +1,39 @@ +use pin_project_lite::pin_project; use std::future::Future; use std::pin::Pin; use crate::future::IntoFuture; use crate::task::{ready, Context, Poll}; -#[doc(hidden)] -#[allow(missing_debug_implementations)] -pub struct FlattenFuture { - state: State, +pin_project! { + #[doc(hidden)] + #[allow(missing_debug_implementations)] + pub struct FlattenFuture { + #[pin] + state: State, + } } -#[derive(Debug)] -enum State { - First(Fut1), - Second(Fut2), - Empty, +pin_project! { + #[project = StateProj] + #[derive(Debug)] + enum State { + First { + #[pin] + fut1: Fut1, + }, + Second { + #[pin] + fut2: Fut2, + }, + Empty, + } } impl FlattenFuture { - pub(crate) fn new(future: Fut1) -> FlattenFuture { + pub(crate) fn new(fut1: Fut1) -> FlattenFuture { FlattenFuture { - state: State::First(future), + state: State::First { fut1 }, } } } @@ -33,19 +46,19 @@ where type Output = ::Output; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let Self { state } = unsafe { self.get_unchecked_mut() }; + let mut state = self.project().state; loop { - match state { - State::First(fut1) => { - let fut2 = ready!(unsafe { Pin::new_unchecked(fut1) }.poll(cx)).into_future(); - *state = State::Second(fut2); + match state.as_mut().project() { + StateProj::First { fut1 } => { + let fut2 = ready!(fut1.poll(cx)).into_future(); + state.set(State::Second { fut2 }); } - State::Second(fut2) => { - let v = ready!(unsafe { Pin::new_unchecked(fut2) }.poll(cx)); - *state = State::Empty; + StateProj::Second { fut2 } => { + let v = ready!(fut2.poll(cx)); + state.set(State::Empty); return Poll::Ready(v); } - State::Empty => panic!("polled a completed future"), + StateProj::Empty => panic!("polled a completed future"), } } } diff --git a/src/net/addr.rs b/src/net/addr.rs index ea839500..71988fb3 100644 --- a/src/net/addr.rs +++ b/src/net/addr.rs @@ -68,6 +68,9 @@ pub enum ToSocketAddrsFuture { Done, } +// The field of `Self::Resolving` is `Unpin`, and the field of `Self::Ready` is never pinned. +impl Unpin for ToSocketAddrsFuture {} + /// Wrap `std::io::Error` with additional message /// /// Keeps the original error kind and stores the original I/O error as `source`. @@ -84,7 +87,7 @@ impl> Future for ToSocketAddrsFuture { type Output = io::Result; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let this = unsafe { self.get_unchecked_mut() }; + let this = self.get_mut(); let state = mem::replace(this, ToSocketAddrsFuture::Done); match state {