Skip to content

Instantly share code, notes, and snippets.

@ensc
Created January 22, 2018 19:53
Show Gist options
  • Save ensc/155b28cbc07af11c3309348f7b19d9df to your computer and use it in GitHub Desktop.
Save ensc/155b28cbc07af11c3309348f7b19d9df to your computer and use it in GitHub Desktop.
extern crate hyper;
extern crate futures;
use hyper::server::{Service, Http, Request, Response};
struct Environment {
}
struct HttpService<'a> {
pub env: &'a Environment,
}
impl <'a> HttpService<'a> {
pub fn new(env: &'a Environment) -> Self {
HttpService {
env: env
}
}
}
impl <'a> Service for HttpService<'a> {
type Request = Request;
type Response = Response;
type Future = futures::future::FutureResult<Self::Response, Self::Error>;
type Error = hyper::Error;
fn call(&self, _req: Request) -> Self::Future {
futures::future::ok(Response::new())
}
}
fn foo() {
let addr = "127.0.0.1:3000".parse().unwrap();
let env = Environment{};
let server = Http::new().bind(&addr,
move || Ok(HttpService::new(&env)));
server.unwrap().run().unwrap();
}
extern crate tokio_core;
fn bar() {
use tokio_core::net::TcpListener;
let addr = "127.0.0.1:3000".parse().unwrap();
let env = Environment{};
let mut core = tokio_core::reactor::Core::new().unwrap();
let handle = core.handle();
use futures::Stream;
use futures::Future;
let listener = TcpListener::bind(&addr, &handle).unwrap()
.incoming()
.for_each(move |(socket, addr)| {
let svc = HttpService::new(&env);
let fut = Http::<hyper::Chunk>::new()
.serve_connection(socket, svc)
.map(|_| ())
.map_err(|_| panic!("err"));
handle.spawn(fut);
Ok(())
});;
}
@ensc
Copy link
Author

ensc commented Jan 22, 2018

error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
  --> main.rs:58:40
   |
58 |             let svc = HttpService::new(&env);
   |                                        ^^^^
   |
note: first, the lifetime cannot outlive the lifetime  as defined on the body at 57:19...
  --> main.rs:57:19
   |
57 |           .for_each(move |(socket, addr)| {
   |  ___________________^
58 | |             let svc = HttpService::new(&env);
59 | |             let fut = Http::<hyper::Chunk>::new()
60 | |                 .serve_connection(socket, svc)
...  |
64 | |             Ok(())
65 | |         });;
   | |_________^
note: ...so that closure can access `env`
  --> main.rs:58:40
   |
58 |             let svc = HttpService::new(&env);
   |                                        ^^^^
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `futures::MapErr<futures::Map<hyper::server::Connection<tokio_core::net::TcpStream, HttpService<'_>>, [[email protected]:61:22: 61:28]>, [[email protected]:62:26: 62:43]>` will meet its required lifetime bounds
  --> main.rs:63:20
   |
63 |             handle.spawn(fut);
   |                    ^^^^^

@ensc
Copy link
Author

ensc commented Jan 22, 2018

[ensc@sheridan /]$ rustc --version
rustc 1.22.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment