feat(server): impl Sink for Body::Sender

Closes #1781
This commit is contained in:
João Oliveira
2019-04-10 18:03:12 +01:00
committed by Sean McArthur
parent 0f33354569
commit 8d70baca61

View File

@@ -3,7 +3,7 @@ use std::fmt;
use bytes::Bytes; use bytes::Bytes;
use futures::sync::{mpsc, oneshot}; use futures::sync::{mpsc, oneshot};
use futures::{Async, Future, Poll, Stream}; use futures::{Async, Future, Poll, Stream, Sink, AsyncSink, StartSend};
use h2; use h2;
use http::HeaderMap; use http::HeaderMap;
@@ -381,6 +381,25 @@ impl Sender {
} }
} }
impl Sink for Sender {
type SinkItem = Chunk;
type SinkError = ::Error;
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
Ok(Async::Ready(()))
}
fn start_send(&mut self, msg: Chunk) -> StartSend<Self::SinkItem, Self::SinkError> {
match self.poll_ready()? {
Async::Ready(_) => {
self.send_data(msg).map_err(|_| ::Error::new_closed())?;
Ok(AsyncSink::Ready)
}
Async::NotReady => Ok(AsyncSink::NotReady(msg)),
}
}
}
impl From<Chunk> for Body { impl From<Chunk> for Body {
#[inline] #[inline]
fn from(chunk: Chunk) -> Body { fn from(chunk: Chunk) -> Body {