serial_traits
A trait that allows you to serialize to and parse from Vec<u8> buffers.
This is great for sending them across for example WS or TCP sockets.
Comes with implementations for primitive types, String and generic collection types, so long the type arguments implement the trait too.
The buffer is no more larger than what you serialized, if you serialize a struct with two f32s, the final buffer will be 8 bytes large.
(PS: Variable-sized collections use an usize to keep track of their size, and _sizes use LEB128.)
Using types that implement the trait:
// this is the trait
use Serializable;
// serializing a type that implements it
let mut buffer: = 6754_u16.serialize;
// deserializing a type that implements it
match u16parse
// the parse function consumes the buffer
assert_eq;
Implementing the trait for your own structs:
Unless you need very specific trait implementations, serial_traits_derive will probably cover all of your use cases by auto-generating implementations for the structs and enums you have defined.
Types with Serializable implemented:
f32,f64u8,u16,u32,u64,u128i8,i16,i32,i64,i128usize,isizechar,bool[T; N]StringVec<T>,VecDeque<T>,LinkedList<T>HashMap<K, V>,BTreeMap<K, V>HashSet<T>,BTreeSet<T>BinaryHeap<T>- and any tuples
(T₁, T₂, …, Tₙ)up to length 12
T, K, and V must additionally implement Serializable.
The trait implementations for usize and isize directly call the two LEB128 functions that come with this crate.
If needed, you can also use the leb128 module of this crate for your own needs:
use ;
// a small number stored in a larger integer data type
let value = 8_i128;
let mut buffer = serialize;
println!;
let parsed = ;
assert_eq;
assert_eq;
Both LEB128 functions work with all primitive integer types.
Can't forget, big thanks to GDenisC for helping a lot with design!