Function postcard::to_slice_cobs

source ·
pub fn to_slice_cobs<'a, 'b, T>(
    value: &'b T,
    buf: &'a mut [u8],
) -> Result<&'a mut [u8]>
where T: Serialize + ?Sized,
Expand description

Serialize a T to the given slice, with the resulting slice containing data in a serialized then COBS encoded format. The terminating sentinel 0x00 byte is included in the output buffer.

When successful, this function returns the slice containing the serialized and encoded message.

§Example

use postcard::to_slice_cobs;
let mut buf = [0u8; 32];

let used = to_slice_cobs(&false, &mut buf).unwrap();
assert_eq!(used, &[0x01, 0x01, 0x00]);

let used = to_slice_cobs("1", &mut buf).unwrap();
assert_eq!(used, &[0x03, 0x01, b'1', 0x00]);

let used = to_slice_cobs("Hi!", &mut buf).unwrap();
assert_eq!(used, &[0x05, 0x03, b'H', b'i', b'!', 0x00]);

let data: &[u8] = &[0x01u8, 0x00, 0x20, 0x30];
let used = to_slice_cobs(data, &mut buf).unwrap();
assert_eq!(used, &[0x03, 0x04, 0x01, 0x03, 0x20, 0x30, 0x00]);