Skip to content

Add ByteString.wrap(ByteArray) for zero-copy creation when ownership is transferred #1789

@MohammedKHC

Description

@MohammedKHC

ByteArray.toByteString() always copies the array to preserve ByteString's
immutability guarantee. However, there are cases where the caller creates a
ByteArray solely to wrap it in a ByteString and never retains a reference to
it afterwards — making the copy unnecessary.

Example

// KeyFactory returns a new ByteArray — copy in toByteString() is wasteful!
val encoded = key.encoded.toByteString()

Proposed API

// Caller transfers ownership — no copy needed
val bs = ByteString.wrap(key.encoded)

Similar to Java's ByteBuffer.wrap(). The method name makes the
ownership transfer explicit and distinguishes it from the safe toByteString().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions