diff --git a/volo-http/src/utils/mod.rs b/volo-http/src/utils/mod.rs index baafeb5d..d86c1256 100644 --- a/volo-http/src/utils/mod.rs +++ b/volo-http/src/utils/mod.rs @@ -1,6 +1,3 @@ #![allow(unused)] pub mod consts; pub mod macros; -mod service_fn; - -pub use self::service_fn::{service_fn, Callback}; diff --git a/volo-http/src/utils/service_fn.rs b/volo-http/src/utils/service_fn.rs deleted file mode 100644 index b4fd2327..00000000 --- a/volo-http/src/utils/service_fn.rs +++ /dev/null @@ -1,71 +0,0 @@ -use std::{fmt, future::Future}; - -use motore::service::Service; - -/// 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) -> ServiceFn { - 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, -} - -impl Service for ServiceFn -where - F: for<'r> Callback<'r, Cx, Request, Response = R, Error = E>, - Request: 'static, - R: 'static, - E: 'static, -{ - type Response = R; - type Error = E; - - fn call<'s, 'cx>( - &'s self, - cx: &'cx mut Cx, - req: Request, - ) -> impl Future> { - (self.f).call(cx, req) - } -} - -impl fmt::Debug for ServiceFn { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("ServiceFn") - .field("f", &format_args!("{}", std::any::type_name::())) - .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, Cx, Request> { - type Response; - type Error; - type Future: Future> + Send + 'r; - - fn call(&self, cx: &'r mut Cx, req: Request) -> Self::Future; -} - -impl<'r, F, Fut, Cx, Request, R, E> Callback<'r, Cx, Request> for F -where - F: Fn(&'r mut Cx, Request) -> Fut, - Fut: Future> + Send + 'r, - Cx: 'r, -{ - type Response = R; - type Error = E; - type Future = Fut; - - fn call(&self, cx: &'r mut Cx, req: Request) -> Self::Future { - self(cx, req) - } -}