From 425ff71d75f4b8d57d2f3682bf799f684ed604dd Mon Sep 17 00:00:00 2001 From: Raphael Cohn Date: Thu, 7 Sep 2017 10:50:51 +0100 Subject: [PATCH] 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. --- src/http/body.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/http/body.rs b/src/http/body.rs index aab1d90a9f..e8c781be45 100644 --- a/src/http/body.rs +++ b/src/http/body.rs @@ -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> 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 for Body { #[inline] fn from (s: String) -> Body { @@ -108,6 +120,17 @@ impl From<&'static str> for Body { } } +impl From> 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> for Body { #[inline] fn from (body: Option) -> Body {