formatting
This commit is contained in:
parent
ad98cfafb2
commit
c25c6edc6a
29 changed files with 886 additions and 571 deletions
|
|
@ -1,9 +1,11 @@
|
|||
use bincode;
|
||||
use bincode::{Decode, Encode};
|
||||
use bincode::config::{BigEndian, Configuration, Fixint};
|
||||
use bincode::{Decode, Encode};
|
||||
use std::mem::size_of;
|
||||
|
||||
const BIN_CONFIG: Configuration<BigEndian, Fixint> = bincode::config::standard().with_big_endian().with_fixed_int_encoding();
|
||||
const BIN_CONFIG: Configuration<BigEndian, Fixint> = bincode::config::standard()
|
||||
.with_big_endian()
|
||||
.with_fixed_int_encoding();
|
||||
|
||||
pub fn encode<T: Encode>(t: &T) -> Result<Vec<u8>, bincode::error::EncodeError> {
|
||||
bincode::encode_to_vec(t, BIN_CONFIG)
|
||||
|
|
@ -40,24 +42,27 @@ pub fn encode_sequence<T: Encode>(ts: &[T]) -> Result<Vec<u8>, bincode::error::E
|
|||
let mut result = vec![];
|
||||
for t in ts {
|
||||
result.append(&mut encode(&t)?);
|
||||
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn encode_sequence_with_sizes<T: Encode>(ts: &[T]) -> Result<(Vec<u8>, Vec<usize>), bincode::error::EncodeError> {
|
||||
pub fn encode_sequence_with_sizes<T: Encode>(
|
||||
ts: &[T],
|
||||
) -> Result<(Vec<u8>, Vec<usize>), 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<T: Decode>(len: usize, bytes: &[u8]) -> Result<Vec<T>, bincode::error::DecodeError> {
|
||||
pub fn decode_sequence<T: Decode>(
|
||||
len: usize,
|
||||
bytes: &[u8],
|
||||
) -> Result<Vec<T>, bincode::error::DecodeError> {
|
||||
let mut result: Vec<T> = Vec::with_capacity(len);
|
||||
let mut offset = 0;
|
||||
for _ in 0..len {
|
||||
|
|
@ -68,7 +73,6 @@ pub fn decode_sequence<T: Decode>(len: usize, bytes: &[u8]) -> Result<Vec<T>, bi
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue