feat(http): add Body::from(cow) for bytes and strings

This change adds the ability to use Cow<'static, [u8]> and Cow<'static, str> for the body of a
HTTP request or response.
This makes it easier to create abstractions that serve static web pages, redirect messages and the
like.
This commit is contained in:
Raphael Cohn
2017-09-07 10:50:51 +01:00
parent 50fd4ab96d
commit 425ff71d75

View File

@@ -2,6 +2,7 @@ use bytes::Bytes;
use futures::{Poll, Stream};
use futures::sync::mpsc;
use tokio_proto;
use std::borrow::Cow;
use http::Chunk;
@@ -94,6 +95,17 @@ impl From<&'static [u8]> for Body {
}
}
impl From<Cow<'static, [u8]>> for Body {
#[inline]
fn from (cow: Cow<'static, [u8]>) -> Body {
if let Cow::Borrowed(value) = cow {
Body::from(value)
} else {
Body::from(cow.to_owned())
}
}
}
impl From<String> for Body {
#[inline]
fn from (s: String) -> Body {
@@ -108,6 +120,17 @@ impl From<&'static str> for Body {
}
}
impl From<Cow<'static, str>> for Body {
#[inline]
fn from (cow: Cow<'static, str>) -> Body {
if let Cow::Borrowed(value) = cow {
Body::from(value)
} else {
Body::from(cow.to_owned())
}
}
}
impl From<Option<Body>> for Body {
#[inline]
fn from (body: Option<Body>) -> Body {