diff --git a/core/lib/src/response/responder.rs b/core/lib/src/response/responder.rs index e60ba627..7b9824fb 100644 --- a/core/lib/src/response/responder.rs +++ b/core/lib/src/response/responder.rs @@ -1,5 +1,6 @@ use std::fs::File; use std::io::Cursor; +use std::sync::Arc; use crate::http::{Status, ContentType, StatusClass}; use crate::response::{self, Response}; @@ -319,6 +320,37 @@ impl<'r> Responder<'r, 'static> for String { } } +#[repr(transparent)] +struct DerefRef(T); + +impl AsRef<[u8]> for DerefRef where T::Target: AsRef<[u8]> { + fn as_ref(&self) -> &[u8] { + self.0.deref().as_ref() + } +} + +/// Returns a response with Content-Type `text/plain` and a fixed-size body +/// containing the string `self`. Always returns `Ok`. +impl<'r> Responder<'r, 'static> for Arc { + fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> { + Response::build() + .header(ContentType::Plain) + .sized_body(self.len(), Cursor::new(DerefRef(self))) + .ok() + } +} + +/// Returns a response with Content-Type `text/plain` and a fixed-size body +/// containing the string `self`. Always returns `Ok`. +impl<'r> Responder<'r, 'static> for Box { + fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> { + Response::build() + .header(ContentType::Plain) + .sized_body(self.len(), Cursor::new(DerefRef(self))) + .ok() + } +} + /// Returns a response with Content-Type `application/octet-stream` and a /// fixed-size body containing the data in `self`. Always returns `Ok`. impl<'r, 'o: 'r> Responder<'r, 'o> for &'o [u8] { @@ -341,6 +373,28 @@ impl<'r> Responder<'r, 'static> for Vec { } } +/// Returns a response with Content-Type `application/octet-stream` and a +/// fixed-size body containing the data in `self`. Always returns `Ok`. +impl<'r> Responder<'r, 'static> for Arc<[u8]> { + fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> { + Response::build() + .header(ContentType::Binary) + .sized_body(self.len(), Cursor::new(self)) + .ok() + } +} + +/// Returns a response with Content-Type `application/octet-stream` and a +/// fixed-size body containing the data in `self`. Always returns `Ok`. +impl<'r> Responder<'r, 'static> for Box<[u8]> { + fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> { + Response::build() + .header(ContentType::Binary) + .sized_body(self.len(), Cursor::new(self)) + .ok() + } +} + /// 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> {