-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved compatibility to the /compatibility/ crate, made bincode-derive…
… support `#[bincode(crate = "bincode_2")]`
- Loading branch information
1 parent
3cb6061
commit 077fc7f
Showing
10 changed files
with
340 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "bincode_compatibility" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
bincode_2 = { path = "..", package = "bincode" } | ||
bincode_1 = { version = "1", package = "bincode" } | ||
serde = { version = "1", features = ["derive"] } | ||
rand = "0.8" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#![cfg(test)] | ||
|
||
use bincode_1::Options; | ||
|
||
mod rand; | ||
mod sway; | ||
|
||
pub fn test_same_with_config<T, C, O>(t: &T, bincode_1_options: O, bincode_2_config: C) | ||
where | ||
T: bincode_2::Encode + serde::Serialize + core::fmt::Debug, | ||
C: bincode_2::config::Config + Clone, | ||
O: bincode_1::Options + Clone, | ||
{ | ||
let bincode_1_output = bincode_1_options.serialize(t).unwrap(); | ||
let bincode_2_output = bincode_2::encode_to_vec(t, bincode_2_config).unwrap(); | ||
|
||
assert_eq!( | ||
bincode_1_output, bincode_2_output, | ||
"{:?} serializes differently", | ||
t | ||
); | ||
} | ||
|
||
pub fn test_same<T>(t: T) | ||
where | ||
T: bincode_2::Encode + serde::Serialize + core::fmt::Debug, | ||
{ | ||
test_same_with_config( | ||
&t, | ||
bincode_1::options() | ||
.with_big_endian() | ||
.with_varint_encoding(), | ||
bincode_2::config::legacy() | ||
.with_big_endian() | ||
.with_variable_int_encoding(), | ||
); | ||
test_same_with_config( | ||
&t, | ||
bincode_1::options() | ||
.with_little_endian() | ||
.with_varint_encoding(), | ||
bincode_2::config::legacy() | ||
.with_little_endian() | ||
.with_variable_int_encoding(), | ||
); | ||
test_same_with_config( | ||
&t, | ||
bincode_1::options() | ||
.with_big_endian() | ||
.with_fixint_encoding(), | ||
bincode_2::config::legacy() | ||
.with_big_endian() | ||
.with_fixed_int_encoding(), | ||
); | ||
test_same_with_config( | ||
&t, | ||
bincode_1::options() | ||
.with_little_endian() | ||
.with_fixint_encoding(), | ||
bincode_2::config::legacy() | ||
.with_little_endian() | ||
.with_fixed_int_encoding(), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Simplified case, taken from: | ||
// https://github.com/rust-random/rand/blob/19404d68764ed08513131f82157e2ccad69dcf83/rand_pcg/src/pcg64.rs#L37-L40 | ||
|
||
use rand::Rng; | ||
|
||
#[derive(Debug, bincode_2::Encode, bincode_2::Decode, serde::Serialize, serde::Deserialize)] | ||
#[bincode(crate = "bincode_2")] | ||
pub struct Lcg64Xsh32 { | ||
state: u64, | ||
increment: u64, | ||
} | ||
|
||
#[test] | ||
pub fn test() { | ||
let mut rng = rand::thread_rng(); | ||
for _ in 0..100 { | ||
crate::test_same(Lcg64Xsh32 { | ||
state: rng.gen(), | ||
increment: rng.gen(), | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Credits to Sway in the Rust Programming Language | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(bincode_2::Encode, bincode_2::Decode, serde::Serialize, serde::Deserialize, Debug)] | ||
#[bincode(crate = "bincode_2")] | ||
pub struct FTXresponseSuccess<T> { | ||
pub success: bool, | ||
pub result: T, | ||
} | ||
|
||
#[derive(bincode_2::Encode, bincode_2::Decode, Serialize, Deserialize, Debug)] | ||
#[bincode(crate = "bincode_2")] | ||
pub struct FTXresponseFailure { | ||
pub success: bool, | ||
pub error: String, | ||
} | ||
|
||
#[derive(bincode_2::Encode, bincode_2::Decode, Serialize, Deserialize, Debug)] | ||
#[bincode(crate = "bincode_2")] | ||
#[serde(untagged)] | ||
pub enum FTXresponse<T> { | ||
Result(FTXresponseSuccess<T>), | ||
Error(FTXresponseFailure), | ||
} | ||
|
||
#[derive(bincode_2::Encode, bincode_2::Decode, Serialize, Deserialize, Debug)] | ||
#[bincode(crate = "bincode_2")] | ||
pub enum TradeSide { | ||
Buy, | ||
Sell, | ||
} | ||
|
||
#[derive(bincode_2::Encode, bincode_2::Decode, Serialize, Deserialize, Debug)] | ||
#[bincode(crate = "bincode_2")] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Trade { | ||
pub id: u64, | ||
pub liquidation: bool, | ||
pub price: f64, | ||
pub side: TradeSide, | ||
pub size: f64, | ||
pub time: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.