Skip to content

Add Bytes::{from_raw_parts,to_raw_parts} methods #800

@jbms

Description

@jbms

It would be nice if Bytes could be decomposed into its "raw parts" in order to reduce the size overhead of types that wish to support using Bytes as the buffer but already store a base pointer and length information separately, e.g. a multi-dimensional array backed by a Bytes buffer.

To support this, there could be:

impl Bytes {
  pub fn to_raw_parts(self) -> (*const u8, usize, BytesOwner) { ... }
  pub unsafe fn from_raw_parts(ptr: *const u8, len: usize, owner: BytesOwner) -> Bytes { ... }
}

where BytesOwner includes the data and vtable information.

Users would be required to call from_raw_parts with exactly the values returned from to_raw_parts in order to free the memory. Dropping BytesOwner would leak the memory.

If the maintainers are open to this, I can create a PR.

Metadata

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