Skip to content

Commit 425ff71

Browse files
committed
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.
1 parent 50fd4ab commit 425ff71

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/http/body.rs

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use bytes::Bytes;
22
use futures::{Poll, Stream};
33
use futures::sync::mpsc;
44
use tokio_proto;
5+
use std::borrow::Cow;
56

67
use http::Chunk;
78

@@ -94,6 +95,17 @@ impl From<&'static [u8]> for Body {
9495
}
9596
}
9697

98+
impl From<Cow<'static, [u8]>> for Body {
99+
#[inline]
100+
fn from (cow: Cow<'static, [u8]>) -> Body {
101+
if let Cow::Borrowed(value) = cow {
102+
Body::from(value)
103+
} else {
104+
Body::from(cow.to_owned())
105+
}
106+
}
107+
}
108+
97109
impl From<String> for Body {
98110
#[inline]
99111
fn from (s: String) -> Body {
@@ -108,6 +120,17 @@ impl From<&'static str> for Body {
108120
}
109121
}
110122

123+
impl From<Cow<'static, str>> for Body {
124+
#[inline]
125+
fn from (cow: Cow<'static, str>) -> Body {
126+
if let Cow::Borrowed(value) = cow {
127+
Body::from(value)
128+
} else {
129+
Body::from(cow.to_owned())
130+
}
131+
}
132+
}
133+
111134
impl From<Option<Body>> for Body {
112135
#[inline]
113136
fn from (body: Option<Body>) -> Body {

0 commit comments

Comments
 (0)