1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-18 15:41:17 +00:00

Remove several uses of Pin::new_unchecked (#1294)

Most of the relevant struct already had a `#[pin_project]` attribute,
but it wasn't being used.

The remaining uses of `Pin::new_unchecked` all involve going from a
`&mut T` to a `Pin<&mut T>`, without directly observing a `Pin<&mut T>`
first. As such, they cannot be replaced by `pin_project`

Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
This commit is contained in:
Aaron Hill 2020-01-27 22:35:51 -05:00 committed by Yuki Okushi
parent d137a8635b
commit 74dcc7366d
2 changed files with 22 additions and 16 deletions

View file

@ -488,10 +488,12 @@ where
} }
} }
#[pin_project::pin_project(PinnedDrop)]
struct OpenWaitingConnection<F, Io> struct OpenWaitingConnection<F, Io>
where where
Io: AsyncRead + AsyncWrite + Unpin + 'static, Io: AsyncRead + AsyncWrite + Unpin + 'static,
{ {
#[pin]
fut: F, fut: F,
key: Key, key: Key,
h2: Option< h2: Option<
@ -525,12 +527,13 @@ where
} }
} }
impl<F, Io> Drop for OpenWaitingConnection<F, Io> #[pin_project::pinned_drop]
impl<F, Io> PinnedDrop for OpenWaitingConnection<F, Io>
where where
Io: AsyncRead + AsyncWrite + Unpin + 'static, Io: AsyncRead + AsyncWrite + Unpin + 'static,
{ {
fn drop(&mut self) { fn drop(self: Pin<&mut Self>) {
if let Some(inner) = self.inner.take() { if let Some(inner) = self.project().inner.take() {
let mut inner = inner.as_ref().borrow_mut(); let mut inner = inner.as_ref().borrow_mut();
inner.release(); inner.release();
inner.check_availibility(); inner.check_availibility();
@ -545,8 +548,8 @@ where
{ {
type Output = (); type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() }; let this = self.as_mut().project();
if let Some(ref mut h2) = this.h2 { if let Some(ref mut h2) = this.h2 {
return match Pin::new(h2).poll(cx) { return match Pin::new(h2).poll(cx) {
@ -571,7 +574,7 @@ where
}; };
} }
match unsafe { Pin::new_unchecked(&mut this.fut) }.poll(cx) { match this.fut.poll(cx) {
Poll::Ready(Err(err)) => { Poll::Ready(Err(err)) => {
let _ = this.inner.take(); let _ = this.inner.take();
if let Some(rx) = this.rx.take() { if let Some(rx) = this.rx.take() {
@ -589,8 +592,8 @@ where
))); )));
Poll::Ready(()) Poll::Ready(())
} else { } else {
this.h2 = Some(handshake(io).boxed_local()); *this.h2 = Some(handshake(io).boxed_local());
unsafe { Pin::new_unchecked(this) }.poll(cx) self.poll(cx)
} }
} }
Poll::Pending => Poll::Pending, Poll::Pending => Poll::Pending,

View file

@ -158,14 +158,16 @@ where
#[pin_project::pin_project] #[pin_project::pin_project]
struct ServiceResponse<F, I, E, B> { struct ServiceResponse<F, I, E, B> {
#[pin]
state: ServiceResponseState<F, B>, state: ServiceResponseState<F, B>,
config: ServiceConfig, config: ServiceConfig,
buffer: Option<Bytes>, buffer: Option<Bytes>,
_t: PhantomData<(I, E)>, _t: PhantomData<(I, E)>,
} }
#[pin_project::pin_project]
enum ServiceResponseState<F, B> { enum ServiceResponseState<F, B> {
ServiceCall(F, Option<SendResponse<Bytes>>), ServiceCall(#[pin] F, Option<SendResponse<Bytes>>),
SendPayload(SendStream<Bytes>, ResponseBody<B>), SendPayload(SendStream<Bytes>, ResponseBody<B>),
} }
@ -247,12 +249,14 @@ where
{ {
type Output = (); type Output = ();
#[pin_project::project]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project(); let mut this = self.as_mut().project();
match this.state { #[project]
ServiceResponseState::ServiceCall(ref mut call, ref mut send) => { match this.state.project() {
match unsafe { Pin::new_unchecked(call) }.poll(cx) { ServiceResponseState::ServiceCall(call, send) => {
match call.poll(cx) {
Poll::Ready(Ok(res)) => { Poll::Ready(Ok(res)) => {
let (res, body) = res.into().replace_body(()); let (res, body) = res.into().replace_body(());
@ -273,8 +277,7 @@ where
if size.is_eof() { if size.is_eof() {
Poll::Ready(()) Poll::Ready(())
} else { } else {
*this.state = this.state.set(ServiceResponseState::SendPayload(stream, body));
ServiceResponseState::SendPayload(stream, body);
self.poll(cx) self.poll(cx)
} }
} }
@ -300,10 +303,10 @@ where
if size.is_eof() { if size.is_eof() {
Poll::Ready(()) Poll::Ready(())
} else { } else {
*this.state = ServiceResponseState::SendPayload( this.state.set(ServiceResponseState::SendPayload(
stream, stream,
body.into_body(), body.into_body(),
); ));
self.poll(cx) self.poll(cx)
} }
} }