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
DecodeError::UnexpectedEofif the buffer has fewer bytes than the declared length.DecodeError::MessageTooLargeif the declared length overflowsusize.