Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to run Hyper on async-std

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
NotificationsYou must be signed in to change notification settings

async-rs/async-std-hyper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is a simple example showing how to runhyper onasync-std.

Instructions

Step 1: Dependencies

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 }

Step 2: Compatibility layer

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)}}}

Step 3: Configure Hyper

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(())})}

License

Licensed under either ofApache License, Version2.0 orMIT license at your option.
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-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp