Split response future from client::Stream (#153)

This commit is contained in:
Carl Lerche
2017-10-16 20:17:07 -07:00
committed by GitHub
parent 7c287af0d0
commit faf59f7e24
9 changed files with 121 additions and 125 deletions

View File

@@ -29,6 +29,11 @@ pub struct Connection<T, B: IntoBuf> {
inner: proto::Connection<T, Peer, B>,
}
#[derive(Debug)]
pub struct ResponseFuture<B: IntoBuf> {
inner: proto::StreamRef<B::Buf, Peer>,
}
#[derive(Debug)]
pub struct Stream<B: IntoBuf> {
inner: proto::StreamRef<B::Buf, Peer>,
@@ -113,7 +118,7 @@ where
&mut self,
request: Request<()>,
end_of_stream: bool,
) -> Result<Stream<B>, ::Error> {
) -> Result<(ResponseFuture<B>, Stream<B>), ::Error> {
self.inner
.send_request(request, end_of_stream, self.pending.as_ref())
.map_err(Into::into)
@@ -121,9 +126,16 @@ where
if stream.is_pending_open() {
self.pending = Some(stream.key());
}
Stream {
let response = ResponseFuture {
inner: stream.clone(),
};
let stream = Stream {
inner: stream,
}
};
(response, stream)
})
}
}
@@ -334,19 +346,26 @@ where
}
}
// ===== impl Stream =====
// ===== impl ResponseFuture =====
impl<B: IntoBuf> Stream<B> {
/// Receive the HTTP/2.0 response, if it is ready.
pub fn poll_response(&mut self) -> Poll<Response<Body<B>>, ::Error> {
impl<B: IntoBuf> Future for ResponseFuture<B> {
type Item = Response<Body<B>>;
type Error = ::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let (parts, _) = try_ready!(self.inner.poll_response()).into_parts();
let body = Body {
inner: ReleaseCapacity { inner: self.inner.clone() },
};
Ok(Response::from_parts(parts, body).into())
}
}
// ===== impl Stream =====
impl<B: IntoBuf> Stream<B> {
/// Request capacity to send data
pub fn reserve_capacity(&mut self, capacity: usize) {
// TODO: Check for overflow
@@ -381,15 +400,6 @@ impl<B: IntoBuf> Stream<B> {
}
}
impl<B: IntoBuf> Future for Stream<B> {
type Item = Response<Body<B>>;
type Error = ::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.poll_response()
}
}
// ===== impl Body =====
impl<B: IntoBuf> Body<B> {