Skip to content

0.5 release prevents going back from Bytes to BytesMut #350

Open
@mqudsi

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) -> BytesMut

with 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.)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions