{"id":13672566,"url":"https://github.com/async-rs/async-std-hyper","last_synced_at":"2025-12-29T05:59:04.411Z","repository":{"id":66118728,"uuid":"239285129","full_name":"async-rs/async-std-hyper","owner":"async-rs","description":"How to run Hyper on async-std","archived":false,"fork":false,"pushed_at":"2022-09-04T16:15:24.000Z","size":13,"stargazers_count":40,"open_issues_count":3,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-19T11:45:34.346Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/async-rs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-02-09T10:28:28.000Z","updated_at":"2024-12-13T16:22:55.000Z","dependencies_parsed_at":"2023-04-09T05:48:07.526Z","dependency_job_id":null,"html_url":"https://github.com/async-rs/async-std-hyper","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/async-rs%2Fasync-std-hyper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/async-rs%2Fasync-std-hyper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/async-rs%2Fasync-std-hyper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/async-rs%2Fasync-std-hyper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/async-rs","download_url":"https://codeload.github.com/async-rs/async-std-hyper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251219601,"owners_count":21554444,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T09:01:39.510Z","updated_at":"2025-12-29T05:59:04.365Z","avatar_url":"https://github.com/async-rs.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# How to run Hyper on async-std\n\nThis is a simple example showing how to run [`hyper`] on [`async-std`].\n\n[`hyper`]: https://docs.rs/hyper\n[`async-std`]: https://docs.rs/async-std\n\n## Instructions\n\n#### Step 1: Dependencies\n\nAdd `async-std`, `hyper`, and `tokio` as dependencies to your crate:\n\n```toml\n[dependencies]\nasync-std = \"1\"\nhyper = { version = \"0.13\", default-features = false }\ntokio = { version = \"0.2\", default-features = false }\n```\n\n#### Step 2: Compatibility layer\n\nCopy this `compat` module into your crate:\n\n```rust\npub mod compat {\n    use std::pin::Pin;\n    use std::task::{Context, Poll};\n\n    use async_std::io;\n    use async_std::net::{TcpListener, TcpStream};\n    use async_std::prelude::*;\n    use async_std::task;\n\n    #[derive(Clone)]\n    pub struct HyperExecutor;\n\n    impl\u003cF\u003e hyper::rt::Executor\u003cF\u003e for HyperExecutor\n    where\n        F: Future + Send + 'static,\n        F::Output: Send + 'static,\n    {\n        fn execute(\u0026self, fut: F) {\n            task::spawn(fut);\n        }\n    }\n\n    pub struct HyperListener(pub TcpListener);\n\n    impl hyper::server::accept::Accept for HyperListener {\n        type Conn = HyperStream;\n        type Error = io::Error;\n\n        fn poll_accept(\n            mut self: Pin\u003c\u0026mut Self\u003e,\n            cx: \u0026mut Context,\n        ) -\u003e Poll\u003cOption\u003cResult\u003cSelf::Conn, Self::Error\u003e\u003e\u003e {\n            let stream = task::ready!(Pin::new(\u0026mut self.0.incoming()).poll_next(cx)).unwrap()?;\n            Poll::Ready(Some(Ok(HyperStream(stream))))\n        }\n    }\n\n    pub struct HyperStream(pub TcpStream);\n\n    impl tokio::io::AsyncRead for HyperStream {\n        fn poll_read(\n            mut self: Pin\u003c\u0026mut Self\u003e,\n            cx: \u0026mut Context,\n            buf: \u0026mut [u8],\n        ) -\u003e Poll\u003cio::Result\u003cusize\u003e\u003e {\n            Pin::new(\u0026mut self.0).poll_read(cx, buf)\n        }\n    }\n\n    impl tokio::io::AsyncWrite for HyperStream {\n        fn poll_write(\n            mut self: Pin\u003c\u0026mut Self\u003e,\n            cx: \u0026mut Context,\n            buf: \u0026[u8],\n        ) -\u003e Poll\u003cio::Result\u003cusize\u003e\u003e {\n            Pin::new(\u0026mut self.0).poll_write(cx, buf)\n        }\n\n        fn poll_flush(mut self: Pin\u003c\u0026mut Self\u003e, cx: \u0026mut Context) -\u003e Poll\u003cio::Result\u003c()\u003e\u003e {\n            Pin::new(\u0026mut self.0).poll_flush(cx)\n        }\n\n        fn poll_shutdown(mut self: Pin\u003c\u0026mut Self\u003e, cx: \u0026mut Context) -\u003e Poll\u003cio::Result\u003c()\u003e\u003e {\n            Pin::new(\u0026mut self.0).poll_close(cx)\n        }\n    }\n}\n```\n\n#### Step 3: Configure Hyper\n\nConfigure the `hyper` builder with:\n\n```rust\nlet server = Server::builder(compat::HyperListener(listener))\n    .executor(compat::Executor);\n```\n\nFull example:\n\n```rust\nuse std::convert::Infallible;\n\nuse async_std::net::TcpListener;\nuse async_std::task;\nuse hyper::service::{make_service_fn, service_fn};\nuse hyper::{Body, Request, Response, Server};\n\nuse compat; // This is the module from Step 2.\n\nasync fn hello(_: Request\u003cBody\u003e) -\u003e Result\u003cResponse\u003cBody\u003e, Infallible\u003e {\n    Ok(Response::new(Body::from(\"Hello World!\")))\n}\n\nfn main() -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    task::block_on(async {\n        let addr = \"127.0.0.1:3000\";\n        let listener = TcpListener::bind(addr).await?;\n\n        let make_svc = make_service_fn(|_conn| async { Ok::\u003c_, Infallible\u003e(service_fn(hello)) });\n        let server = Server::builder(compat::HyperListener(listener))\n            .executor(compat::HyperExecutor)\n            .serve(make_svc);\n\n        println!(\"Listening on http://{}\", addr);\n        server.await?;\n        Ok(())\n    })\n}\n```\n\n## License\n\n\u003csup\u003e\nLicensed under either of \u003ca href=\"LICENSE-APACHE\"\u003eApache License, Version\n2.0\u003c/a\u003e or \u003ca href=\"LICENSE-MIT\"\u003eMIT license\u003c/a\u003e at your option.\n\u003c/sup\u003e\n\n\u003cbr/\u003e\n\n\u003csub\u003e\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in this crate by you, as defined in the Apache-2.0 license, shall\nbe dual licensed as above, without any additional terms or conditions.\n\u003c/sub\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasync-rs%2Fasync-std-hyper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasync-rs%2Fasync-std-hyper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasync-rs%2Fasync-std-hyper/lists"}