-
Notifications
You must be signed in to change notification settings - Fork 288
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
Bytes::make_mut #611
Comments
Returning the original buffer upon failure allows defining a fallback path which is much more flexible. Mirroring https://doc.rust-lang.org/stable/std/sync/struct.Arc.html#method.try_unwrap sounds the best to me as |
It's not super simple to do this, because as far as I recall, |
It probably has to work by cases on specific vtables. |
There is From for Vec, and a private Something like this: if self.is_unique() {
Ok(BytesMut::from_vec(Vec::from(self)))
} else {
Err(self)
} https://docs.rs/bytes/latest/src/bytes/bytes_mut.rs.html#831 |
This looks like a great approach; would you be willing to make it into a PR? |
Can we also pack into this PR the equivalent of Because its nice to be able to try it, but you are kind of stuck if you actually want to have a mutable object and you dont care if it makes a copy. |
I made a zero-copy (if possible) version to convert directly to mut without going through a |
Would it be possible to add a make_mut method on Bytes that converts to BytesMut in place if possible? For example if the Bytes was created from a Vec and has only one reference, then it could turn that Vec into a BytesMut and give that to you in place.
If it can't convert it could either return Option or just make a copy. I see this library has an emphasis on avoiding implicit slow operations (good!) so the Option approach might be better.
The text was updated successfully, but these errors were encountered: