Function encode

Source
pub fn encode<B: Buffer>(
    iter: impl IntoIterator<Item = impl Borrow<u8>>,
) -> Result<B, OutOfMemory>
Expand description

Takes a slice of bytes as input and returns a buffer containing the encoded message.

Returns Err(()) when the buffer can’t be grown to hold the entire output.

§Examples

// example data
let bytes = [0x12, 0x34, 0x56, 0x78];
let expected = [0x1b, 0x1b, 0x1b, 0x1b, 0x01, 0x01, 0x01, 0x01, 0x12, 0x34, 0x56, 0x78, 0x1b, 0x1b, 0x1b, 0x1b, 0x1a, 0x00, 0xb8, 0x7b];

§Using alloc::Vec

let encoded = encode::<Vec<u8>>(&bytes);
assert!(encoded.is_ok());
assert_eq!(encoded.unwrap().as_slice(), &expected);

§Using ArrayBuf

let encoded = encode::<ArrayBuf<20>>(&bytes);
assert!(encoded.is_ok());
assert_eq!(&*encoded.unwrap(), &expected);

// encoding returns `Err(())` if the encoded message does not fit into the vector
let encoded = encode::<ArrayBuf<19>>(&bytes);
assert_eq!(encoded, Err(OutOfMemory));