Skip to content

Fix for newer versions of async-std #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
781 changes: 439 additions & 342 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -5,6 +5,6 @@ authors = ["Stjepan Glavina <stjepang@gmail.com>"]
edition = "2018"

[dependencies]
async-std = "1"
hyper = { version = "0.13", default-features = false }
tokio = { version = "0.2", default-features = false }
async-std = "1.12.0"
hyper = { version = "0.14.20", default-features = false, features = ["server", "http1", "http2"] }
tokio = { version = "1.20.1", default-features = false }
29 changes: 21 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let listener = TcpListener::bind(addr).await?;

let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(hello)) });
let server = Server::builder(compat::HyperListener(listener))
let server = Server::builder(compat::HyperListener::new(&listener))
.executor(compat::HyperExecutor)
.serve(make_svc);

@@ -30,7 +30,7 @@ pub mod compat {
use std::task::{Context, Poll};

use async_std::io;
use async_std::net::{TcpListener, TcpStream};
use async_std::net::{self, TcpListener, TcpStream};
use async_std::prelude::*;
use async_std::task;

@@ -47,17 +47,27 @@ pub mod compat {
}
}

pub struct HyperListener(pub TcpListener);
pub struct HyperListener<'listener> {
incoming: net::Incoming<'listener>,
}

impl hyper::server::accept::Accept for HyperListener {
impl<'listener> HyperListener<'listener> {
pub fn new(listener: &'listener TcpListener) -> Self {
Self {
incoming: listener.incoming(),
}
}
}

impl hyper::server::accept::Accept for HyperListener<'_> {
type Conn = HyperStream;
type Error = io::Error;

fn poll_accept(
mut self: Pin<&mut Self>,
cx: &mut Context,
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
let stream = task::ready!(Pin::new(&mut self.0.incoming()).poll_next(cx)).unwrap()?;
let stream = task::ready!(Pin::new(&mut self.incoming).poll_next(cx)).unwrap()?;
Poll::Ready(Some(Ok(HyperStream(stream))))
}
}
@@ -68,9 +78,12 @@ pub mod compat {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.0).poll_read(cx, buf)
buf: &mut tokio::io::ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let bytes =
task::ready!(Pin::new(&mut self.0).poll_read(cx, buf.initialize_unfilled())?);
buf.advance(bytes);
Poll::Ready(Ok(()))
}
}