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().
ByteArray.toByteString()always copies the array to preserve ByteString'simmutability 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
Proposed API
Similar to Java's
ByteBuffer.wrap(). The method name makes theownership transfer explicit and distinguishes it from the safe
toByteString().