1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! `crdts` is a library of thoroughly-tested, serializable CRDT's
//! ported from the riak_dt library to rust.
#![crate_type = "lib"]
#![deny(missing_docs)]

pub use gcounter::GCounter;
pub use lwwreg::LWWReg;
pub use orswot::Orswot;
pub use pncounter::PNCounter;
pub use vclock::VClock;

/// `lwwreg` contains the last-write-wins register.
pub mod lwwreg;
/// `vclock` contains the vector clock.
pub mod vclock;
/// `orswot` contains the addition-biased or-set without tombstone.
pub mod orswot;
/// `gcounter` contains the grow-only counter
pub mod gcounter;
/// `pncounter` contains the positive-negative counter
pub mod pncounter;

#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate bincode;

use bincode::{Infinite, deserialize, serialize};
use serde::Serialize;
use serde::de::DeserializeOwned;

/// Dumps this type to binary.
///
/// # Examples
///
/// ```
/// use crdts::{Orswot, to_binary, from_binary};
/// let mut a = Orswot::new();
/// a.add(1, 1);
/// let encoded = to_binary(&a);
/// let decoded = from_binary(encoded).unwrap();
/// assert_eq!(a, decoded);
/// ```
pub fn to_binary<A: Serialize>(s: &A) -> Vec<u8> {
    serialize(s, Infinite).unwrap()
}

/// Attempts to reconstruct a type from binary.
///
/// # Examples
///
/// ```
/// use crdts::{Orswot, to_binary, from_binary};
/// let mut a = Orswot::new();
/// a.add(1, 1);
/// let encoded = to_binary(&a);
/// let decoded = from_binary(encoded).unwrap();
/// assert_eq!(a, decoded);
/// ```
pub fn from_binary<A: DeserializeOwned>(
    encoded: Vec<u8>,
) -> bincode::Result<A> {
    deserialize(&encoded[..])
}