refactor(lib): remove pin related unsafe code (#2220)
This commit is contained in:
@@ -30,7 +30,7 @@ httparse = "1.0"
|
|||||||
h2 = "0.2.2"
|
h2 = "0.2.2"
|
||||||
itoa = "0.4.1"
|
itoa = "0.4.1"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
pin-project = "0.4.17"
|
pin-project = "0.4.20"
|
||||||
time = "0.1"
|
time = "0.1"
|
||||||
tower-service = "0.3"
|
tower-service = "0.3"
|
||||||
tokio = { version = "0.2.5", features = ["sync"] }
|
tokio = { version = "0.2.5", features = ["sync"] }
|
||||||
|
|||||||
@@ -8,6 +8,8 @@
|
|||||||
|
|
||||||
#[cfg(feature = "stream")]
|
#[cfg(feature = "stream")]
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
|
#[cfg(feature = "stream")]
|
||||||
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use crate::common::{
|
use crate::common::{
|
||||||
task::{self, Poll},
|
task::{self, Poll},
|
||||||
@@ -53,6 +55,9 @@ where
|
|||||||
{
|
{
|
||||||
struct PollFn<F>(F);
|
struct PollFn<F>(F);
|
||||||
|
|
||||||
|
// The closure `F` is never pinned
|
||||||
|
impl<F> Unpin for PollFn<F> {}
|
||||||
|
|
||||||
impl<F, IO, E> Accept for PollFn<F>
|
impl<F, IO, E> Accept for PollFn<F>
|
||||||
where
|
where
|
||||||
F: FnMut(&mut task::Context<'_>) -> Poll<Option<Result<IO, E>>>,
|
F: FnMut(&mut task::Context<'_>) -> Poll<Option<Result<IO, E>>>,
|
||||||
@@ -63,7 +68,7 @@ where
|
|||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut task::Context<'_>,
|
cx: &mut task::Context<'_>,
|
||||||
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
|
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
|
||||||
unsafe { (self.get_unchecked_mut().0)(cx) }
|
(self.get_mut().0)(cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,7 +86,8 @@ pub fn from_stream<S, IO, E>(stream: S) -> impl Accept<Conn = IO, Error = E>
|
|||||||
where
|
where
|
||||||
S: Stream<Item = Result<IO, E>>,
|
S: Stream<Item = Result<IO, E>>,
|
||||||
{
|
{
|
||||||
struct FromStream<S>(S);
|
#[pin_project]
|
||||||
|
struct FromStream<S>(#[pin] S);
|
||||||
|
|
||||||
impl<S, IO, E> Accept for FromStream<S>
|
impl<S, IO, E> Accept for FromStream<S>
|
||||||
where
|
where
|
||||||
@@ -93,7 +99,7 @@ where
|
|||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut task::Context<'_>,
|
cx: &mut task::Context<'_>,
|
||||||
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
|
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
|
||||||
unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0).poll_next(cx) }
|
self.project().0.poll_next(cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
// TODO: Eventually to be replaced with tower_util::Oneshot.
|
// TODO: Eventually to be replaced with tower_util::Oneshot.
|
||||||
|
|
||||||
use std::marker::Unpin;
|
use pin_project::pin_project;
|
||||||
use std::mem;
|
|
||||||
|
|
||||||
use tower_service::Service;
|
use tower_service::Service;
|
||||||
|
|
||||||
use crate::common::{task, Future, Pin, Poll};
|
use crate::common::{task, Future, Pin, Poll};
|
||||||
@@ -20,24 +18,19 @@ where
|
|||||||
// is ready, and then calling `Service::call` with the request, and
|
// is ready, and then calling `Service::call` with the request, and
|
||||||
// waiting for that `Future`.
|
// waiting for that `Future`.
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
|
#[pin_project]
|
||||||
pub struct Oneshot<S: Service<Req>, Req> {
|
pub struct Oneshot<S: Service<Req>, Req> {
|
||||||
|
#[pin]
|
||||||
state: State<S, Req>,
|
state: State<S, Req>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pin_project(project = StateProj, project_replace = StateProjOwn)]
|
||||||
enum State<S: Service<Req>, Req> {
|
enum State<S: Service<Req>, Req> {
|
||||||
NotReady(S, Req),
|
NotReady(S, Req),
|
||||||
Called(S::Future),
|
Called(#[pin] S::Future),
|
||||||
Tmp,
|
Tmp,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unpin is projected to S::Future, but never S.
|
|
||||||
impl<S, Req> Unpin for Oneshot<S, Req>
|
|
||||||
where
|
|
||||||
S: Service<Req>,
|
|
||||||
S::Future: Unpin,
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S, Req> Future for Oneshot<S, Req>
|
impl<S, Req> Future for Oneshot<S, Req>
|
||||||
where
|
where
|
||||||
S: Service<Req>,
|
S: Service<Req>,
|
||||||
@@ -45,24 +38,23 @@ where
|
|||||||
type Output = Result<S::Response, S::Error>;
|
type Output = Result<S::Response, S::Error>;
|
||||||
|
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
|
fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
|
||||||
// Safety: The service's future is never moved once we get one.
|
let mut me = self.project();
|
||||||
let mut me = unsafe { Pin::get_unchecked_mut(self) };
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match me.state {
|
match me.state.as_mut().project() {
|
||||||
State::NotReady(ref mut svc, _) => {
|
StateProj::NotReady(ref mut svc, _) => {
|
||||||
ready!(svc.poll_ready(cx))?;
|
ready!(svc.poll_ready(cx))?;
|
||||||
// fallthrough out of the match's borrow
|
// fallthrough out of the match's borrow
|
||||||
}
|
}
|
||||||
State::Called(ref mut fut) => {
|
StateProj::Called(fut) => {
|
||||||
return unsafe { Pin::new_unchecked(fut) }.poll(cx);
|
return fut.poll(cx);
|
||||||
}
|
}
|
||||||
State::Tmp => unreachable!(),
|
StateProj::Tmp => unreachable!(),
|
||||||
}
|
}
|
||||||
|
|
||||||
match mem::replace(&mut me.state, State::Tmp) {
|
match me.state.as_mut().project_replace(State::Tmp) {
|
||||||
State::NotReady(mut svc, req) => {
|
StateProjOwn::NotReady(mut svc, req) => {
|
||||||
me.state = State::Called(svc.call(req));
|
me.state.set(State::Called(svc.call(req)));
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user