- Notifications
You must be signed in to change notification settings - Fork4
How to run Hyper on async-std
License
Apache-2.0, MIT licenses found
Licenses found
Apache-2.0
LICENSE-APACHEMIT
LICENSE-MITNotificationsYou must be signed in to change notification settings
async-rs/async-std-hyper
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This is a simple example showing how to runhyper
onasync-std
.
Addasync-std
,hyper
, andtokio
as dependencies to your crate:
[dependencies]async-std ="1"hyper = {version ="0.13",default-features =false }tokio = {version ="0.2",default-features =false }
Copy thiscompat
module into your crate:
pubmod compat{use std::pin::Pin;use std::task::{Context,Poll};use async_std::io;use async_std::net::{TcpListener,TcpStream};use async_std::prelude::*;use async_std::task;#[derive(Clone)]pubstructHyperExecutor;impl<F> hyper::rt::Executor<F>forHyperExecutorwhereF:Future +Send +'static,F::Output:Send +'static,{fnexecute(&self,fut:F){ task::spawn(fut);}}pubstructHyperListener(pubTcpListener);impl hyper::server::accept::AcceptforHyperListener{typeConn =HyperStream;typeError = io::Error;fnpoll_accept(mutself:Pin<&mutSelf>,cx:&mutContext,) ->Poll<Option<Result<Self::Conn,Self::Error>>>{let stream = task::ready!(Pin::new(&mutself.0.incoming()).poll_next(cx)).unwrap()?;Poll::Ready(Some(Ok(HyperStream(stream))))}}pubstructHyperStream(pubTcpStream);impl tokio::io::AsyncReadforHyperStream{fnpoll_read(mutself:Pin<&mutSelf>,cx:&mutContext,buf:&mut[u8],) ->Poll<io::Result<usize>>{Pin::new(&mutself.0).poll_read(cx, buf)}}impl tokio::io::AsyncWriteforHyperStream{fnpoll_write(mutself:Pin<&mutSelf>,cx:&mutContext,buf:&[u8],) ->Poll<io::Result<usize>>{Pin::new(&mutself.0).poll_write(cx, buf)}fnpoll_flush(mutself:Pin<&mutSelf>,cx:&mutContext) ->Poll<io::Result<()>>{Pin::new(&mutself.0).poll_flush(cx)}fnpoll_shutdown(mutself:Pin<&mutSelf>,cx:&mutContext) ->Poll<io::Result<()>>{Pin::new(&mutself.0).poll_close(cx)}}}
Configure thehyper
builder with:
let server =Server::builder(compat::HyperListener(listener)).executor(compat::Executor);
Full example:
use std::convert::Infallible;use async_std::net::TcpListener;use async_std::task;use hyper::service::{make_service_fn, service_fn};use hyper::{Body,Request,Response,Server};use compat;// This is the module from Step 2.asyncfnhello(_:Request<Body>) ->Result<Response<Body>,Infallible>{Ok(Response::new(Body::from("Hello World!")))}fnmain() ->Result<(),Box<dyn std::error::Error>>{ task::block_on(async{let addr ="127.0.0.1:3000";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)).executor(compat::HyperExecutor).serve(make_svc);println!("Listening on http://{}", addr); server.await?;Ok(())})}
Unless you explicitly state otherwise, any contribution intentionally submittedfor inclusion in this crate by you, as defined in the Apache-2.0 license, shallbe dual licensed as above, without any additional terms or conditions.
About
How to run Hyper on async-std
Resources
License
Apache-2.0, MIT licenses found
Licenses found
Apache-2.0
LICENSE-APACHEMIT
LICENSE-MITUh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.