Update h2-support to std-future

This commit is contained in:
Gurwinder Singh
2019-08-15 08:25:46 +05:30
committed by Sean McArthur
parent c8fefd49f1
commit 529ef4cd40
10 changed files with 511 additions and 989 deletions

View File

@@ -1,24 +1,28 @@
use h2;
use string::{String, TryFrom};
use bytes::Bytes;
use futures::{Async, Future, Poll};
use futures::ready;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use string::{String, TryFrom};
pub fn byte_str(s: &str) -> String<Bytes> {
String::try_from(Bytes::from(s)).unwrap()
}
pub fn yield_once() -> impl Future<Item=(), Error=()> {
pub async fn yield_once() {
let mut yielded = false;
futures::future::poll_fn(move || {
futures::future::poll_fn(move |cx| {
if yielded {
Ok(Async::Ready(()))
Poll::Ready(())
} else {
yielded = true;
futures::task::current().notify();
Ok(Async::NotReady)
cx.waker().clone().wake();
Poll::Pending
}
})
.await;
}
pub fn wait_for_capacity(stream: h2::SendStream<Bytes>, target: usize) -> WaitForCapacity {
@@ -40,18 +44,17 @@ impl WaitForCapacity {
}
impl Future for WaitForCapacity {
type Item = h2::SendStream<Bytes>;
type Error = ();
type Output = h2::SendStream<Bytes>;
fn poll(&mut self) -> Poll<Self::Item, ()> {
let _ = futures::try_ready!(self.stream().poll_capacity().map_err(|_| panic!()));
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let _ = ready!(self.stream().poll_capacity(cx)).unwrap();
let act = self.stream().capacity();
if act >= self.target {
return Ok(self.stream.take().unwrap().into());
return Poll::Ready(self.stream.take().unwrap().into());
}
Ok(Async::NotReady)
Poll::Pending
}
}