amebazii::types

Function to_bytes_with_capacity

Source
pub fn to_bytes_with_capacity<T>(obj: &T) -> Result<Vec<u8>, Error>
where T: ToStream + BinarySize,
Expand description

Serializes an object into a vector of bytes with an optimized capacity.

This function serializes the object (obj) into a Vec<u8>, ensuring that the vector is allocated with the minimum required capacity to hold the serialized data. The object must implement both ToStream and BinarySize to allow calculating the exact binary size beforehand.

§Parameters

  • obj: A reference to the object to be serialized.

§Returns

  • Ok(Vec<u8>): A vector of bytes with the minimum required capacity for the serialized object.
  • Err(Error): If an error occurs while writing the object to the byte vector.