-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow streaming responses of types that do not implement io::Read #813
Comments
I'm in full agreement that this isn't the ideal interface, and while I also agree that there are workable solutions to the problem, it is unlikely that any which modify I'd be happy to merge code that provides an adapter that implements |
Well, if the interface cannot change, then I wonder whether it is really relevant to provide an inefficient |
Here is an example of such an adapter using the pipe crate : /// Starts the given function in a thread and return a reader of the generated data
pub fn piper<F: Send + 'static>(writefn: F) -> PipeReader
where F: FnOnce(PipeWriter) -> Result<()> {
let (reader, writer) = pipe();
thread::spawn(move || writefn(writer));
reader
} |
This reverts commit 2af7c0b
It can absolutely be an external crate. The only reason to upstream it into Rocket is to give it greater visibility. |
As this can be done entirely externally, and as we'll eventually resolve this when we migrate to async, I'm closing this out. |
Well, this cannot be done entirely externally without some ugly glue code, and a performance penalty. @SergioBenitez : Why not leaving this issue open until this is implemented (as part of the migration to async or otherwise) ? This would allow people (including me 😃 ) to track this issue specifically. |
Hello ! Now that the work on async is closer to being released, what is the story about this ? Will it be possible to use libraries that generate data by taking a |
Currently, the only way to stream data from the server to the client is to have a type implementing
io::Read
.This is quite a constraint, because of the type signature of the
read
method : the object has to be able to write a given amount of data to a given buffer, save its state, and then resume writing on the next call toread
. Most libraries that allow the generation of data in a streaming fashion do not support that. Instead of implementingRead
, they implement an object that takes a writer in the form of a genericWrite
argument, and write their result directly into that object.Proposition
Writable
with a single method:Read
values :Stream
andResponse
to accept arguments of typeWritable
instead ofRead
Use-cases
#499 Is a good example of why streaming values that do not implement
Read
is a good idea.An example is the handlebars library. Its
Handlebar
struct has arender_to_write
method that can stream a template into aWrite
object.Another example is serde and its
Serializer
that also takes aWrite
object.The text was updated successfully, but these errors were encountered: