refactor(lib): remove pin related unsafe code (#2220)

This commit is contained in:
Taiki Endo
2020-06-09 08:02:14 +09:00
committed by GitHub
parent d5d09ed753
commit 9998f0fd0b
3 changed files with 24 additions and 26 deletions

View File

@@ -30,7 +30,7 @@ httparse = "1.0"
h2 = "0.2.2"
itoa = "0.4.1"
log = "0.4"
pin-project = "0.4.17"
pin-project = "0.4.20"
time = "0.1"
tower-service = "0.3"
tokio = { version = "0.2.5", features = ["sync"] }

View File

@@ -8,6 +8,8 @@
#[cfg(feature = "stream")]
use futures_core::Stream;
#[cfg(feature = "stream")]
use pin_project::pin_project;
use crate::common::{
task::{self, Poll},
@@ -53,6 +55,9 @@ where
{
struct PollFn<F>(F);
// The closure `F` is never pinned
impl<F> Unpin for PollFn<F> {}
impl<F, IO, E> Accept for PollFn<F>
where
F: FnMut(&mut task::Context<'_>) -> Poll<Option<Result<IO, E>>>,
@@ -63,7 +68,7 @@ where
self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
) -> 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
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>
where
@@ -93,7 +99,7 @@ where
self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
) -> 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)
}
}

View File

@@ -1,8 +1,6 @@
// TODO: Eventually to be replaced with tower_util::Oneshot.
use std::marker::Unpin;
use std::mem;
use pin_project::pin_project;
use tower_service::Service;
use crate::common::{task, Future, Pin, Poll};
@@ -20,24 +18,19 @@ where
// is ready, and then calling `Service::call` with the request, and
// waiting for that `Future`.
#[allow(missing_debug_implementations)]
#[pin_project]
pub struct Oneshot<S: Service<Req>, Req> {
#[pin]
state: State<S, Req>,
}
#[pin_project(project = StateProj, project_replace = StateProjOwn)]
enum State<S: Service<Req>, Req> {
NotReady(S, Req),
Called(S::Future),
Called(#[pin] S::Future),
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>
where
S: Service<Req>,
@@ -45,24 +38,23 @@ where
type Output = Result<S::Response, S::Error>;
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 = unsafe { Pin::get_unchecked_mut(self) };
let mut me = self.project();
loop {
match me.state {
State::NotReady(ref mut svc, _) => {
match me.state.as_mut().project() {
StateProj::NotReady(ref mut svc, _) => {
ready!(svc.poll_ready(cx))?;
// fallthrough out of the match's borrow
}
State::Called(ref mut fut) => {
return unsafe { Pin::new_unchecked(fut) }.poll(cx);
StateProj::Called(fut) => {
return fut.poll(cx);
}
State::Tmp => unreachable!(),
StateProj::Tmp => unreachable!(),
}
match mem::replace(&mut me.state, State::Tmp) {
State::NotReady(mut svc, req) => {
me.state = State::Called(svc.call(req));
match me.state.as_mut().project_replace(State::Tmp) {
StateProjOwn::NotReady(mut svc, req) => {
me.state.set(State::Called(svc.call(req)));
}
_ => unreachable!(),
}