Creating a hyper service in a match expression #2757
Answered
by
olix0r
mihaigalos
asked this question in
Q&A
-
Hi, I'm trying to do the following: let service = match args.is_present("secure") {
true => Box::new(make_service_fn(|_| async {
Ok::<_, hyper::Error>(service_fn(service_secure))
})),
false => Box::new(make_service_fn(|_| async {
Ok::<_, hyper::Error>(service_fn(service))
})),
}; This doesn't compile: 103 | let service = match args.is_present("secure") {
| ------------------------------- `match` arms have incompatible types
104 | true => Box::new(make_service_fn(|_| async {
| _________________-
105 | | Ok::<_, hyper::Error>(service_fn(service_secure))
106 | | })),
| |___________- this is found to be of type `Box<hyper::service::make::MakeServiceFn<[closure@src/main.rs:104:42: 106:10]>>`
107 | false => Box::new(make_service_fn(|_| async {
| __________________^
108 | | Ok::<_, hyper::Error>(service_fn(service))
109 | | })),
| |___________^ expected closure, found a different closure Any way around this? |
Beta Was this translation helpful? Give feedback.
Answered by
olix0r
Feb 12, 2022
Replies: 1 comment 1 reply
-
You probably want to use let is_secure = args.is_present("secure");
let service = make_service_fn(move |_| async move {
let svc = if is_secure {
BoxService::new(service_fn(call_secure))
} else {
BoxService::new(service_fn(call_insecure))
};
Ok::<_, hyper::Error>(svc)
}); If you don't want to move the comparison into make_service, you'll need both the Also note there's a |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
mihaigalos
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You probably want to use
tower::util::BoxService
. If you really only want to vary the service definition i'd probably move the match into the makeservice, too:If you don't want to move the comparison into make_service, you'll need both the
make_service_fn
andservice_fn
to be wrapped in BoxService.Also note there's a
BoxCloneService
if the service needs to implement clone.