Impl 'Responder' for 'Box<T: Responder + Sized>'.

In other words, allow boxing sized responders.

Resolves #1999.
This commit is contained in:
Sergio Benitez 2022-05-10 15:33:37 -05:00
parent 8bd1e29677
commit bf2bd0e9ed
3 changed files with 49 additions and 7 deletions

View File

@ -15,7 +15,7 @@ error[E0277]: the trait bound `usize: Responder<'_, '_>` is not satisfied
<Accepted<R> as Responder<'r, 'o>>
<Arc<[u8]> as Responder<'r, 'static>>
<Arc<str> 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
<Accepted<R> as Responder<'r, 'o>>
<Arc<[u8]> as Responder<'r, 'static>>
<Arc<str> 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
<Accepted<R> as Responder<'r, 'o>>
<Arc<[u8]> as Responder<'r, 'static>>
<Arc<str> 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
<Accepted<R> as Responder<'r, 'o>>
<Arc<[u8]> as Responder<'r, 'static>>
<Arc<str> as Responder<'r, 'static>>
and 39 others
and 40 others

View File

@ -13,7 +13,7 @@ error[E0277]: the trait bound `u8: Responder<'_, '_>` is not satisfied
<Accepted<R> as Responder<'r, 'o>>
<Arc<[u8]> as Responder<'r, 'static>>
<Arc<str> as Responder<'r, 'static>>
and 43 others
and 44 others
error[E0277]: the trait bound `Header<'_>: From<u8>` 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
<Accepted<R> as Responder<'r, 'o>>
<Arc<[u8]> as Responder<'r, 'static>>
<Arc<str> as Responder<'r, 'static>>
and 43 others
and 44 others
error[E0277]: the trait bound `Header<'_>: From<u8>` 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
<Accepted<R> as Responder<'r, 'o>>
<Arc<[u8]> as Responder<'r, 'static>>
<Arc<str> as Responder<'r, 'static>>
and 43 others
and 44 others
note: required by a bound in `route::handler::<impl Outcome<rocket::Response<'o>, Status, rocket::Data<'o>>>::from`
--> $WORKSPACE/core/lib/src/route/handler.rs
|

View File

@ -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<dyn Responder>`.
///
/// ```rust,compile_fail
/// #[macro_use] extern crate rocket;
///
/// use rocket::response::Responder;
///
/// #[get("/")]
/// fn f() -> Box<dyn Responder<'static,'static>> {
/// Box::new(())
/// }
/// ```
///
/// However, this `impl` allows boxing sized responders:
///
/// ```rust
/// #[macro_use] extern crate rocket;
///
/// #[derive(Responder)]
/// enum Content {
/// Redirect(Box<rocket::response::Redirect>),
/// Text(String),
/// }
///
/// #[get("/")]
/// fn f() -> Option<Box<String>> {
/// None
/// }
///
/// #[get("/")]
/// fn g() -> Content {
/// Content::Text("hello".to_string())
/// }
/// ```
impl<'r, 'o: 'r, T: Responder<'r, 'o> + Sized> Responder<'r, 'o> for Box<T> {
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> {