I did similar with hyper 0.11
, but don't know how to get it works with hyper 0.12
:
extern crate hyper;
extern crate futures;
use hyper::service::{NewService, Service};
use hyper::{Body,Error,Response,StatusCode,Request,Server};
use futures::{future, Future};
struct TestServer {
something: u64,
}
impl NewService for TestServer {
type ReqBody = Body;
type ResBody = Body;
type Error = Error;
type InitError = Error;
type Service = TestServer;
type Future = Box<Future<Item = Self::Service, Error = Self::InitError> + Send>;
fn new_service(&self) -> Self::Future {
Box::new(future::ok(Self {
something: self.something,
}))
}
}
impl Service for TestServer {
type ReqBody = Body;
type ResBody = Body;
type Error = Error;
type Future = Box<Future<Item = Response<Body>, Error = Error> + Send>;
fn call(&mut self, _req: Request<Self::ReqBody>) -> Self::Future {
let something = self.something.to_string();
Box::new(future::ok(
Response::builder()
.status(StatusCode::OK)
.body(something.into())
.unwrap()
))
}
}
impl TestServer {
fn start(self, port: u16) {
let addr = format!("0.0.0.0:{}", port).parse().unwrap();
let server = Server::bind(&addr).serve(self);
println!("Serving at {}", addr);
hyper::rt::run(server); //<=========================== Error here!
}
}
fn main() {
let test = TestServer{something:42};
test.start(8888);
}
Error:
error[E0271]: type mismatch resolving `<hyper::Server<hyper::server::conn::AddrIncoming, TestServer> as futures::Future>::Error == ()`
--> src/main.rs:47:9
|
47 | hyper::rt::run(server);
| ^^^^^^^^^^^^^^ expected struct `hyper::Error`, found ()
|
= note: expected type `hyper::Error`
found type `()`
= note: required by `hyper::rt::run`
I also try to do similar to this, but it failed too:
let mut runtime = tokio::runtime::Runtime::new()?;
runtime.spawn(server);
Does this piece of code have any chance to compile and run with hyper 0.12
?