Skip to content

Commit cc5b371

Browse files
authored
Merge pull request #13 from sfackler/write-body
Switch response from Box<Read> to Box<WriteBody>
2 parents 1a2cf3a + f1664c9 commit cc5b371

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extern crate semver;
22

33
use std::io::prelude::*;
4+
use std::io;
45
use std::collections::HashMap;
56
use std::error::Error;
67
use std::net::SocketAddr;
@@ -129,7 +130,7 @@ pub struct Response {
129130
pub headers: HashMap<String, Vec<String>>,
130131

131132
/// A Writer for body of the response
132-
pub body: Box<Read + Send>
133+
pub body: Box<WriteBody + Send>
133134
}
134135

135136
/// A Handler takes a request and returns a response or an error.
@@ -146,3 +147,18 @@ impl<F, E> Handler for F
146147
(*self)(request).map_err(|e| Box::new(e) as Box<Error+Send>)
147148
}
148149
}
150+
151+
/// A trait which writes the response body out to a `Write`r.
152+
///
153+
/// This is implemented for all `Read`ers.
154+
pub trait WriteBody {
155+
fn write_body(&mut self, out: &mut Write) -> io::Result<u64>;
156+
}
157+
158+
impl<R> WriteBody for R
159+
where R: Read
160+
{
161+
fn write_body(&mut self, out: &mut Write) -> io::Result<u64> {
162+
io::copy(self, out)
163+
}
164+
}

0 commit comments

Comments
 (0)