Skip to content

Commit

Permalink
refactor(lib): remove pin related unsafe code (#2220)
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e authored Jun 8, 2020
1 parent d5d09ed commit 9998f0f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
12 changes: 9 additions & 3 deletions src/server/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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>>>,
Expand All @@ -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)
}
}

Expand All @@ -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
Expand All @@ -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)
}
}

Expand Down
36 changes: 14 additions & 22 deletions src/service/oneshot.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -20,49 +18,43 @@ 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>,
{
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!(),
}
Expand Down

0 comments on commit 9998f0f

Please sign in to comment.