Skip to content

Instantly share code, notes, and snippets.

@panicbit
Last active August 30, 2015 20:57
Show Gist options
  • Save panicbit/c629bdbeb22cd7579cd8 to your computer and use it in GitHub Desktop.
Save panicbit/c629bdbeb22cd7579cd8 to your computer and use it in GitHub Desktop.
// 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);
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