1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-22 16:16:40 +00:00

Add responder impl for Cow<str> (#2164)

This commit is contained in:
fakeshadow 2021-04-15 16:54:51 -07:00 committed by GitHub
parent 64bed506c2
commit 845c02cb86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 44 deletions

View file

@ -1,4 +1,5 @@
use std::{ use std::{
borrow::Cow,
fmt, mem, fmt, mem,
pin::Pin, pin::Pin,
task::{Context, Poll}, task::{Context, Poll},
@ -118,12 +119,23 @@ impl From<String> for Body {
} }
} }
impl<'a> From<&'a String> for Body { impl From<&'_ String> for Body {
fn from(s: &'a String) -> Body { fn from(s: &String) -> Body {
Body::Bytes(Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(&s))) 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 { impl From<Bytes> for Body {
fn from(s: Bytes) -> Body { fn from(s: Bytes) -> Body {
Body::Bytes(s) Body::Bytes(s)

View file

@ -1,4 +1,4 @@
use std::fmt; use std::{borrow::Cow, fmt};
use actix_http::{ use actix_http::{
body::Body, body::Body,
@ -117,53 +117,29 @@ impl<T: Responder> Responder for (T, StatusCode) {
} }
} }
impl Responder for &'static str { macro_rules! impl_responder {
($res: ty, $ct: path) => {
impl Responder for $res {
fn respond_to(self, _: &HttpRequest) -> HttpResponse { fn respond_to(self, _: &HttpRequest) -> HttpResponse {
HttpResponse::Ok() HttpResponse::Ok().content_type($ct).body(self)
.content_type(mime::TEXT_PLAIN_UTF_8)
.body(self)
} }
} }
};
}
impl Responder for &'static [u8] { impl_responder!(&'static str, mime::TEXT_PLAIN_UTF_8);
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.content_type(mime::APPLICATION_OCTET_STREAM)
.body(self)
}
}
impl Responder for String { impl_responder!(String, mime::TEXT_PLAIN_UTF_8);
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.content_type(mime::TEXT_PLAIN_UTF_8)
.body(self)
}
}
impl<'a> Responder for &'a String { impl_responder!(&'_ String, mime::TEXT_PLAIN_UTF_8);
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.content_type(mime::TEXT_PLAIN_UTF_8)
.body(self)
}
}
impl Responder for Bytes { impl_responder!(Cow<'_, str>, mime::TEXT_PLAIN_UTF_8);
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.content_type(mime::APPLICATION_OCTET_STREAM)
.body(self)
}
}
impl Responder for BytesMut { impl_responder!(&'static [u8], mime::APPLICATION_OCTET_STREAM);
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
HttpResponse::Ok() impl_responder!(Bytes, mime::APPLICATION_OCTET_STREAM);
.content_type(mime::APPLICATION_OCTET_STREAM)
.body(self) impl_responder!(BytesMut, mime::APPLICATION_OCTET_STREAM);
}
}
/// Allows overriding status code and headers for a responder. /// Allows overriding status code and headers for a responder.
pub struct CustomResponder<T> { pub struct CustomResponder<T> {
@ -358,6 +334,31 @@ pub(crate) mod tests {
HeaderValue::from_static("text/plain; charset=utf-8") 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); let resp = Bytes::from_static(b"test").respond_to(&req);
assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.body().bin_ref(), b"test"); assert_eq!(resp.body().bin_ref(), b"test");