• Stars
    star
    120
  • Rank 295,983 (Top 6 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Utility for comparing two structs and re-applying the differences to other structs

serde-diff

A small helper that can

  1. Serialize the fields that differ between two values of the same type
  2. Apply previously serialized field differences to other values of the same type.

The SerdeDiff trait impl can serialize field paths recursively, greatly reducing the amount of data that needs to be serialized when only a small part of a struct/enum has changed.

Build Status Crates.io Docs.rs

Usage

On a struct or enum:

#[derive(SerdeDiff, Serialize, Deserialize)]

Serialize & apply differences for various formats:

rmp_serde (MessagePack - very small messages)

let msgpack_data = rmp_serde::to_vec_named(&Diff::serializable(&old, &new))?;
let mut deserializer = rmp_serde::Deserializer::new(msgpack_data.as_slice());
Apply::apply(&mut deserializer, &mut target)?;

bincode (very fast serialize/deserialize)

let bincode_data = bincode::serialize(&Diff::serializable(&old, &new))?;
bincode::config().deserialize_seed(Apply::deserializable(&mut target), &bincode_data)?;

serde_json

        let json_data = serde_json::to_string(&Diff::serializable(&old, &new))?;
        let mut deserializer = serde_json::Deserializer::from_str(&json_data);
        Apply::apply(&mut deserializer, &mut target)?;

Built-in type support

  • Primitive types
  • std::time::{Duration, SystemTime}
  • IP addresses in std
  • Vec
  • HashMap (thanks @milkey-mouse)
  • BTreeMap (thanks @milkey-mouse)
  • Fixed-size arrays (thanks @Boscop)
  • Tuples (thanks @Boscop)

Simple example

Cargo.toml

[dependencies]
serde-diff = "0.3"
serde = "1"
serde_json = "1" # all serde formats are supported, serde_json is shown in this example

main.rs

use serde_diff::{Apply, Diff, SerdeDiff};
use serde::{Serialize, Deserialize};
#[derive(SerdeDiff, Serialize, Deserialize, PartialEq, Debug)]
struct TestStruct {
    a: u32,
    b: f64,
}

fn main() {
    let old = TestStruct {
        a: 5,
        b: 2.,
    };
    let new = TestStruct {
        a: 8, // Differs from old.a, will be serialized
        b: 2.,
    };
    let mut target = TestStruct {
        a: 0,
        b: 4.,
    };
    let json_data = serde_json::to_string(&Diff::serializable(&old, &new)).unwrap();
    let mut deserializer = serde_json::Deserializer::from_str(&json_data);
    Apply::apply(&mut deserializer, &mut target).unwrap();


    let result = TestStruct {
        a: 8,
        b: 4.,
    };
    assert_eq!(result, target);
}

Derive macro attributes

Opaque structs:

#[derive(SerdeDiff, Serialize, Deserialize, PartialEq)]
#[serde_diff(opaque)] // opaque structs are serialized as a unit and fields do not need to implement SerdeDiff
struct DoesNotRecurse {
    value: ExternalType, 
}

Opaque fields:

#[derive(SerdeDiff, Serialize, Deserialize, PartialEq)]
struct WrapperStruct {
    #[serde_diff(opaque)]
    value: ExternalType, // opaque fields only need to implement Serialize + Deserialize + PartialEq,
}

Skip fields:

#[derive(SerdeDiff, Serialize, Deserialize, PartialEq)]
struct WrapperStruct {
    #[serde_diff(skip)]
    value: ExternalType,
}

Generics:

#[derive(SerdeDiff, Serialize, Deserialize, PartialEq, Debug)]
struct GenericStruct<T>
where
    T: SerdeDiff,
{
    a: T,
}

Enums:

#[derive(SerdeDiff, Serialize, Deserialize, PartialEq, Debug)]
enum TestEnum {
    Structish { x: u32, y: u32 },
    Enumish(i32, i32, i32),
    Unitish,
}

Contribution

All contributions are assumed to be dual-licensed under MIT/Apache-2.

License

Distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT.

More Repositories

1

amethyst

Data-oriented and data-driven game engine written in Rust
Rust
7,982
star
2

specs

Specs - Parallel ECS
Rust
2,483
star
3

rlua

High level Lua bindings to Rust
C
1,628
star
4

legion

High performance Rust ECS library
Rust
1,616
star
5

bracket-lib

The Roguelike Toolkit (RLTK), implemented for Rust.
Rust
1,500
star
6

rustrogueliketutorial

Roguelike Tutorial in Rust - using RLTK
Rust
894
star
7

rendy

State of the art "build your own engine" kit powered by gfx-hal
Rust
815
star
8

distill

Asset pipeline system for game engines & editor suites.
Rust
367
star
9

shred

Shared resource dispatcher
Rust
231
star
10

evoli

An ecosystem-simulation game made with Amethyst
Rust
217
star
11

amethyst-starter-2d

Seed project for 2D games
Rust
201
star
12

space-menace

An action 2D platformer made with Amethyst game engine
Rust
180
star
13

shotcaller

A moddable RTS/MOBA game made with bracket-lib and minigene.
Rust
143
star
14

hibitset

Hierarchical bit set container
Rust
114
star
15

voxel-mapper

Make beautiful voxel maps.
Rust
113
star
16

specs-physics

nphysics integration for the Specs entity component system
Rust
94
star
17

sheep

Modular and lightweight spritesheet packer 🐑
Rust
89
star
18

tools

Game development tools for the Amethyst engine
Rust
80
star
19

grumpy_visitors

🧙‍♂️🧙‍♀️ A prototype of a top-down EvilInvasion-like 2D arcade/action (with co-op!)
Rust
77
star
20

amethyst-imgui

imgui integration for Amethyst
Rust
66
star
21

legion_transform

A Unity-inspired hierarchical transform implementation using Legion ECS
Rust
51
star
22

editor-core

Crate that allows an Amethyst game to communicate with an editor.
Rust
44
star
23

rfcs

RFCs are documents that contain major plans and decisions for the engine
32
star
24

dwarf_seeks_fortune

A 2D puzzle platformer made with the Amethyst game engine.
Rust
25
star
25

amethyst_iced

Iced rendering plugin for the Amethyst game engine
Rust
24
star
26

pong_wasm

WASM end-to-end proof of concept -- work in progress
Rust
22
star
27

web_worker

Rust
19
star
28

ludumdare42

A game made by the Amethyst team for Ludum Dare 42
Rust
16
star
29

website-legacy

Project website and blog (DEPRECATED)
HTML
11
star
30

website

Official Amethyst website
JavaScript
9
star
31

amethyst-rhusics

A bridge between Amethyst and rhusics (unmaintained)
Rust
9
star
32

awesome-specs

A curated list of projects that use or help with using Specs.
6
star
33

crystal-editor

Svelte
6
star
34

ludumdare43

Rust
2
star
35

resources

Files that are important to keep around but not tied to any specific code base
1
star
36

builder

The docker container used in amethyst's CI/CD infrastructure.
Dockerfile
1
star
37

wasm_rush_report

Report about adding WASM support to the Amethyst game engine.
1
star
38

laminar-ffi

Crate that exposes Laminar functionality to C
Rust
1
star
39

amethyst-docs-builder

Webhook server that builds amethyst's book and documentation
Go
1
star
40

amethystup

Setup script for Amethyst dependencies
Shell
1
star
41

documentation

Non-rustdoc documentation and policies
1
star