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

Need for BytesMut::reserve_exact #396

Open
mzabaluev opened this issue Jun 5, 2020 · 2 comments
Open

Need for BytesMut::reserve_exact #396

mzabaluev opened this issue Jun 5, 2020 · 2 comments

Comments

@mzabaluev
Copy link
Contributor

Some applications may want the capacity of BytesMut to be reported exactly as previously requested (unless more is already reserved). BytesMut::reserve allows for allocation excess and over-reserving in expectation for further expansion, which is probably good for the general case. Hence the proposal for another method.

For an example where this could save on external bookkeeping, see mzabaluev/chunked-bytes#1

@mzabaluev
Copy link
Contributor Author

let _ = buf.split_off(buf.capacity()); can be used to emulate this, but it involves creating a temporary tail BytesMut and always going atomic.

@mzabaluev
Copy link
Contributor Author

As I understand it, the stumbling block here is the Vec representation: Vec::reserve_exact does not actually guarantee the exact capacity, and the cap member of BytesMut is tied to the virtual Vec and so can't be fudged without breaking safety for Vec::from_raw_parts. So extra bookkeeping would be needed to implement this.

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

1 participant