Implement SendRequest::ready. (#215)
This provides a functional future API for waiting for SendRequest readiness.
This commit is contained in:
		| @@ -28,6 +28,13 @@ pub struct SendRequest<B: IntoBuf> { | |||||||
|     pending: Option<proto::StreamKey>, |     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. | /// A future to drive the H2 protocol on a connection. | ||||||
| /// | /// | ||||||
| /// This must be placed in an executor to ensure proper connection management. | /// This must be placed in an executor to ensure proper connection management. | ||||||
| @@ -78,6 +85,12 @@ where | |||||||
|         Ok(().into()) |         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 |     /// Send a request on a new HTTP 2.0 stream | ||||||
|     pub fn send_request( |     pub fn send_request( | ||||||
|         &mut self, |         &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 ===== | ||||||
|  |  | ||||||
| impl Builder { | impl Builder { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user