Skip to content

Commit abad0fc

Browse files
authored
Merge pull request #1080 from hyperium/bytes
feat(http): use the bytes crate for Chunk and internally
2 parents cf7cc50 + 65b3e08 commit abad0fc

File tree

14 files changed

+307
-599
lines changed

14 files changed

+307
-599
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ include = [
2020
]
2121

2222
[dependencies]
23-
base64 = "0.4.0"
23+
base64 = "0.4"
24+
bytes = "0.4"
2425
futures = "0.1.7"
2526
futures-cpupool = "0.1"
2627
httparse = "1.0"

src/header/common/link.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ mod tests {
933933
use header::Header;
934934

935935
use http::{ServerTransaction, Http1Transaction};
936-
use http::buf::MemBuf;
936+
use bytes::BytesMut;
937937

938938
use mime::Mime;
939939
use mime::TopLevel::Text;
@@ -1018,7 +1018,7 @@ mod tests {
10181018

10191019
let expected_link = Link::new(vec![first_link, second_link, third_link]);
10201020

1021-
let raw = MemBuf::from(b"GET /super_short_uri/and_whatever HTTP/1.1\r\nHost: \
1021+
let mut raw = BytesMut::from(b"GET /super_short_uri/and_whatever HTTP/1.1\r\nHost: \
10221022
hyper.rs\r\nAccept: a lot of things\r\nAccept-Charset: \
10231023
utf8\r\nAccept-Encoding: *\r\nLink: </TheBook/chapter2>; \
10241024
rel=\"previous\"; title*=UTF-8'de'letztes%20Kapitel, \
@@ -1029,7 +1029,7 @@ mod tests {
10291029
rel=\"previous\"; rev=next; title=\"previous chapter\"\
10301030
\r\n\r\n".to_vec());
10311031

1032-
let (mut res, _) = ServerTransaction::parse(&raw).unwrap().unwrap();
1032+
let (mut res, _) = ServerTransaction::parse(&mut raw).unwrap().unwrap();
10331033

10341034
let link = res.headers.remove::<Link>().unwrap();
10351035

src/header/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ use self::internals::{Item, VecMap, Entry};
8787
pub use self::shared::*;
8888
pub use self::common::*;
8989
pub use self::raw::Raw;
90-
use http::buf::MemSlice;
90+
use bytes::Bytes;
9191

9292
mod common;
9393
mod internals;
@@ -611,8 +611,8 @@ impl<'a> Extend<HeaderView<'a>> for Headers {
611611
}
612612
}
613613

614-
impl<'a> Extend<(&'a str, MemSlice)> for Headers {
615-
fn extend<I: IntoIterator<Item=(&'a str, MemSlice)>>(&mut self, iter: I) {
614+
impl<'a> Extend<(&'a str, Bytes)> for Headers {
615+
fn extend<I: IntoIterator<Item=(&'a str, Bytes)>>(&mut self, iter: I) {
616616
for (name, value) in iter {
617617
let name = HeaderName(UniCase(maybe_literal(name)));
618618
//let trim = header.value.iter().rev().take_while(|&&x| x == b' ').count();

src/header/raw.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Cow;
22
use std::fmt;
3-
use http::buf::MemSlice;
3+
use bytes::Bytes;
44

55
/// A raw header value.
66
#[derive(Clone, PartialEq, Eq)]
@@ -72,7 +72,7 @@ enum Lines {
7272
enum Line {
7373
Static(&'static [u8]),
7474
Owned(Vec<u8>),
75-
Shared(MemSlice),
75+
Shared(Bytes),
7676
}
7777

7878
fn eq<A: AsRef<[u8]>, B: AsRef<[u8]>>(a: &[A], b: &[B]) -> bool {
@@ -152,9 +152,9 @@ impl<'a> From<&'a [u8]> for Raw {
152152
}
153153
}
154154

155-
impl From<MemSlice> for Raw {
155+
impl From<Bytes> for Raw {
156156
#[inline]
157-
fn from(val: MemSlice) -> Raw {
157+
fn from(val: Bytes) -> Raw {
158158
Raw(Lines::One(Line::Shared(val)))
159159
}
160160
}
@@ -166,9 +166,9 @@ impl From<Vec<u8>> for Line {
166166
}
167167
}
168168

169-
impl From<MemSlice> for Line {
169+
impl From<Bytes> for Line {
170170
#[inline]
171-
fn from(val: MemSlice) -> Line {
171+
fn from(val: Bytes) -> Line {
172172
Line::Shared(val)
173173
}
174174
}
@@ -183,11 +183,11 @@ impl AsRef<[u8]> for Line {
183183
}
184184
}
185185

186-
pub fn parsed(val: MemSlice) -> Raw {
186+
pub fn parsed(val: Bytes) -> Raw {
187187
Raw(Lines::One(From::from(val)))
188188
}
189189

190-
pub fn push(raw: &mut Raw, val: MemSlice) {
190+
pub fn push(raw: &mut Raw, val: Bytes) {
191191
raw.push_line(Line::from(val));
192192
}
193193

src/http/body.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::convert::From;
2-
3-
use tokio_proto;
4-
use http::Chunk;
1+
use bytes::Bytes;
52
use futures::{Poll, Stream};
63
use futures::sync::mpsc;
4+
use tokio_proto;
5+
6+
use http::Chunk;
77

88
pub type TokioBody = tokio_proto::streaming::Body<Chunk, ::Error>;
99

@@ -58,6 +58,12 @@ impl From<Chunk> for Body {
5858
}
5959
}
6060

61+
impl From<Bytes> for Body {
62+
fn from (bytes: Bytes) -> Body {
63+
Body(TokioBody::from(Chunk::from(bytes)))
64+
}
65+
}
66+
6167
impl From<Vec<u8>> for Body {
6268
fn from (vec: Vec<u8>) -> Body {
6369
Body(TokioBody::from(Chunk::from(vec)))

0 commit comments

Comments
 (0)