write_to_spare_capacity

Function write_to_spare_capacity 

pub fn write_to_spare_capacity(
    buf: &mut Vec<u8>,
    f: impl FnOnce(&mut Writer<'_>) -> Result<(), EncodeError>,
) -> Result<(), EncodeError>
Available on crate feature alloc only.
Expand description

Convenience function to write to the spare capacity of a Vec<u8>.

The closure f is given a Writer over the spare capacity of the vector, and can write data into it. After f returns, the vector’s length is updated to include the newly written data.

This function never grows the buffer. It’s the caller’s responsibility to ensure that the spare capacity is sufficient.

§Errors

Returns an EncodeError if and only if f returns an error. Even in case of an error, the vector’s length is still updated to include any bytes that were written.