-
Notifications
You must be signed in to change notification settings - Fork 331
0.5 release prevents going back from Bytes to BytesMut #350
Description
Is the (new) inability to roundtrip between Bytes and BytesMut intentional? The inability to do so somewhat diminishes the usefulness of the reference counting feature this crate offers, if it means that freeze() renders a chunk of memory permanently immutable with no way to reclaim borrowed ranges and start over again without reallocating.
I understand that Bytes is (now) unaware of the existence of BytesMut at this time, but it should still be possible to update the BytesMut api to recover memory whose ref count has reached zero. I was all set to patch BytesMut to add a version of freeze() that looked like this:
pub fn freeze() -> (Bytes, Unfreeze)where Unfreeze is a struct with a single function
pub fn unfreeze(self) -> BytesMutwith Unfreeze itself containing a weak reference to the same shared memory used by Bytes instances, but then I discovered that Bytes doesn't use Arc but rather a custom implementation that does not support weak references.
(The reason for having BytesMut return an Unfreeze rather than adding, e.g., an unfreeze() /try_mut() method to Bytes is that the latter would require Bytes keeping track of whether it was instantiated from read-only memory or not, which the current vtable mask isn't capable of distinguishing between.)
(My use case involves reading some bytes into a buffer, sending the ref-counted read bytes to another thread for processing, then reclaiming them when an event is set (and the Bytes have been dropped) to reuse the buffer for the next cycle.)