* Release stream capacity back to the connection to avoid capacity leaks. * Actually notify waiting tasks when capacity becomes available.
		
			
				
	
	
		
			45 lines
		
	
	
		
			1010 B
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			45 lines
		
	
	
		
			1010 B
		
	
	
	
		
			Rust
		
	
	
	
	
	
| use h2::client;
 | |
| 
 | |
| use super::string::{String, TryFrom};
 | |
| use bytes::Bytes;
 | |
| use futures::{Async, Future, Poll};
 | |
| 
 | |
| pub fn byte_str(s: &str) -> String<Bytes> {
 | |
|     String::try_from(Bytes::from(s)).unwrap()
 | |
| }
 | |
| 
 | |
| pub fn wait_for_capacity(stream: client::Stream<Bytes>, target: usize) -> WaitForCapacity {
 | |
|     WaitForCapacity {
 | |
|         stream: Some(stream),
 | |
|         target: target,
 | |
|     }
 | |
| }
 | |
| 
 | |
| pub struct WaitForCapacity {
 | |
|     stream: Option<client::Stream<Bytes>>,
 | |
|     target: usize,
 | |
| }
 | |
| 
 | |
| impl WaitForCapacity {
 | |
|     fn stream(&mut self) -> &mut client::Stream<Bytes> {
 | |
|         self.stream.as_mut().unwrap()
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl Future for WaitForCapacity {
 | |
|     type Item = client::Stream<Bytes>;
 | |
|     type Error = ();
 | |
| 
 | |
|     fn poll(&mut self) -> Poll<Self::Item, ()> {
 | |
|         let _ = try_ready!(self.stream().poll_capacity().map_err(|_| panic!()));
 | |
| 
 | |
|         let act = self.stream().capacity();
 | |
| 
 | |
|         if act >= self.target {
 | |
|             return Ok(self.stream.take().unwrap().into());
 | |
|         }
 | |
| 
 | |
|         Ok(Async::NotReady)
 | |
|     }
 | |
| }
 |