aboutsummaryrefslogtreecommitdiff
path: root/core/src/util/encode.rs
blob: bf6367107bc990593373df3cc49925e043d14c24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
use bincode::Encode;

use crate::{Error, Result};

/// Encode the given type `T` into a `Vec<u8>`.
pub fn encode<T: Encode>(src: &T) -> Result<Vec<u8>> {
    let vec = bincode::encode_to_vec(src, bincode::config::standard())?;
    Ok(vec)
}

/// Encode the given type `T` into the given slice..
pub fn encode_into_slice<T: Encode>(src: &T, dst: &mut [u8]) -> Result<usize> {
    bincode::encode_into_slice(src, dst, bincode::config::standard()).map_err(Error::from)
}