1 unstable release
0.1.0 | Oct 30, 2023 |
---|
#2350 in Encoding
Used in 2 crates
22KB
665 lines
serialize-bits
Rust serializer/deserializer : Struct to bits, bits to Struct.
The library already implements the traits for :
- u8, u16, u32, u64, u128, usize
- i8, i16, i32, i64, i128, isize
- char
- bool
- String
- Option
- SocketAddr
- Vec, VecDeque, LinkedList
- HashSet, BTreeSet
- BinaryHeap
- HashMap<K, V>, BTreeMap<K, V>
Serialization
Implement the SerializerData trait.
Example :
impl SerializerData for String {
fn to_data(&self) -> Vec<u8> {
let mut res = Vec::new();
res.append(&mut self.len().to_data());
res.append(&mut self.as_bytes().to_vec());
res
}
}
Deserialization
Implement the DeserializerData trait.
Example :
impl DeserializerData for String {
fn from_data(data: &Vec<u8>, index: usize) -> (Self, usize)
where
Self: Sized,
{
let (size, index) = usize::from_data(data, index);
let list = sub("String", data, index, size);
(String::from_utf8(list).unwrap(), index + size)
}
}