Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add responder impl for Cow<str> #2164

Merged
merged 1 commit into from Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions actix-http/src/body/body.rs
@@ -1,4 +1,5 @@
use std::{
borrow::Cow,
fmt, mem,
pin::Pin,
task::{Context, Poll},
Expand Down Expand Up @@ -118,12 +119,23 @@ impl From<String> for Body {
}
}

impl<'a> From<&'a String> for Body {
fn from(s: &'a String) -> Body {
impl From<&'_ String> for Body {
fn from(s: &String) -> Body {
Body::Bytes(Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(&s)))
}
}

impl From<Cow<'_, str>> for Body {
fn from(s: Cow<'_, str>) -> Body {
match s {
Cow::Owned(s) => Body::from(s),
Cow::Borrowed(s) => {
Body::Bytes(Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(s)))
}
}
}
}

impl From<Bytes> for Body {
fn from(s: Bytes) -> Body {
Body::Bytes(s)
Expand Down
85 changes: 43 additions & 42 deletions src/responder.rs
@@ -1,4 +1,4 @@
use std::fmt;
use std::{borrow::Cow, fmt};

use actix_http::{
body::Body,
Expand Down Expand Up @@ -117,53 +117,29 @@ impl<T: Responder> Responder for (T, StatusCode) {
}
}

impl Responder for &'static str {
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.content_type(mime::TEXT_PLAIN_UTF_8)
.body(self)
}
macro_rules! impl_responder {
($res: ty, $ct: path) => {
impl Responder for $res {
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
HttpResponse::Ok().content_type($ct).body(self)
}
}
};
}

impl Responder for &'static [u8] {
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.content_type(mime::APPLICATION_OCTET_STREAM)
.body(self)
}
}
impl_responder!(&'static str, mime::TEXT_PLAIN_UTF_8);

impl Responder for String {
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.content_type(mime::TEXT_PLAIN_UTF_8)
.body(self)
}
}
impl_responder!(String, mime::TEXT_PLAIN_UTF_8);

impl<'a> Responder for &'a String {
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.content_type(mime::TEXT_PLAIN_UTF_8)
.body(self)
}
}
impl_responder!(&'_ String, mime::TEXT_PLAIN_UTF_8);

impl Responder for Bytes {
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.content_type(mime::APPLICATION_OCTET_STREAM)
.body(self)
}
}
impl_responder!(Cow<'_, str>, mime::TEXT_PLAIN_UTF_8);

impl Responder for BytesMut {
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.content_type(mime::APPLICATION_OCTET_STREAM)
.body(self)
}
}
impl_responder!(&'static [u8], mime::APPLICATION_OCTET_STREAM);

impl_responder!(Bytes, mime::APPLICATION_OCTET_STREAM);

impl_responder!(BytesMut, mime::APPLICATION_OCTET_STREAM);

/// Allows overriding status code and headers for a responder.
pub struct CustomResponder<T> {
Expand Down Expand Up @@ -358,6 +334,31 @@ pub(crate) mod tests {
HeaderValue::from_static("text/plain; charset=utf-8")
);

let s = String::from("test");
let resp = Cow::Borrowed(s.as_str()).respond_to(&req);
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.body().bin_ref(), b"test");
assert_eq!(
resp.headers().get(CONTENT_TYPE).unwrap(),
HeaderValue::from_static("text/plain; charset=utf-8")
);

let resp = Cow::<'_, str>::Owned(s).respond_to(&req);
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.body().bin_ref(), b"test");
assert_eq!(
resp.headers().get(CONTENT_TYPE).unwrap(),
HeaderValue::from_static("text/plain; charset=utf-8")
);

let resp = Cow::Borrowed("test").respond_to(&req);
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.body().bin_ref(), b"test");
assert_eq!(
resp.headers().get(CONTENT_TYPE).unwrap(),
HeaderValue::from_static("text/plain; charset=utf-8")
);

let resp = Bytes::from_static(b"test").respond_to(&req);
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.body().bin_ref(), b"test");
Expand Down