From bf2bd0e9ede12dc227326d069cd84fc98dddf2e4 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Tue, 10 May 2022 15:33:37 -0500 Subject: [PATCH] Impl 'Responder' for 'Box'. In other words, allow boxing sized responders. Resolves #1999. --- .../ui-fail-nightly/catch_type_errors.stderr | 8 ++-- .../ui-fail-nightly/responder-types.stderr | 6 +-- core/lib/src/response/responder.rs | 42 +++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/core/codegen/tests/ui-fail-nightly/catch_type_errors.stderr b/core/codegen/tests/ui-fail-nightly/catch_type_errors.stderr index e311c72f..1054b6fb 100644 --- a/core/codegen/tests/ui-fail-nightly/catch_type_errors.stderr +++ b/core/codegen/tests/ui-fail-nightly/catch_type_errors.stderr @@ -15,7 +15,7 @@ error[E0277]: the trait bound `usize: Responder<'_, '_>` is not satisfied as Responder<'r, 'o>> as Responder<'r, 'static>> as Responder<'r, 'static>> - and 39 others + and 40 others error[E0277]: the trait bound `bool: Responder<'_, '_>` is not satisfied --> tests/ui-fail-nightly/catch_type_errors.rs:11:30 @@ -34,7 +34,7 @@ error[E0277]: the trait bound `bool: Responder<'_, '_>` is not satisfied as Responder<'r, 'o>> as Responder<'r, 'static>> as Responder<'r, 'static>> - and 39 others + and 40 others error[E0308]: mismatched types --> tests/ui-fail-nightly/catch_type_errors.rs:16:17 @@ -67,7 +67,7 @@ error[E0277]: the trait bound `usize: Responder<'_, '_>` is not satisfied as Responder<'r, 'o>> as Responder<'r, 'static>> as Responder<'r, 'static>> - and 39 others + and 40 others error[E0277]: the trait bound `usize: Responder<'_, '_>` is not satisfied --> tests/ui-fail-nightly/catch_type_errors.rs:21:12 @@ -86,4 +86,4 @@ error[E0277]: the trait bound `usize: Responder<'_, '_>` is not satisfied as Responder<'r, 'o>> as Responder<'r, 'static>> as Responder<'r, 'static>> - and 39 others + and 40 others diff --git a/core/codegen/tests/ui-fail-nightly/responder-types.stderr b/core/codegen/tests/ui-fail-nightly/responder-types.stderr index 2815320e..2db1a8cd 100644 --- a/core/codegen/tests/ui-fail-nightly/responder-types.stderr +++ b/core/codegen/tests/ui-fail-nightly/responder-types.stderr @@ -13,7 +13,7 @@ error[E0277]: the trait bound `u8: Responder<'_, '_>` is not satisfied as Responder<'r, 'o>> as Responder<'r, 'static>> as Responder<'r, 'static>> - and 43 others + and 44 others error[E0277]: the trait bound `Header<'_>: From` is not satisfied --> tests/ui-fail-nightly/responder-types.rs:11:5 @@ -53,7 +53,7 @@ error[E0277]: the trait bound `u8: Responder<'_, '_>` is not satisfied as Responder<'r, 'o>> as Responder<'r, 'static>> as Responder<'r, 'static>> - and 43 others + and 44 others error[E0277]: the trait bound `Header<'_>: From` is not satisfied --> tests/ui-fail-nightly/responder-types.rs:17:5 @@ -116,7 +116,7 @@ error[E0277]: the trait bound `usize: Responder<'_, '_>` is not satisfied as Responder<'r, 'o>> as Responder<'r, 'static>> as Responder<'r, 'static>> - and 43 others + and 44 others note: required by a bound in `route::handler::, Status, rocket::Data<'o>>>::from` --> $WORKSPACE/core/lib/src/route/handler.rs | diff --git a/core/lib/src/response/responder.rs b/core/lib/src/response/responder.rs index 3c74e7c5..0f3ddeb4 100644 --- a/core/lib/src/response/responder.rs +++ b/core/lib/src/response/responder.rs @@ -395,6 +395,48 @@ impl<'r> Responder<'r, 'static> for Box<[u8]> { } } +/// Returns the response generated by the inner `T`. Note that this explicitly +/// does not support `Box`. +/// +/// ```rust,compile_fail +/// #[macro_use] extern crate rocket; +/// +/// use rocket::response::Responder; +/// +/// #[get("/")] +/// fn f() -> Box> { +/// Box::new(()) +/// } +/// ``` +/// +/// However, this `impl` allows boxing sized responders: +/// +/// ```rust +/// #[macro_use] extern crate rocket; +/// +/// #[derive(Responder)] +/// enum Content { +/// Redirect(Box), +/// Text(String), +/// } +/// +/// #[get("/")] +/// fn f() -> Option> { +/// None +/// } +/// +/// #[get("/")] +/// fn g() -> Content { +/// Content::Text("hello".to_string()) +/// } +/// ``` +impl<'r, 'o: 'r, T: Responder<'r, 'o> + Sized> Responder<'r, 'o> for Box { + fn respond_to(self, req: &'r Request<'_>) -> response::Result<'o> { + let inner = *self; + inner.respond_to(req) + } +} + /// Returns a response with a sized body for the file. Always returns `Ok`. impl<'r> Responder<'r, 'static> for File { fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {