serialize_bits

Crates.ioserialize_bits
lib.rsserialize_bits
version0.1.0
sourcesrc
created_at2023-10-30 12:45:00.427233
updated_at2023-10-30 12:45:00.427233
descriptionSerialization/Deserialization in bits
homepage
repositoryhttps://github.com/gr3gdev/serialize-bits
max_upload_size
id1018444
size70,465
(gr3gdev)

documentation

README

Build & Tests Rust Docs

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)
    }
}
Commit count: 3

cargo fmt