Skip to content
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

BufReader: appending more data to buffer #57000

Closed
kex103 opened this issue Dec 20, 2018 · 5 comments
Closed

BufReader: appending more data to buffer #57000

kex103 opened this issue Dec 20, 2018 · 5 comments

Comments

@kex103
Copy link

kex103 commented Dec 20, 2018

Unless I'm missing something, after a fill_buf call, there isn't a way for me to request the BufReader to read again, appending the results to its internal buffer.

Would it be feasible to add a function like "append_buff" to the BufRead trait? Or to BufReader directly? This would call self.inner.read, passing to it the subslice of the remaining internal buffer self.buf.

The motivation for this is to read dynamically sized requests that don't have a message delimiter.
In my case, by parsing through the request, I can determine if the request is complete or not, but I would not be able to know the full size of the message beforehand.

@sfackler
Copy link
Member

BufReader has a fixed internal size - you'll need to collect into a separate buffer if you want to be able to process messages of an arbitrary length.

@kex103
Copy link
Author

kex103 commented Dec 20, 2018

Thanks for the response!

Yes, this proposed function won't help messages that are larger than the buffer size.

However, correct me if I'm wrong, but fill_buf doesn't necessarily fill up the buffer, despite the name. It just makes one read call, which needs to be consumed before it will read again

So my first fill_buf call will populate some of the buffer, but the rest of the request is not ready yet. After parsing through the partially-filled buffer, I want to be able to have the BufReader make another read call, and then parse through the combined message. In my case, the messages are much smaller than the internal buffer size, but they are still being fragmented. For context, the BufReader I'm using is wrapping a mio TcpStream, which I believe is a wrapper around a TcpStream set to non-blocking.

@sfackler
Copy link
Member

BufReader it not really designed to be a universal solution to all read-to-buffer use cases. It's specifically designed to minimize the cost of repeated small reads.

@abonander
Copy link
Contributor

@kex103 I created buf_redux as a more flexible alternative to the std::io buffered types. Take a look and let me know what you think.

@kex103
Copy link
Author

kex103 commented Dec 21, 2018

@abonander Thanks, I'll check it out!

Closing this issue. I realize there are considerations with just adding an "append_buff" function, as now I'm getting clipped messages if I hit the end of the buffer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants