I am exploring the Iron web framework for Rust and have created a small handler that will read an image derived from the request URL, resize it and then deliver the result. From what I can tell an Iron Response can be built from a several different types, including types that implement the Read trait.
The save function in the image crate takes a type that implements the Write trait.
It feels like these two functions should be able to be hooked up such that the writer writes to a buffer that the reader reads from. I found the pipe crate, which seems to implement this behaviour but I'm having trouble getting the Read
end of the pipe into something that Iron will accept.
A slightly simplified version of my function:
fn artwork(req: &mut Request) -> IronResult<Response> {
let mut filepath = PathBuf::from("artwork/sample.png");
let img = match image::open(&filepath) {
Ok(img) => img,
Err(e) => return Err(IronError::new(e, status::InternalServerError))
};
let (mut read, mut write) = pipe::pipe();
thread::spawn(move || {
let thumb = img.resize(128, 128, image::FilterType::Triangle);
thumb.save(&mut write, image::JPEG).unwrap();
});
let mut res = Response::new();
res.status = Some(iron::status::Ok);
res.body = Some(Box::new(read));
Ok(res)
}
The error I'm receiving:
src/main.rs:70:21: 70:35 error: the trait `iron::response::WriteBody` is not implemented for the type `pipe::PipeReader` [E0277]
src/main.rs:70 res.body = Some(Box::new(read));
^~~~~~~~~~~~~~
PipeReader implements Read
and WriteBody is implemented for Read
so I feel this should work. I also tried:
let reader: Box<Read> = Box::new(read);
let mut res = Response::new();
res.status = Some(iron::status::Ok);
res.body = Some(reader);
but this gives the error:
src/main.rs:72:21: 72:27 error: mismatched types:
expected `Box<iron::response::WriteBody + Send>`,
found `Box<std::io::Read>`
(expected trait `iron::response::WriteBody`,
found trait `std::io::Read`) [E0308]
src/main.rs:72 res.body = Some(reader);
^~~~~~
How can I hook up the save
function to the Iron response body?