-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(volo-http): support
service_fn
(#317)
* feat(volo-http): support `service_fn` as route * chore(volo-http): remove extractor for `HeaderMap` Extracting `HeaderMap` needs a `clone` and it may cost a long time. To use the `HeaderMap` without copy or clone, the `service_fn` is a better choice. --------- Signed-off-by: Yu Li <[email protected]>
- Loading branch information
1 parent
660ec92
commit 4efb1e4
Showing
7 changed files
with
100 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::{convert::Infallible, fmt, future::Future}; | ||
|
||
use hyper::body::Incoming; | ||
use motore::service::Service; | ||
|
||
use crate::{HttpContext, Response}; | ||
|
||
/// Returns a new [`ServiceFn`] with the given closure. | ||
/// | ||
/// This lets you build a [`Service`] from an async function that returns a [`Result`]. | ||
pub fn service_fn<F>(f: F) -> ServiceFn<F> { | ||
ServiceFn { f } | ||
} | ||
|
||
/// A [`Service`] implemented by a closure. See the docs for [`service_fn`] for more details. | ||
#[derive(Copy, Clone)] | ||
pub struct ServiceFn<F> { | ||
f: F, | ||
} | ||
|
||
impl<F> Service<HttpContext, Incoming> for ServiceFn<F> | ||
where | ||
F: for<'r> Callback<'r>, | ||
{ | ||
type Response = Response; | ||
type Error = Infallible; | ||
|
||
fn call<'s, 'cx>( | ||
&'s self, | ||
cx: &'cx mut HttpContext, | ||
req: Incoming, | ||
) -> impl Future<Output = Result<Self::Response, Self::Error>> { | ||
(self.f).call(cx, req) | ||
} | ||
} | ||
|
||
impl<F> fmt::Debug for ServiceFn<F> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("ServiceFn") | ||
.field("f", &format_args!("{}", std::any::type_name::<F>())) | ||
.finish() | ||
} | ||
} | ||
|
||
/// [`Service`] for binding lifetime to return value while using closure. | ||
/// This is just a temporary workaround for lifetime issues. | ||
/// | ||
/// Related issue: https://github.com/rust-lang/rust/issues/70263. | ||
/// Related RFC: https://github.com/rust-lang/rfcs/pull/3216. | ||
pub trait Callback<'r> { | ||
type Future: Future<Output = Result<Response, Infallible>> + Send + 'r; | ||
|
||
fn call(&self, cx: &'r mut HttpContext, req: Incoming) -> Self::Future; | ||
} | ||
|
||
impl<'r, F, Fut> Callback<'r> for F | ||
where | ||
F: Fn(&'r mut HttpContext, Incoming) -> Fut, | ||
Fut: Future<Output = Result<Response, Infallible>> + Send + 'r, | ||
{ | ||
type Future = Fut; | ||
|
||
fn call(&self, cx: &'r mut HttpContext, req: Incoming) -> Self::Future { | ||
self(cx, req) | ||
} | ||
} |