From 0a81946664cccb6f90bd44e1688a44fa9d6b0615 Mon Sep 17 00:00:00 2001 From: glendc Date: Mon, 19 Aug 2024 14:30:32 +0200 Subject: [PATCH] provide StaticResponseFactory: to make custom response for ConsumeErr easy in future we might also want to allow a response function, but for now not needed. Open to feature requests or PRs for it --- src/http/response/into_response.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/http/response/into_response.rs b/src/http/response/into_response.rs index a3331d33d..9a1f7e7da 100644 --- a/src/http/response/into_response.rs +++ b/src/http/response/into_response.rs @@ -28,6 +28,30 @@ pub trait IntoResponse { fn into_response(self) -> Response; } +/// Wrapper that can be used to turn an `IntoResponse` type into +/// something that implements Into. +pub struct StaticResponseFactory(pub T); + +impl From> for Response { + fn from(value: StaticResponseFactory) -> Self { + value.0.into_response() + } +} + +impl fmt::Debug for StaticResponseFactory { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("StaticResponseFactory") + .field(&self.0) + .finish() + } +} + +impl Clone for StaticResponseFactory { + fn clone(&self) -> Self { + Self(self.0.clone()) + } +} + impl IntoResponse for StatusCode { fn into_response(self) -> Response { let mut res = ().into_response();