This is my code:
use std::convert::Infallible;
use std::future::Future;
use hyper::{Body, Request, Response};
use crate::router::{Match, Router, Params};
type ResponseType = Result<Response<Body>, Infallible>;
pub fn make_router() -> Router<&'static impl Fn(Request<Body>) -> dyn Future<Output=ResponseType>> {
let mut router: Router<&'static fn(Request<Body>) -> dyn Future<Output=ResponseType>> = Router::new();
router.add("/", &hello);
router.add("/a/*", &request_a);
router.add("/b/*", &request_b);
router
}
pub fn match_router(rt: Router<&'static fn(Request<Body>) -> dyn Future<Output=ResponseType>>, url: &str) -> &'static fn(Request<Body>) -> dyn Future<Output=ResponseType> {
match rt.recognize(url.as_str()) {
Ok(Match { handler, params }) => *handler,
_ => &hello,
}
}
async fn hello(req: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok(Response::new(Body::from("Hello, World!\n")))
}
async fn request_a(req: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok(Response::new(Body::from("Hello, World A!\n")))
}
async fn request_b(req: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok(Response::new(Body::from("Hello, World B!\n")))
}
When I compiled it, it show me these errors:
error[E0308]: mismatched types
--> src/dispatcher.rs:12:21
|
12 | router.add("/", &hello);
| ^^^^^^ expected fn pointer, found fn item
|
= note: expected reference `&'static fn(hyper::Request<hyper::Body>) -> (dyn std::future::Future<Output = std::result::Result<hyper::Response<hyper::Body>, std::convert::Infallible>> + 'static)`
found reference `&fn(hyper::Request<hyper::Body>) -> impl std::future::Future {dispatcher::hello}`
error[E0308]: mismatched types
--> src/dispatcher.rs:13:24
|
13 | router.add("/a/*", &request_a);
| ^^^^^^^^^^ expected fn pointer, found fn item
|
= note: expected reference `&'static fn(hyper::Request<hyper::Body>) -> (dyn std::future::Future<Output = std::result::Result<hyper::Response<hyper::Body>, std::convert::Infallible>> + 'static)`
found reference `&fn(hyper::Request<hyper::Body>) -> impl std::future::Future {dispatcher::request_a}`
I'm wondering about the fn pointer
, how to pass the correct fn pointer
?