From 2dec3b725f865f9b5959db0ed3712167aeac1194 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 14 Nov 2020 05:46:10 +0900 Subject: [PATCH] Remove pin-related unsafe code --- src/async_impl/body.rs | 20 +++++++++++--------- src/async_impl/client.rs | 40 ++++++++++++++++++++++++---------------- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/async_impl/body.rs b/src/async_impl/body.rs index 3e58666..762d70a 100644 --- a/src/async_impl/body.rs +++ b/src/async_impl/body.rs @@ -6,6 +6,7 @@ use std::task::{Context, Poll}; use bytes::Bytes; use futures_core::Stream; use http_body::Body as HttpBody; +use pin_project_lite::pin_project; use tokio::time::Delay; /// An asynchronous request body. @@ -30,7 +31,12 @@ enum Inner { }, } -struct WrapStream(S); +pin_project! { + struct WrapStream { + #[pin] + inner: S, + } +} struct WrapHyper(hyper::Body); @@ -86,9 +92,9 @@ impl Body { { use futures_util::TryStreamExt; - let body = Box::pin(WrapStream( - stream.map_ok(Bytes::from).map_err(Into::into), - )); + let body = Box::pin(WrapStream { + inner: stream.map_ok(Bytes::from).map_err(Into::into), + }); Body { inner: Inner::Streaming { body, @@ -279,11 +285,7 @@ where self: Pin<&mut Self>, cx: &mut Context, ) -> Poll>> { - // safe pin projection - let item = - futures_core::ready!( - unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) }.poll_next(cx)? - ); + let item = futures_core::ready!(self.project().inner.poll_next(cx)?); Poll::Ready(item.map(|val| Ok(val.into()))) } diff --git a/src/async_impl/client.rs b/src/async_impl/client.rs index ff103f7..15af416 100644 --- a/src/async_impl/client.rs +++ b/src/async_impl/client.rs @@ -25,6 +25,7 @@ use std::future::Future; use std::pin::Pin; use std::task::{Context, Poll}; use tokio::time::Delay; +use pin_project_lite::pin_project; use log::debug; @@ -1265,8 +1266,11 @@ impl ClientRef { } } -pub(super) struct Pending { - inner: PendingInner, +pin_project! { + pub(super) struct Pending { + #[pin] + inner: PendingInner, + } } enum PendingInner { @@ -1274,35 +1278,39 @@ enum PendingInner { Error(Option), } -struct PendingRequest { - method: Method, - url: Url, - headers: HeaderMap, - body: Option>, +pin_project! { + struct PendingRequest { + method: Method, + url: Url, + headers: HeaderMap, + body: Option>, - urls: Vec, + urls: Vec, - client: Arc, + client: Arc, - in_flight: ResponseFuture, - timeout: Option, + #[pin] + in_flight: ResponseFuture, + #[pin] + timeout: Option, + } } impl PendingRequest { fn in_flight(self: Pin<&mut Self>) -> Pin<&mut ResponseFuture> { - unsafe { Pin::map_unchecked_mut(self, |x| &mut x.in_flight) } + self.project().in_flight } fn timeout(self: Pin<&mut Self>) -> Pin<&mut Option> { - unsafe { Pin::map_unchecked_mut(self, |x| &mut x.timeout) } + self.project().timeout } fn urls(self: Pin<&mut Self>) -> &mut Vec { - unsafe { &mut Pin::get_unchecked_mut(self).urls } + self.project().urls } fn headers(self: Pin<&mut Self>) -> &mut HeaderMap { - unsafe { &mut Pin::get_unchecked_mut(self).headers } + self.project().headers } } @@ -1314,7 +1322,7 @@ impl Pending { } fn inner(self: Pin<&mut Self>) -> Pin<&mut PendingInner> { - unsafe { Pin::map_unchecked_mut(self, |x| &mut x.inner) } + self.project().inner } }