Serialize `value` into a versioned, magic-tagged byte buffer.
(
magic: &[u8; MAGIC_LEN],
version: u8,
value: &T,
)
| 19 | |
| 20 | /// Serialize `value` into a versioned, magic-tagged byte buffer. |
| 21 | pub fn encode<T: ToMessagePack>( |
| 22 | magic: &[u8; MAGIC_LEN], |
| 23 | version: u8, |
| 24 | value: &T, |
| 25 | ) -> Result<Vec<u8>, CodecError> { |
| 26 | let body = zerompk::to_msgpack_vec(value).map_err(|e| CodecError::Corrupt { |
| 27 | detail: e.to_string(), |
| 28 | })?; |
| 29 | let mut out = Vec::with_capacity(HEADER_LEN + body.len()); |
| 30 | out.extend_from_slice(magic); |
| 31 | out.push(version); |
| 32 | out.extend_from_slice(&body); |
| 33 | Ok(out) |
| 34 | } |
| 35 | |
| 36 | /// Validate the envelope header and deserialize the body into `T`. |
| 37 | /// |