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::{
borrow::Cow,
fmt, mem,
pin::Pin,
task::{Context, Poll},
@ -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)

View file

@ -1,4 +1,4 @@
use std::fmt;
use std::{borrow::Cow, fmt};
use actix_http::{
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 {
HttpResponse::Ok()
.content_type(mime::TEXT_PLAIN_UTF_8)
.body(self)
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> {
@ -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");