-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from Totodore/fix-hyper-util
fix: implement manually `tokio_io` module for `hyper v1`
- Loading branch information
Showing
7 changed files
with
189 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
#![allow(dead_code)] | ||
//! This mod is a copy of https://github.com/hyperium/hyper-util/blob/master/src/rt/tokio_io.rs | ||
//! Because the hyper-util is not yet published to crates.io | ||
//! | ||
//! It is an adapter from hyper v1 IO traits to tokio's IO traits. | ||
use std::{ | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
/// A wrapping implementing hyper IO traits for a type that | ||
/// implements Tokio's IO traits. | ||
#[derive(Debug)] | ||
#[pin_project::pin_project] | ||
pub struct TokioIo<T> { | ||
#[pin] | ||
inner: T, | ||
} | ||
|
||
impl<T> TokioIo<T> { | ||
/// Wrap a type implementing Tokio's IO traits. | ||
pub fn new(inner: T) -> Self { | ||
Self { inner } | ||
} | ||
|
||
/// Borrow the inner type. | ||
pub fn inner(&self) -> &T { | ||
&self.inner | ||
} | ||
|
||
/// Consume this wrapper and get the inner type. | ||
pub fn into_inner(self) -> T { | ||
self.inner | ||
} | ||
} | ||
|
||
impl<T> hyper_v1::rt::Read for TokioIo<T> | ||
where | ||
T: tokio::io::AsyncRead, | ||
{ | ||
fn poll_read( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
mut buf: hyper_v1::rt::ReadBufCursor<'_>, | ||
) -> Poll<Result<(), std::io::Error>> { | ||
let n = unsafe { | ||
let mut tbuf = tokio::io::ReadBuf::uninit(buf.as_mut()); | ||
match tokio::io::AsyncRead::poll_read(self.project().inner, cx, &mut tbuf) { | ||
Poll::Ready(Ok(())) => tbuf.filled().len(), | ||
other => return other, | ||
} | ||
}; | ||
|
||
unsafe { | ||
buf.advance(n); | ||
} | ||
Poll::Ready(Ok(())) | ||
} | ||
} | ||
|
||
impl<T> hyper_v1::rt::Write for TokioIo<T> | ||
where | ||
T: tokio::io::AsyncWrite, | ||
{ | ||
fn poll_write( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &[u8], | ||
) -> Poll<Result<usize, std::io::Error>> { | ||
tokio::io::AsyncWrite::poll_write(self.project().inner, cx, buf) | ||
} | ||
|
||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), std::io::Error>> { | ||
tokio::io::AsyncWrite::poll_flush(self.project().inner, cx) | ||
} | ||
|
||
fn poll_shutdown( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
) -> Poll<Result<(), std::io::Error>> { | ||
tokio::io::AsyncWrite::poll_shutdown(self.project().inner, cx) | ||
} | ||
|
||
fn is_write_vectored(&self) -> bool { | ||
tokio::io::AsyncWrite::is_write_vectored(&self.inner) | ||
} | ||
|
||
fn poll_write_vectored( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
bufs: &[std::io::IoSlice<'_>], | ||
) -> Poll<Result<usize, std::io::Error>> { | ||
tokio::io::AsyncWrite::poll_write_vectored(self.project().inner, cx, bufs) | ||
} | ||
} | ||
|
||
impl<T> tokio::io::AsyncRead for TokioIo<T> | ||
where | ||
T: hyper_v1::rt::Read, | ||
{ | ||
fn poll_read( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
tbuf: &mut tokio::io::ReadBuf<'_>, | ||
) -> Poll<Result<(), std::io::Error>> { | ||
//let init = tbuf.initialized().len(); | ||
let filled = tbuf.filled().len(); | ||
let sub_filled = unsafe { | ||
let mut buf = hyper_v1::rt::ReadBuf::uninit(tbuf.unfilled_mut()); | ||
|
||
match hyper_v1::rt::Read::poll_read(self.project().inner, cx, buf.unfilled()) { | ||
Poll::Ready(Ok(())) => buf.filled().len(), | ||
other => return other, | ||
} | ||
}; | ||
|
||
let n_filled = filled + sub_filled; | ||
// At least sub_filled bytes had to have been initialized. | ||
let n_init = sub_filled; | ||
unsafe { | ||
tbuf.assume_init(n_init); | ||
tbuf.set_filled(n_filled); | ||
} | ||
|
||
Poll::Ready(Ok(())) | ||
} | ||
} | ||
|
||
impl<T> tokio::io::AsyncWrite for TokioIo<T> | ||
where | ||
T: hyper_v1::rt::Write, | ||
{ | ||
fn poll_write( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &[u8], | ||
) -> Poll<Result<usize, std::io::Error>> { | ||
hyper_v1::rt::Write::poll_write(self.project().inner, cx, buf) | ||
} | ||
|
||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), std::io::Error>> { | ||
hyper_v1::rt::Write::poll_flush(self.project().inner, cx) | ||
} | ||
|
||
fn poll_shutdown( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
) -> Poll<Result<(), std::io::Error>> { | ||
hyper_v1::rt::Write::poll_shutdown(self.project().inner, cx) | ||
} | ||
|
||
fn is_write_vectored(&self) -> bool { | ||
hyper_v1::rt::Write::is_write_vectored(&self.inner) | ||
} | ||
|
||
fn poll_write_vectored( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
bufs: &[std::io::IoSlice<'_>], | ||
) -> Poll<Result<usize, std::io::Error>> { | ||
hyper_v1::rt::Write::poll_write_vectored(self.project().inner, cx, bufs) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters