[][src]Function pigeon::pack_buffer

pub fn pack_buffer(data: impl Pack, buffer: &mut [u8]) -> Result<usize>

Pack the type to a buffer directly.

This is a short-hand for creating a Writer then calling write on it.

Position

This advances the writer only if all the underlying writes succeed.

On failure, there may be incomplete data written to the buffer, the writer's position will not include that data.

Returns

On success, this returns an Ok with the amount of bytes written. If there is not enough space to write all the data, this returns Err(Error::UnexpectedEof).

Examples

let mut buf = [0; 6];

let written = pigeon::pack_buffer(&12345678u32, &mut buf[..]).unwrap();

assert_eq!(written, 4);
assert_eq!(&buf, &[0, 188, 97, 78, 0, 0]);
assert_eq!(&buf[0..written], &[0, 188, 97, 78]);