karyon_core/util/encode.rs
1use bincode::Encode;
2
3use crate::{Error, Result};
4
5/// Encode the given type `T` into a `Vec<u8>`.
6pub fn encode<T: Encode>(src: &T) -> Result<Vec<u8>> {
7 let vec = bincode::encode_to_vec(src, bincode::config::standard().with_fixed_int_encoding())?;
8 Ok(vec)
9}
10
11/// Encode the given type `T` into the given slice..
12pub fn encode_into_slice<T: Encode>(src: &T, dst: &mut [u8]) -> Result<usize> {
13 bincode::encode_into_slice(
14 src,
15 dst,
16 bincode::config::standard().with_fixed_int_encoding(),
17 )
18 .map_err(Error::from)
19}