bserde 0.1.2

Library for serializing data to binary.
Documentation
  • Coverage
  • 17.07%
    7 out of 41 items documented0 out of 29 items with examples
  • Size
  • Source code size: 13.17 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.16 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 17s Average build duration of successful builds.
  • all releases: 17s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • RealViper8

bserde

Crate for doing binary serialization

Installation

Install the crate as a dependency in your apps's Cargo.toml file like so:

[dependencies]

bserde = "0.1.1"

Usage

Import BSerializer, BDeserialize and BSerializer then you should be got to go.

And also what you want to serialize:

use bserde::{BDeserialize, BSerialize, BSerializer};

Define a struct to store your config files. You must implement the BSerialize and the BDeserialize trait for your own types

If you dont need much control you can use the derive macro: bserde_derive

For Example

use bserde::{BDeserialize, BSerialize, BSerializer};
use bserde::serializer::BinarySerializer;
use std::{
  fs,
  io::{BufReader, Read},
};

#[derive(Debug)]
struct Person {
  name: String,
  age: u8,
}

impl BSerialize<'_> for Person {
  fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
  where
    S: _serde::BSerializer,
  {
    let mut s = serializer.serialize_struct("Person", 2)?;
    s.serialize_field("name", &self.name)?;
    s.end()
  }
}

impl BDeserialize for Person {
  type Error = std::io::Error;
  fn deserialize(input: &mut &[u8]) -> Result<Self, Self::Error> {
    Ok(Self {
      name: String::deserialize(input)?,
    })
  }
}

fn main() {
  // SERIALIZING

  // Creating the output file
  let out = fs::File::create("test.bin").unwrap();

  // Creating the serializer
  let mut s = BinarySerializer::new();

  // Creating the person
  let p = Person {
    name: String::from("Hello World"),
    age: 34,
  };

  p.serialize(&mut s);
  // Outputting the file
  s.save(out).unwrap();

  // DESERIALIZING

  // Creating the reader
  let mut new_f = BufReader::new(fs::File::open("test.bin").unwrap());

  // Creating the buffer to store the bytes.
  let mut buf = Vec::new();
  new_f.read_to_end(&mut buf).unwrap();

  // Deserializing the struct
  let p = Person::deserialize(&mut buf.as_slice()).unwrap();

  dbg!(&p);
}