Implement SendRequest::ready. (#215)

This provides a functional future API for waiting for SendRequest
readiness.
This commit is contained in:
Carl Lerche
2018-01-11 10:21:23 -08:00
committed by GitHub
parent aa23a9735d
commit 1db3f34de8

View File

@@ -28,6 +28,13 @@ pub struct SendRequest<B: IntoBuf> {
pending: Option<proto::StreamKey>,
}
/// Returns a `SendRequest` instance once it is ready to send at least one
/// request.
#[derive(Debug)]
pub struct ReadySendRequest<B: IntoBuf> {
inner: Option<SendRequest<B>>,
}
/// A future to drive the H2 protocol on a connection.
///
/// This must be placed in an executor to ensure proper connection management.
@@ -78,6 +85,12 @@ where
Ok(().into())
}
/// Consumes `self`, returning a future that returns `self` back once it is
/// ready to send a request.
pub fn ready(self) -> ReadySendRequest<B> {
ReadySendRequest { inner: Some(self) }
}
/// Send a request on a new HTTP 2.0 stream
pub fn send_request(
&mut self,
@@ -147,6 +160,27 @@ where
}
}
// ===== impl ReadySendRequest =====
impl<B> Future for ReadySendRequest<B>
where B: IntoBuf,
B::Buf: 'static,
{
type Item = SendRequest<B>;
type Error = ::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
match self.inner {
Some(ref mut send_request) => {
let _ = try_ready!(send_request.poll_ready());
}
None => panic!("called `poll` after future completed"),
}
Ok(self.inner.take().unwrap().into())
}
}
// ===== impl Builder =====
impl Builder {