From 8d70baca611869c1997571e8513717396b13328b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Wed, 10 Apr 2019 18:03:12 +0100 Subject: [PATCH] feat(server): impl Sink for Body::Sender Closes #1781 --- src/body/body.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/body/body.rs b/src/body/body.rs index 0f4a1d0220..6e5e7c39fe 100644 --- a/src/body/body.rs +++ b/src/body/body.rs @@ -3,7 +3,7 @@ use std::fmt; use bytes::Bytes; use futures::sync::{mpsc, oneshot}; -use futures::{Async, Future, Poll, Stream}; +use futures::{Async, Future, Poll, Stream, Sink, AsyncSink, StartSend}; use h2; 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 { + 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 for Body { #[inline] fn from(chunk: Chunk) -> Body {