-
-
Save inv2004/9593c35216ee7a94268eb39f47ecaa52 to your computer and use it in GitHub Desktop.
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
#![feature(async_await)] | |
extern crate futures; | |
extern crate hyper; | |
use std::sync::Arc; | |
use std::collections::HashMap; | |
use futures::{FutureExt, TryFutureExt, Future, Poll}; | |
use futures::compat::{Future01CompatExt}; | |
use hyper::{Body, Client, Request, Response, Server, Uri, rt::run, service::service_fn, Method, rt::Stream}; | |
use futures::task::Context; | |
use std::net::SocketAddr; | |
use std::error::Error; | |
type Map = HashMap<String, String>; | |
async fn start_server(addr: SocketAddr) { | |
println!("Listening on http://{}", addr); | |
let start_fut = Server::bind(&addr).serve(|| service_fn(|req| serve_req(req).boxed().compat()) ); | |
if let Err(e) = start_fut.compat().await { | |
eprintln!("server error: {}", e); | |
} | |
} | |
async fn serve_req(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { | |
match *req.method() { | |
Method::GET => println!("get"), | |
Method::PUT => { | |
let body = req.into_body().concat2().compat().await?; | |
let s = String::from_utf8(body.to_vec()).unwrap(); | |
println!("put"); | |
}, | |
_ => panic!("unknown") | |
} | |
Ok(Response::new(Body::from("test"))) | |
} | |
fn main() { | |
let addr: SocketAddr = "127.0.0.1:9999".parse().unwrap(); | |
run(start_server(addr).unit_error().boxed().compat()); | |
} | |
// ERROR: | |
error[E0277]: `(dyn hyper::rt::Stream<Error = std::boxed::Box<(dyn std::error::Error + std::marker::Send + std::marker::Sync + 'static)>, Item = hyper::Chunk> + std::marker::Send + 'static)` cannot be shared between threads safely | |
--> src\main.rs:19:82 | |
| | |
19 | let start_fut = Server::bind(&addr).serve(|| service_fn(|req| serve_req(req).boxed().compat()) ); | |
| ^^^^^ `(dyn hyper::rt::Stream<Error = std::boxed::Box<(dyn std::error::Error + std::marker::Send + std::marker::Sync + 'static)>, Item = hyper::Chunk> + std::marker::Send + 'static)` cannot be shared between threads safely | |
| | |
= help: the trait `std::marker::Sync` is not implemented for `(dyn hyper::rt::Stream<Error = std::boxed::Box<(dyn std::error::Error + std::marker::Send + std::marker::Sync + 'static)>, Item = hyper::Chunk> + std::marker::Send + 'static)` | |
= note: required because of the requirements on the impl of `std::marker::Sync` for `std::ptr::Unique<(dyn hyper::rt::Stream<Error = std::boxed::Box<(dyn std::error::Error + std::marker::Send + std::marker::Sync + 'static)>, Item = hyper::Chunk> + std::marker::Send + 'static)>` | |
= note: required because it appears within the type `std::boxed::Box<(dyn hyper::rt::Stream<Error = std::boxed::Box<(dyn std::error::Error + std::marker::Send + std::marker::Sync + 'static)>, Item = hyper::Chunk> + std::marker::Send + 'static)>` | |
= note: required because it appears within the type `hyper::body::body::Kind` | |
= note: required because it appears within the type `hyper::Body` | |
= note: required because it appears within the type `hyper::Request<hyper::Body>` | |
= note: required because of the requirements on the impl of `std::marker::Send` for `&hyper::Request<hyper::Body>` | |
= note: required because it appears within the type `for<'r, 's> {hyper::Request<hyper::Body>, &'r hyper::Request<hyper::Body>, &'s hyper::Method, hyper::Method, fn(std::result::Result<hyper::Chunk, hyper::Error>) -> std::result::Result<<std::result::Result<hyper::Chunk, hyper::Error> as std::ops::Try>::Ok, <std::result::Result<hyper::Chunk, hyper::Error> as std::ops::Try>::Error> {<std::result::Result<hyper::Chunk, hyper::Error> as std::ops::Try>::into_result}, futures::compat::Compat01As03<futures::stream::concat::Concat2<hyper::Body>>, ()}` | |
= note: required because it appears within the type `[static generator@src\main.rs:27:80: 38:2 req:hyper::Request<hyper::Body> for<'r, 's> {hyper::Request<hyper::Body>, &'r hyper::Request<hyper::Body>, &'s hyper::Method, hyper::Method, fn(std::result::Result<hyper::Chunk, hyper::Error>) -> std::result::Result<<std::result::Result<hyper::Chunk, hyper::Error> as std::ops::Try>::Ok, <std::result::Result<hyper::Chunk, hyper::Error> as std::ops::Try>::Error> {<std::result::Result<hyper::Chunk, hyper::Error> as std::ops::Try>::into_result}, futures::compat::Compat01As03<futures::stream::concat::Concat2<hyper::Body>>, ()}]` | |
= note: required because it appears within the type `std::future::GenFuture<[static generator@src\main.rs:27:80: 38:2 req:hyper::Request<hyper::Body> for<'r, 's> {hyper::Request<hyper::Body>, &'r hyper::Request<hyper::Body>, &'s hyper::Method, hyper::Method, fn(std::result::Result<hyper::Chunk, hyper::Error>) -> std::result::Result<<std::result::Result<hyper::Chunk, hyper::Error> as std::ops::Try>::Ok, <std::result::Result<hyper::Chunk, hyper::Error> as std::ops::Try>::Error> {<std::result::Result<hyper::Chunk, hyper::Error> as std::ops::Try>::into_result}, futures::compat::Compat01As03<futures::stream::concat::Concat2<hyper::Body>>, ()}]>` | |
= note: required because it appears within the type `impl futures::Future` | |
= note: required because it appears within the type `impl futures::Future` | |
error: aborting due to previous error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment