use bincode::config::{BigEndian, Configuration, Fixint}; use bincode::{Decode, Encode}; const BIN_CONFIG: Configuration = bincode::config::standard() .with_big_endian() .with_fixed_int_encoding(); pub fn encode(t: &T) -> Result, bincode::error::EncodeError> { bincode::encode_to_vec(t, BIN_CONFIG) } pub fn decode(bytes: &[u8]) -> Result<(T, usize), bincode::error::DecodeError> { bincode::decode_from_slice(bytes, BIN_CONFIG) } // We don't care about encoding the length here (since it will be used for a row with known column // size) pub fn encode_sequence(ts: &[T]) -> Result, bincode::error::EncodeError> { let mut result = vec![]; for t in ts { result.append(&mut encode(&t)?); } Ok(result) } pub fn encode_sequence_with_sizes( ts: &[T], ) -> Result<(Vec, Vec), bincode::error::EncodeError> { let mut result_bytes = vec![]; let mut sizes = Vec::with_capacity(ts.len()); for t in ts { let mut bytes = encode(&t)?; sizes.push(bytes.len()); result_bytes.append(&mut bytes); } Ok((result_bytes, sizes)) } pub fn decode_sequence( len: usize, bytes: &[u8], ) -> Result, bincode::error::DecodeError> { let mut result: Vec = Vec::with_capacity(len); let mut offset = 0; for _ in 0..len { let (x, bytes_consumed) = decode::(&bytes[offset..])?; offset += bytes_consumed; result.push(x); } Ok(result) }