Skip to main content

decode_bytes_to_bytes

Function decode_bytes_to_bytes 

Source
pub fn decode_bytes_to_bytes(buf: &mut impl Buf) -> Result<Bytes, DecodeError>
Expand description

Decode a bytes value into a Bytes: read a varint length prefix, then take that many bytes via Buf::copy_to_bytes.

When buf is itself backed by Bytes, copy_to_bytes is a zero-copy refcount bump (via split_to); for &[u8] and other inputs it falls back to an allocation + copy, equivalent to wrapping decode_bytes in Bytes::from.

§Memory retention

In the zero-copy case the returned Bytes aliases the source allocation, so the entire source buffer is retained until every aliased Bytes drops. To detach a field from a large source buffer, deep-copy it with Bytes::copy_from_slice(&field).

§Errors