tomaka / rouille

Web framework in Rust
Apache License 2.0
1.09k stars 105 forks source link

Response body should be a function that takes a Write #191

Open rightfold opened 5 years ago

rightfold commented 5 years ago

Currently the response body is a Read, but this makes it very difficult to stream bodies from non-Read sources (e.g. from a database query) as you need to write a state machine to fill the buffer. If the response body is a function that takes a Write, then this will be very easy as you can just call write repeatedly from within the function.

The function could be called by Rouille with the stream directly, or with a wrapper that adds chunked encoding metadata.

Something along these lines:

pub struct ResponseBody {
    data: Box<FnOnce(&Write) -> io::Result<()> + Send>,
    data_length: Option<usize>,
}