Serialize/DeSerialize for Rust built-in types and user defined types (complex struct types)

Overview

serialize-rs

Use serialize-rs as a crate

add dependencies to Cargo.toml

[dependencies]
serialize-rs = { git = "https://github.com/mutalisk999/serialize-rs.git"}

Example for Serialize/DeSerialize User Define Type (Complex Struct Type)

extern crate serialize_rs;

use std::io::{BufRead, Write, BufWriter, Cursor};
use std::error::Error;
use serialize_rs::{Serialize, DeSerialize};

#[derive(Debug)]
struct Xxxx
{
    a: i32,
    b: String,
    c: Option
   
    
}

impl Xxxx {
    fn new() -> Xxxx {
        Xxxx {
            a: 0i32,
            b: String::new(),
            c: Some(0.0f32)
        }
    }
}

impl Serialize for Xxxx {
    fn serialize(&self, w: &mut dyn Write) -> Result<(), Box
    
     > {
        self.a.serialize(w)?;
        self.b.serialize(w)?;
        self.c.serialize(w)?;
        Ok(())
    }
}

impl DeSerialize for Xxxx {
    fn deserialize(&mut self, r: &mut dyn BufRead) -> Result<(), Box
     
      > {
        self.a.deserialize(r)?;
        self.b.deserialize(r)?;
        self.c.deserialize(r)?;
        Ok(())
    }
}

fn main() {
    let mut x = Xxxx::new();
    x.a = 100;
    x.b = String::from("hello world");
    x.c = Some(0.123456f32);
    let mut buf = BufWriter::new(Vec::new());
    let _ = x.serialize(&mut buf);

    let mut buf = Cursor::new(buf.buffer());
    let mut val: Xxxx = Xxxx::new();
    println!("{:?}", val);
    let _ = val.deserialize(&mut buf);
    println!("{:?}", val);
}


     
    
   

output print

Xxxx { a: 0, b: "", c: Some(0.0) }
Xxxx { a: 100, b: "hello world", c: Some(0.123456) }

or simply

extern crate serialize_rs;

use std::io::{BufRead, Write, BufWriter, Cursor};
use std::error::Error;
use serialize_rs::{Serialize, DeSerialize, serialize_struct, deserialize_struct};

#[derive(Debug)]
struct Xxxx
{
    a: i32,
    b: String,
    c: Option
   
    
}

impl Xxxx {
    fn new() -> Xxxx {
        Xxxx {
            a: 0i32,
            b: String::new(),
            c: Some(0.0f32)
        }
    }
}

serialize_struct!(Xxxx, a, b, c);
deserialize_struct!(Xxxx, a, b, c);

fn main() {
    let mut x = Xxxx::new();
    x.a = 100;
    x.b = String::from("hello world");
    x.c = Some(0.123456f32);
    let mut buf = BufWriter::new(Vec::new());
    let _ = x.serialize(&mut buf);

    let mut buf = Cursor::new(buf.buffer());
    let mut val: Xxxx = Xxxx::new();
    println!("{:?}", val);
    let _ = val.deserialize(&mut buf);
    println!("{:?}", val);
}

   

output print

Xxxx { a: 0, b: "", c: Some(0.0) }
Xxxx { a: 100, b: "hello world", c: Some(0.123456) }
You might also like...
rust_aads - Rust Algorithms And Data Structures

rust_aads - Rust Algorithms And Data Structures rust_aads is an open repository with algorithms and data structures, used in computer science and comp

Extra iterator adaptors, iterator methods, free functions, and macros.

Itertools Extra iterator adaptors, functions and macros. Please read the API documentation here How to use with cargo: [dependencies] itertools = "0.1

Obake is a procedural macro for declaring and maintaining versioned data-structures.

Obake is a procedural macro for declaring and maintaining versioned data-structures. The name 'obake' is taken from the Japanese 'お化け (おばけ)', a class of supernatural beings in Japanese folklore that shapeshift.

Broot - A new way to see and navigate directory trees
Broot - A new way to see and navigate directory trees

Broot A better way to navigate directories Installation Instructions Get an overview of a directory, even a big one br -s Notice the unlisted? That's

A hashmap implementation, which uses hashset, and keys are contained within values.

A hashmap implementation, which uses hashset, and keys are contained within values.

Fast, efficient, and robust memory reclamation for concurrent data structures

Seize Fast, efficient, and robust memory reclamation for concurrent data structures. Introduction Concurrent data structures are faced with the proble

A scalable message queue powered by a segmented, partitioned, replicated and immutable log.
A scalable message queue powered by a segmented, partitioned, replicated and immutable log.

A scalable message queue powered by a segmented, partitioned, replicated and immutable log. This is currently a work in progress. laminarmq is intende

Ternary search tree collection in rust

tst Ternary search tree collection in rust with similar API to std::collections as it possible. Ternary search tree is a type of trie (sometimes calle

A priority queue for Rust with efficient change function.

PriorityQueue This crate implements a Priority Queue with a function to change the priority of an object. Priority and items are stored in an IndexMap

Releases(v1.0.1)
Owner
null
Generic array types in Rust

generic-array This crate implements generic array types for Rust. Requires minumum Rust version of 1.36.0, or 1.41.0 for From<[T; N]> implementations

Bartłomiej Kamiński 325 Dec 25, 2022
Rust crate to extend io::Read & io::Write types with progress callbacks

progress-streams Rust crate to provide progress callbacks for types which implement io::Read or io::Write. Examples Reader extern crate progress_strea

Pop!_OS 19 Dec 3, 2022
Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.

Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.

null 6.5k Dec 31, 2022
Coding-challenge - Algorithms and Data-structures, problems and solutions in Rust language using cargo-workspaces

Coding Challenge LeetCode/Hackerrank e.t.c Using this as an opportunity to improve my knowledge of rust lang If you found this repo useful to you, add

Tolumide Shopein 17 Apr 24, 2022
Rust-algorithm-club - Learn algorithms and data structures with Rust

Rust Algorithm Club ?? ?? This repo is under construction. Most materials are written in Chinese. Check it out here if you are able to read Chinese. W

Weihang Lo 360 Dec 28, 2022
K-dimensional tree in Rust for fast geospatial indexing and lookup

kdtree K-dimensional tree in Rust for fast geospatial indexing and nearest neighbors lookup Crate Documentation Usage Benchmark License Usage Add kdtr

Rui Hu 152 Jan 2, 2023
Algorithms and Data Structures of all kinds written in Rust.

Classic Algorithms in Rust This repo contains the implementation of various classic algorithms for educational purposes in Rust. Right now, it is in i

Alexander González 49 Dec 14, 2022
A simple rust library to help create octrees and quadtrees for chunked level of detail

LodTree LodTree, a simple tree data structure for doing chunk-based level of detail. Goals The aim of this crate is to provide a generic, easy to use

Dimev 14 Dec 29, 2022
Common data structures and algorithms in Rust

Contest Algorithms in Rust A collection of classic data structures and algorithms, emphasizing usability, beauty and clarity over full generality. As

Aram Ebtekar 3.3k Dec 27, 2022
Rust data structures and client for the PubChem REST API

pubchem.rs Rust data structures and client for the PubChem REST API. ?? Usage ?? Compound Create a Compound to query the PubChem API for a single comp

Martin Larralde 2 Jan 18, 2022