More encoder clean up
This commit is contained in:
@@ -1,10 +1,6 @@
|
|||||||
use crate::{Error, Status};
|
use crate::{Error, Status};
|
||||||
use bytes::{Buf, Bytes, IntoBuf};
|
use bytes::{Buf, Bytes, IntoBuf};
|
||||||
use futures_core::Stream;
|
|
||||||
use futures_util::{ready, TryStreamExt};
|
|
||||||
use http::HeaderMap;
|
|
||||||
use http_body::Body as HttpBody;
|
use http_body::Body as HttpBody;
|
||||||
use pin_project::pin_project;
|
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
@@ -70,14 +66,6 @@ pub struct BoxBody {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BoxBody {
|
impl BoxBody {
|
||||||
pub fn from_stream<S>(s: S) -> Self
|
|
||||||
where
|
|
||||||
S: Stream<Item = Result<crate::body::BytesBuf, Status>> + Send + 'static,
|
|
||||||
{
|
|
||||||
let body = AsyncBody::new(s);
|
|
||||||
Self::map_from(body)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a new `BoxBody` mapping item and error to the default types.
|
/// Create a new `BoxBody` mapping item and error to the default types.
|
||||||
pub fn map_from<B>(inner: B) -> Self
|
pub fn map_from<B>(inner: B) -> Self
|
||||||
where
|
where
|
||||||
@@ -111,62 +99,3 @@ impl HttpBody for BoxBody {
|
|||||||
HttpBody::poll_trailers(self.inner.as_mut(), cx)
|
HttpBody::poll_trailers(self.inner.as_mut(), cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project]
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct AsyncBody<S> {
|
|
||||||
#[pin]
|
|
||||||
inner: S,
|
|
||||||
error: Option<Status>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> AsyncBody<S>
|
|
||||||
where
|
|
||||||
S: Stream<Item = Result<crate::body::BytesBuf, Status>>,
|
|
||||||
{
|
|
||||||
pub fn new(inner: S) -> Self {
|
|
||||||
Self { inner, error: None }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> HttpBody for AsyncBody<S>
|
|
||||||
where
|
|
||||||
S: Stream<Item = Result<crate::body::BytesBuf, Status>>,
|
|
||||||
{
|
|
||||||
type Data = BytesBuf;
|
|
||||||
type Error = Status;
|
|
||||||
|
|
||||||
fn is_end_stream(&self) -> bool {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
fn poll_data(
|
|
||||||
self: Pin<&mut Self>,
|
|
||||||
cx: &mut Context<'_>,
|
|
||||||
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
|
|
||||||
let mut self_proj = self.project();
|
|
||||||
match ready!(self_proj.inner.try_poll_next_unpin(cx)) {
|
|
||||||
Some(Ok(d)) => Some(Ok(d)).into(),
|
|
||||||
Some(Err(status)) => {
|
|
||||||
*self_proj.error = Some(status);
|
|
||||||
None.into()
|
|
||||||
}
|
|
||||||
None => None.into(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn poll_trailers(
|
|
||||||
self: Pin<&mut Self>,
|
|
||||||
_cx: &mut Context<'_>,
|
|
||||||
) -> Poll<Result<Option<HeaderMap>, Status>> {
|
|
||||||
// let self_proj = self.project();
|
|
||||||
// let status = if let Some(status) = self_proj.error.take() {
|
|
||||||
// status
|
|
||||||
// } else {
|
|
||||||
// Status::new(Code::Ok, "")
|
|
||||||
// };
|
|
||||||
|
|
||||||
// Poll::Ready(Ok(Some(status.to_header_map()?)))
|
|
||||||
Poll::Ready(Ok(None))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
body::{Body, BoxBody},
|
body::{Body, BoxBody},
|
||||||
codec::{decode_empty, decode_response, encode, Codec, EncodeBody, Streaming},
|
codec::{decode_empty, decode_response, encode_client, Codec, Streaming},
|
||||||
Code, GrpcService, Request, Response, Status,
|
Code, GrpcService, Request, Response, Status,
|
||||||
};
|
};
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
@@ -114,8 +114,7 @@ impl<T> Grpc<T> {
|
|||||||
let uri = Uri::from_parts(parts).expect("path_and_query only is valid Uri");
|
let uri = Uri::from_parts(parts).expect("path_and_query only is valid Uri");
|
||||||
|
|
||||||
let request = request
|
let request = request
|
||||||
.map(|s| encode(codec.encoder(), Box::pin(s)).into_stream())
|
.map(|s| encode_client(codec.encoder(), Box::pin(s)))
|
||||||
.map(EncodeBody::new_client)
|
|
||||||
.map(BoxBody::map_from);
|
.map(BoxBody::map_from);
|
||||||
|
|
||||||
let mut request = request.into_http(uri);
|
let mut request = request.into_http(uri);
|
||||||
|
|||||||
@@ -9,7 +9,31 @@ use std::pin::Pin;
|
|||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
use tokio_codec::Encoder;
|
use tokio_codec::Encoder;
|
||||||
|
|
||||||
pub fn encode<T, U>(mut encoder: T, source: U) -> impl TryStream<Ok = BytesBuf, Error = Status>
|
pub fn encode_server<T, U>(
|
||||||
|
encoder: T,
|
||||||
|
source: U,
|
||||||
|
) -> EncodeBody<impl Stream<Item = Result<BytesBuf, Status>>>
|
||||||
|
where
|
||||||
|
T: Encoder<Error = Status>,
|
||||||
|
U: Stream<Item = Result<T::Item, Status>>,
|
||||||
|
{
|
||||||
|
let stream = encode(encoder, source).into_stream();
|
||||||
|
EncodeBody::new_server(stream)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn encode_client<T, U>(
|
||||||
|
encoder: T,
|
||||||
|
source: U,
|
||||||
|
) -> EncodeBody<impl Stream<Item = Result<BytesBuf, Status>>>
|
||||||
|
where
|
||||||
|
T: Encoder<Error = Status>,
|
||||||
|
U: Stream<Item = Result<T::Item, Status>>,
|
||||||
|
{
|
||||||
|
let stream = encode(encoder, source).into_stream();
|
||||||
|
EncodeBody::new_client(stream)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encode<T, U>(mut encoder: T, source: U) -> impl TryStream<Ok = BytesBuf, Error = Status>
|
||||||
where
|
where
|
||||||
T: Encoder<Error = Status>,
|
T: Encoder<Error = Status>,
|
||||||
U: Stream<Item = Result<T::Item, Status>>,
|
U: Stream<Item = Result<T::Item, Status>>,
|
||||||
@@ -64,7 +88,7 @@ impl<S> EncodeBody<S>
|
|||||||
where
|
where
|
||||||
S: Stream<Item = Result<crate::body::BytesBuf, Status>>,
|
S: Stream<Item = Result<crate::body::BytesBuf, Status>>,
|
||||||
{
|
{
|
||||||
pub fn new_client(inner: S) -> Self {
|
pub(crate) fn new_client(inner: S) -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner,
|
inner,
|
||||||
error: None,
|
error: None,
|
||||||
@@ -72,7 +96,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_server(inner: S) -> Self {
|
pub(crate) fn new_server(inner: S) -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner,
|
inner,
|
||||||
error: None,
|
error: None,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ mod encode;
|
|||||||
mod prost;
|
mod prost;
|
||||||
|
|
||||||
pub use self::decode::{decode_empty, decode_request, decode_response, Streaming};
|
pub use self::decode::{decode_empty, decode_request, decode_response, Streaming};
|
||||||
pub use self::encode::{encode, EncodeBody};
|
pub use self::encode::{encode_client, encode_server, EncodeBody};
|
||||||
pub use self::prost::ProstCodec;
|
pub use self::prost::ProstCodec;
|
||||||
|
|
||||||
use crate::Status;
|
use crate::Status;
|
||||||
|
|||||||
+15
-22
@@ -1,15 +1,12 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
body::{BoxBody, BytesBuf},
|
body::BoxBody,
|
||||||
codec::{decode_request, encode, Codec, Streaming},
|
codec::{decode_request, encode_server, Codec, Streaming},
|
||||||
server::{ClientStreamingService, ServerStreamingService, StreamingService, UnaryService},
|
server::{ClientStreamingService, ServerStreamingService, StreamingService, UnaryService},
|
||||||
Code, Request, Response, Status,
|
Code, Request, Response, Status,
|
||||||
};
|
};
|
||||||
use futures_core::{Stream, TryStream};
|
use futures_core::TryStream;
|
||||||
use futures_util::{future, stream, TryStreamExt};
|
use futures_util::{future, stream, TryStreamExt};
|
||||||
use http_body::Body;
|
use http_body::Body;
|
||||||
use std::pin::Pin;
|
|
||||||
|
|
||||||
type BoxStream<T> = Pin<Box<dyn Stream<Item = Result<T, Status>> + Send + 'static>>;
|
|
||||||
|
|
||||||
pub struct Grpc<T> {
|
pub struct Grpc<T> {
|
||||||
codec: T,
|
codec: T,
|
||||||
@@ -44,8 +41,7 @@ where
|
|||||||
return self
|
return self
|
||||||
.map_response::<stream::Once<future::Ready<Result<T::Encode, Status>>>>(Err(
|
.map_response::<stream::Once<future::Ready<Result<T::Encode, Status>>>>(Err(
|
||||||
status,
|
status,
|
||||||
))
|
));
|
||||||
.map(BoxBody::from_stream);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -54,7 +50,7 @@ where
|
|||||||
.await
|
.await
|
||||||
.map(|r| r.map(|m| stream::once(future::ok(m))));
|
.map(|r| r.map(|m| stream::once(future::ok(m))));
|
||||||
|
|
||||||
self.map_response(response).map(BoxBody::from_stream)
|
self.map_response(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn server_streaming<S, B>(
|
pub async fn server_streaming<S, B>(
|
||||||
@@ -72,15 +68,13 @@ where
|
|||||||
let request = match self.map_request_unary(req).await {
|
let request = match self.map_request_unary(req).await {
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
Err(status) => {
|
Err(status) => {
|
||||||
return self
|
return self.map_response::<S::ResponseStream>(Err(status));
|
||||||
.map_response::<S::ResponseStream>(Err(status))
|
|
||||||
.map(BoxBody::from_stream);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = service.call(request).await;
|
let response = service.call(request).await;
|
||||||
|
|
||||||
self.map_response(response).map(BoxBody::from_stream)
|
self.map_response(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
//BoxStream<T::Decode>,
|
//BoxStream<T::Decode>,
|
||||||
@@ -102,7 +96,7 @@ where
|
|||||||
.call(request)
|
.call(request)
|
||||||
.await
|
.await
|
||||||
.map(|r| r.map(|m| stream::once(future::ok(m))));
|
.map(|r| r.map(|m| stream::once(future::ok(m))));
|
||||||
self.map_response(response).map(BoxBody::from_stream)
|
self.map_response(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn streaming<S, B>(
|
pub async fn streaming<S, B>(
|
||||||
@@ -119,7 +113,7 @@ where
|
|||||||
{
|
{
|
||||||
let request = self.map_request_streaming(req);
|
let request = self.map_request_streaming(req);
|
||||||
let response = service.call(request).await;
|
let response = service.call(request).await;
|
||||||
self.map_response(response).map(BoxBody::from_stream)
|
self.map_response(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn map_request_unary<B>(
|
async fn map_request_unary<B>(
|
||||||
@@ -161,7 +155,7 @@ where
|
|||||||
fn map_response<B>(
|
fn map_response<B>(
|
||||||
&mut self,
|
&mut self,
|
||||||
response: Result<crate::Response<B>, Status>,
|
response: Result<crate::Response<B>, Status>,
|
||||||
) -> http::Response<BoxStream<BytesBuf>>
|
) -> http::Response<BoxBody>
|
||||||
where
|
where
|
||||||
B: TryStream<Ok = T::Encode, Error = Status> + Send + 'static,
|
B: TryStream<Ok = T::Encode, Error = Status> + Send + 'static,
|
||||||
{
|
{
|
||||||
@@ -175,15 +169,15 @@ where
|
|||||||
http::header::HeaderValue::from_static(T::CONTENT_TYPE),
|
http::header::HeaderValue::from_static(T::CONTENT_TYPE),
|
||||||
);
|
);
|
||||||
|
|
||||||
let body = encode(self.codec.encoder(), body.into_stream()).into_stream();
|
let body = encode_server(self.codec.encoder(), body.into_stream());
|
||||||
|
|
||||||
// FIXME: try to return impl Trait?
|
// FIXME: try to return impl Trait?
|
||||||
let body = Box::pin(body) as BoxStream<BytesBuf>;
|
// let body = Box::pin(body) as BoxStream<BytesBuf>;
|
||||||
http::Response::from_parts(parts, body)
|
http::Response::from_parts(parts, BoxBody::map_from(body))
|
||||||
}
|
}
|
||||||
Err(status) => {
|
Err(status) => {
|
||||||
let status = stream::once(future::err(status));
|
let status = stream::once(future::err(status));
|
||||||
let body = encode(self.codec.encoder(), status).into_stream();
|
let body = encode_server(self.codec.encoder(), status);
|
||||||
let (mut parts, _body) = Response::new(()).into_http().into_parts();
|
let (mut parts, _body) = Response::new(()).into_http().into_parts();
|
||||||
|
|
||||||
parts.headers.insert(
|
parts.headers.insert(
|
||||||
@@ -191,8 +185,7 @@ where
|
|||||||
http::header::HeaderValue::from_static(T::CONTENT_TYPE),
|
http::header::HeaderValue::from_static(T::CONTENT_TYPE),
|
||||||
);
|
);
|
||||||
|
|
||||||
let body = Box::pin(body) as BoxStream<BytesBuf>;
|
http::Response::from_parts(parts, BoxBody::map_from(body))
|
||||||
http::Response::from_parts(parts, body)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user