Last active
August 30, 2015 20:57
-
-
Save panicbit/c629bdbeb22cd7579cd8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Instead of this fuss: | |
let prefix = Cursor::new("foo".to_owned().into_bytes()); | |
let data = Cursor::new((...).into_bytes()); // Some big Vec/String | |
let body: Box<Read + Send> = Box::new(prefix.chain(data)); | |
// We can now just write: | |
let prefix = "foo"; | |
let data = ...; // Some big Vec/String | |
let body = chain_body(prefix, data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::{self,Read}; | |
use std::borrow::Borrow; | |
use iron::response::{Response,WriteBody,ResponseBody}; | |
use iron::modifier::Modifier; | |
pub struct BodyChain<T: WriteBody + Send, U: WriteBody + Send>(pub T, pub U); | |
impl <T: WriteBody + Send, U: WriteBody + Send> WriteBody for BodyChain<T, U> { | |
fn write_body(&mut self, res: &mut ResponseBody) -> io::Result<()> { | |
let &mut BodyChain(ref mut a, ref mut b) = self; | |
a.write_body(res).and_then(|_| b.write_body(res)) | |
} | |
} | |
impl <T: WriteBody + Send + 'static, U: WriteBody + Send + 'static> Modifier<Response> for BodyChain<T,U> { | |
fn modify(self, res: &mut Response) { | |
res.body = Some(Box::new(self)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment