Update lib to std-future

This commit is contained in:
Gurwinder Singh
2019-08-15 08:25:14 +05:30
committed by Sean McArthur
parent 782f1f712c
commit c8fefd49f1
19 changed files with 1125 additions and 1038 deletions

View File

@@ -4,8 +4,10 @@ use crate::frame::{self, Frame, FrameSize};
use crate::hpack;
use bytes::{Buf, BufMut, BytesMut};
use futures::*;
use tokio_io::{AsyncRead, AsyncWrite, try_nb};
use futures::ready;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio_io::{AsyncRead, AsyncWrite};
use std::io::{self, Cursor};
@@ -55,12 +57,12 @@ const CHAIN_THRESHOLD: usize = 256;
// TODO: Make generic
impl<T, B> FramedWrite<T, B>
where
T: AsyncWrite,
T: AsyncWrite + Unpin,
B: Buf,
{
pub fn new(inner: T) -> FramedWrite<T, B> {
FramedWrite {
inner: inner,
inner,
hpack: hpack::Encoder::default(),
buf: Cursor::new(BytesMut::with_capacity(DEFAULT_BUFFER_CAPACITY)),
next: None,
@@ -73,17 +75,17 @@ where
///
/// Calling this function may result in the current contents of the buffer
/// to be flushed to `T`.
pub fn poll_ready(&mut self) -> Poll<(), io::Error> {
pub fn poll_ready(&mut self, cx: &mut Context) -> Poll<io::Result<()>> {
if !self.has_capacity() {
// Try flushing
self.flush()?;
ready!(self.flush(cx))?;
if !self.has_capacity() {
return Ok(Async::NotReady);
return Poll::Pending;
}
}
Ok(Async::Ready(()))
Poll::Ready(Ok(()))
}
/// Buffer a frame.
@@ -123,33 +125,33 @@ where
// Save off the last frame...
self.last_data_frame = Some(v);
}
},
}
Frame::Headers(v) => {
if let Some(continuation) = v.encode(&mut self.hpack, self.buf.get_mut()) {
self.next = Some(Next::Continuation(continuation));
}
},
}
Frame::PushPromise(v) => {
if let Some(continuation) = v.encode(&mut self.hpack, self.buf.get_mut()) {
self.next = Some(Next::Continuation(continuation));
}
},
}
Frame::Settings(v) => {
v.encode(self.buf.get_mut());
log::trace!("encoded settings; rem={:?}", self.buf.remaining());
},
}
Frame::GoAway(v) => {
v.encode(self.buf.get_mut());
log::trace!("encoded go_away; rem={:?}", self.buf.remaining());
},
}
Frame::Ping(v) => {
v.encode(self.buf.get_mut());
log::trace!("encoded ping; rem={:?}", self.buf.remaining());
},
}
Frame::WindowUpdate(v) => {
v.encode(self.buf.get_mut());
log::trace!("encoded window_update; rem={:?}", self.buf.remaining());
},
}
Frame::Priority(_) => {
/*
@@ -157,18 +159,18 @@ where
log::trace!("encoded priority; rem={:?}", self.buf.remaining());
*/
unimplemented!();
},
}
Frame::Reset(v) => {
v.encode(self.buf.get_mut());
log::trace!("encoded reset; rem={:?}", self.buf.remaining());
},
}
}
Ok(())
}
/// Flush buffered data to the wire
pub fn flush(&mut self) -> Poll<(), io::Error> {
pub fn flush(&mut self, cx: &mut Context) -> Poll<io::Result<()>> {
log::trace!("flush");
loop {
@@ -177,12 +179,12 @@ where
Some(Next::Data(ref mut frame)) => {
log::trace!(" -> queued data frame");
let mut buf = Buf::by_ref(&mut self.buf).chain(frame.payload_mut());
try_ready!(self.inner.write_buf(&mut buf));
},
ready!(Pin::new(&mut self.inner).poll_write_buf(cx, &mut buf))?;
}
_ => {
log::trace!(" -> not a queued data frame");
try_ready!(self.inner.write_buf(&mut self.buf));
},
ready!(Pin::new(&mut self.inner).poll_write_buf(cx, &mut self.buf))?;
}
}
}
@@ -196,11 +198,10 @@ where
self.last_data_frame = Some(frame);
debug_assert!(self.is_empty());
break;
},
}
Some(Next::Continuation(frame)) => {
// Buffer the continuation frame, then try to write again
if let Some(continuation) = frame.encode(&mut self.hpack, self.buf.get_mut()) {
// We previously had a CONTINUATION, and after encoding
// it, we got *another* one? Let's just double check
// that at least some progress is being made...
@@ -213,7 +214,7 @@ where
self.next = Some(Next::Continuation(continuation));
}
},
}
None => {
break;
}
@@ -222,15 +223,15 @@ where
log::trace!("flushing buffer");
// Flush the upstream
try_nb!(self.inner.flush());
ready!(Pin::new(&mut self.inner).poll_flush(cx))?;
Ok(Async::Ready(()))
Poll::Ready(Ok(()))
}
/// Close the codec
pub fn shutdown(&mut self) -> Poll<(), io::Error> {
try_ready!(self.flush());
self.inner.shutdown().map_err(Into::into)
pub fn shutdown(&mut self, cx: &mut Context) -> Poll<io::Result<()>> {
ready!(self.flush(cx))?;
Pin::new(&mut self.inner).poll_shutdown(cx)
}
fn has_capacity(&self) -> bool {
@@ -267,23 +268,18 @@ impl<T, B> FramedWrite<T, B> {
}
}
impl<T: io::Read, B> io::Read for FramedWrite<T, B> {
fn read(&mut self, dst: &mut [u8]) -> io::Result<usize> {
self.inner.read(dst)
}
}
impl<T: AsyncRead, B> AsyncRead for FramedWrite<T, B> {
fn read_buf<B2: BufMut>(&mut self, buf: &mut B2) -> Poll<usize, io::Error>
where
Self: Sized,
{
self.inner.read_buf(buf)
}
impl<T: AsyncRead + Unpin, B: Unpin> AsyncRead for FramedWrite<T, B> {
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
self.inner.prepare_uninitialized_buffer(buf)
}
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.inner).poll_read(cx, buf)
}
}
#[cfg(feature = "unstable")]