Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bincode 1 compatibility framework #489

Merged
merged 7 commits into from
Feb 1, 2022
Prev Previous commit
Next Next commit
Added decode/deserialize test to test_same
VictorKoenders committed Feb 1, 2022

Unverified

This user has not yet uploaded their public signing key.
commit 82ab6e309b395b5871d269b39e1eb5f0be3e7e60
42 changes: 38 additions & 4 deletions compatibility/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#![cfg(test)]

use ::rand::Rng;
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,
T: bincode_2::Encode
+ bincode_2::Decode
+ serde::Serialize
+ serde::de::DeserializeOwned
+ core::fmt::Debug
+ PartialEq,
C: bincode_2::config::Config,
O: bincode_1::Options + Copy,
{
let bincode_1_output = bincode_1_options.serialize(t).unwrap();
let bincode_2_output = bincode_2::encode_to_vec(t, bincode_2_config).unwrap();
@@ -19,11 +25,30 @@ where
"{:?} serializes differently",
t
);

let decoded: T = bincode_1_options.deserialize(&bincode_1_output).unwrap();
assert_eq!(&decoded, t);
let decoded: T = bincode_1_options.deserialize(&bincode_2_output).unwrap();
assert_eq!(&decoded, t);

let decoded: T = bincode_2::decode_from_slice(&bincode_1_output, bincode_2_config)
.unwrap()
.0;
assert_eq!(&decoded, t);
let decoded: T = bincode_2::decode_from_slice(&bincode_2_output, bincode_2_config)
.unwrap()
.0;
assert_eq!(&decoded, t);
}

pub fn test_same<T>(t: T)
where
T: bincode_2::Encode + serde::Serialize + core::fmt::Debug,
T: bincode_2::Encode
+ bincode_2::Decode
+ serde::Serialize
+ serde::de::DeserializeOwned
+ core::fmt::Debug
+ PartialEq,
{
test_same_with_config(
&t,
@@ -62,3 +87,12 @@ where
.with_fixed_int_encoding(),
);
}

pub fn gen_string(rng: &mut impl Rng) -> String {
let len = rng.gen_range(0..100usize);
let mut result = String::with_capacity(len * 4);
for _ in 0..len {
result.push(rng.gen_range('\0'..char::MAX));
}
result
}