Remove pin-related unsafe code

This commit is contained in:
Taiki Endo
2020-11-14 05:46:10 +09:00
committed by Sean McArthur
parent ff507e12fc
commit 2dec3b725f
2 changed files with 35 additions and 25 deletions

View File

@@ -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>(S);
pin_project! {
struct WrapStream<S> {
#[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<Option<Result<Self::Data, Self::Error>>> {
// 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())))
}

View File

@@ -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<crate::Error>),
}
struct PendingRequest {
method: Method,
url: Url,
headers: HeaderMap,
body: Option<Option<Bytes>>,
pin_project! {
struct PendingRequest {
method: Method,
url: Url,
headers: HeaderMap,
body: Option<Option<Bytes>>,
urls: Vec<Url>,
urls: Vec<Url>,
client: Arc<ClientRef>,
client: Arc<ClientRef>,
in_flight: ResponseFuture,
timeout: Option<Delay>,
#[pin]
in_flight: ResponseFuture,
#[pin]
timeout: Option<Delay>,
}
}
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<Delay>> {
unsafe { Pin::map_unchecked_mut(self, |x| &mut x.timeout) }
self.project().timeout
}
fn urls(self: Pin<&mut Self>) -> &mut Vec<Url> {
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
}
}