I have a library which uses hyper internally. I want the user to be able to create an App
which contains a Server
internally that handles HTTP connections.
use hyper::server::conn::AddrIncoming;
use hyper::server::Server;
use hyper::service::service_fn_ok;
use std::net::SocketAddr;
pub struct App {
inner: Server<AddrIncoming, ()>,
}
impl App {
pub fn new() -> Self {
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
let inner = Server::bind(&addr).serve(|| service_fn_ok(|_req| unimplemented!()));
App { inner }
}
}
The error is, as expected:
error[E0308]: mismatched types
--> src/lib.rs:15:15
|
15 | App { inner }
| ^^^^^ expected (), found closure
|
= note: expected type `hyper::server::Server<_, ()>`
found type `hyper::server::Server<_, [closure@src/lib.rs:13:47: 13:88]>`
It's not well documented, but the second type parameter for Server
is the kind of MakeService
it uses.
I can't figure out how to refer to the closure in the type of inner
. Is there some way that I can box the closure to make the code compile? Is there a way to implement MakeService
by hand, instead of using a closure?
The hyper docs refer to the function make_service_fn
, which returns a MakeServiceFn
, but the type isn't public, so I can't use it in the type of inner
.