Skip to content

Commit

Permalink
chore: Remove OptionPin (#1377)
Browse files Browse the repository at this point in the history
Use Option::as_pin_mut instead.
  • Loading branch information
taiki-e authored Jun 22, 2023
1 parent d72818e commit 0d86e36
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 33 deletions.
31 changes: 12 additions & 19 deletions tonic/src/transport/server/recover_error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
util::{OptionPin, OptionPinProj},
Status,
};
use crate::Status;
use futures_util::ready;
use http::Response;
use pin_project::pin_project;
Expand Down Expand Up @@ -82,20 +79,16 @@ where
#[pin_project]
pub(crate) struct MaybeEmptyBody<B> {
#[pin]
inner: OptionPin<B>,
inner: Option<B>,
}

impl<B> MaybeEmptyBody<B> {
fn full(inner: B) -> Self {
Self {
inner: OptionPin::Some(inner),
}
Self { inner: Some(inner) }
}

fn empty() -> Self {
Self {
inner: OptionPin::None,
}
Self { inner: None }
}
}

Expand All @@ -110,26 +103,26 @@ where
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
match self.project().inner.project() {
OptionPinProj::Some(b) => b.poll_data(cx),
OptionPinProj::None => Poll::Ready(None),
match self.project().inner.as_pin_mut() {
Some(b) => b.poll_data(cx),
None => Poll::Ready(None),
}
}

fn poll_trailers(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Option<http::HeaderMap>, Self::Error>> {
match self.project().inner.project() {
OptionPinProj::Some(b) => b.poll_trailers(cx),
OptionPinProj::None => Poll::Ready(Ok(None)),
match self.project().inner.as_pin_mut() {
Some(b) => b.poll_trailers(cx),
None => Poll::Ready(Ok(None)),
}
}

fn is_end_stream(&self) -> bool {
match &self.inner {
OptionPin::Some(b) => b.is_end_stream(),
OptionPin::None => true,
Some(b) => b.is_end_stream(),
None => true,
}
}
}
9 changes: 4 additions & 5 deletions tonic/src/transport/service/grpc_timeout.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::metadata::GRPC_TIMEOUT_HEADER;
use crate::util::{OptionPin, OptionPinProj};
use http::{HeaderMap, HeaderValue, Request};
use pin_project::pin_project;
use std::{
Expand Down Expand Up @@ -61,8 +60,8 @@ where
inner: self.inner.call(req),
sleep: timeout_duration
.map(tokio::time::sleep)
.map(OptionPin::Some)
.unwrap_or(OptionPin::None),
.map(Some)
.unwrap_or(None),
}
}
}
Expand All @@ -72,7 +71,7 @@ pub(crate) struct ResponseFuture<F> {
#[pin]
inner: F,
#[pin]
sleep: OptionPin<Sleep>,
sleep: Option<Sleep>,
}

impl<F, Res, E> Future for ResponseFuture<F>
Expand All @@ -89,7 +88,7 @@ where
return Poll::Ready(result.map_err(Into::into));
}

if let OptionPinProj::Some(sleep) = this.sleep.project() {
if let Some(sleep) = this.sleep.as_pin_mut() {
futures_util::ready!(sleep.poll(cx));
return Poll::Ready(Err(TimeoutExpired(()).into()));
}
Expand Down
9 changes: 0 additions & 9 deletions tonic/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@
// some combinations of features might cause things here not to be used
#![allow(dead_code)]

use pin_project::pin_project;

/// A pin-project compatible `Option`
#[pin_project(project = OptionPinProj)]
pub(crate) enum OptionPin<T> {
Some(#[pin] T),
None,
}

pub(crate) mod base64 {
use base64::{
alphabet,
Expand Down

0 comments on commit 0d86e36

Please sign in to comment.