Skip to content

Commit

Permalink
feat(http): add Body::from(cow) for bytes and strings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
raphaelcohn committed Sep 7, 2017
1 parent 50fd4ab commit 425ff71
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/http/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 425ff71

Please sign in to comment.