From d21608ca7b52b40c0bee8e9eaba58da05bf6afab Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Wed, 28 Apr 2021 01:21:57 -0700 Subject: [PATCH] Remove 'Content' in favor of 'content::Custom'. Also adds the shorthand '(ContentType, R)', where 'R: Responder', responder implementation. This brings it to parity with the 'response::status' API. --- contrib/lib/src/templates/mod.rs | 4 ++-- core/lib/src/response/content.rs | 32 +++++++++++++++++++++++++++----- core/lib/src/response/mod.rs | 1 - 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/contrib/lib/src/templates/mod.rs b/contrib/lib/src/templates/mod.rs index 0409b8fc..ac06ba0f 100644 --- a/contrib/lib/src/templates/mod.rs +++ b/contrib/lib/src/templates/mod.rs @@ -140,7 +140,7 @@ use std::error::Error; use rocket::{Rocket, Orbit, Ignite, Sentinel}; use rocket::request::Request; use rocket::fairing::Fairing; -use rocket::response::{self, Content, Responder}; +use rocket::response::{self, Responder}; use rocket::http::{ContentType, Status}; const DEFAULT_TEMPLATE_DIR: &str = "templates"; @@ -430,7 +430,7 @@ impl<'r> Responder<'r, 'static> for Template { self.finalize(&ctxt)? }; - Content(content_type, render).respond_to(req) + (content_type, render).respond_to(req) } } diff --git a/core/lib/src/response/content.rs b/core/lib/src/response/content.rs index 94eed797..42ff34a0 100644 --- a/core/lib/src/response/content.rs +++ b/core/lib/src/response/content.rs @@ -7,6 +7,21 @@ //! remainder of the response to the wrapped responder. This allows for setting //! the Content-Type of a type that doesn't set it itself or for overriding one //! that does. +//! +//! The [`Custom`] type allows responding with _any_ `Content-Type`. As a +//! convenience, `(ContentType, R)` where `R: Responder` is _also_ a +//! `Responder`, identical to `Custom`. +//! +//! ```rust +//! # use rocket::get; +//! use rocket::http::ContentType; +//! +//! #[get("/")] +//! fn index() -> (ContentType, &'static str) { +//! (ContentType::HTML, "Is this HTML?

Sure, why not!

") +//! } +//! ``` + //! //! # Example //! @@ -35,18 +50,18 @@ use crate::http::ContentType; /// Set the Content-Type of a string to PDF. /// /// ```rust -/// use rocket::response::content::Content; +/// use rocket::response::content::Custom; /// use rocket::http::ContentType; /// /// # #[allow(unused_variables)] -/// let response = Content(ContentType::PDF, "Hi."); +/// let response = Custom(ContentType::PDF, "Hi."); /// ``` #[derive(Debug, Clone, PartialEq)] -pub struct Content(pub ContentType, pub R); +pub struct Custom(pub ContentType, pub R); /// Overrides the Content-Type of the response to the wrapped `ContentType` then /// delegates the remainder of the response to the wrapped responder. -impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for Content { +impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for Custom { fn respond_to(self, req: &'r Request<'_>) -> response::Result<'o> { Response::build() .merge(self.1.respond_to(req)?) @@ -72,7 +87,7 @@ macro_rules! ctrs { /// remainder of the response to the wrapped responder. impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for $name { fn respond_to(self, req: &'r Request<'_>) -> response::Result<'o> { - Content(ContentType::$ct, self.0).respond_to(req) + Custom(ContentType::$ct, self.0).respond_to(req) } } )+ @@ -89,3 +104,10 @@ ctrs! { Css: CSS, "CSS", "text/css", JavaScript: JavaScript, "JavaScript", "application/javascript" } + +impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for (ContentType, R) { + #[inline(always)] + fn respond_to(self, request: &'r Request<'_>) -> response::Result<'o> { + Custom(self.0, self.1).respond_to(request) + } +} diff --git a/core/lib/src/response/mod.rs b/core/lib/src/response/mod.rs index 8688d3da..7524e55d 100644 --- a/core/lib/src/response/mod.rs +++ b/core/lib/src/response/mod.rs @@ -45,7 +45,6 @@ pub use self::flash::Flash; pub use self::named_file::NamedFile; pub use self::stream::Stream; pub use self::debug::Debug; -#[doc(inline)] pub use self::content::Content; /// Type alias for the `Result` of a [`Responder::respond_to()`] call. pub type Result<'r> = std::result::Result, crate::http::Status>;