pub fn ser_to_buf_asc_exact<T>(buf: &mut [u8], value: &T) -> ResultExpand description
Serialize value into pre-allocated, exact size byte buffer
Buffer is expected to be of exact size to hold serialized data. You can use calc_size()
to get exact size of required buffer before serialization.
In case of buffer underflow or buffer overflow, corresponding error is returned.
Example
#[derive(serde_derive::Serialize)]
struct Foo(u16, String);
let foo = Foo(1, "abc".to_string());
assert_eq!(calc_size_asc(&foo).unwrap(), 6);
let mut buf = [0_u8; 6]; // need buffer of exact size!
ser_to_buf_asc_exact(&mut buf, &foo).unwrap();
assert_eq!(&buf[2..5], b"abc");
assert_eq!(buf[5], 7); // last byte is string length (3) in varint encoding