| 1 |
use core::fmt::Debug; |
| 2 |
|
| 3 |
fn the_same_with_config<V, C, CMP>(element: &V, config: C, cmp: CMP) |
| 4 |
where |
| 5 |
V: TheSameTrait, |
| 6 |
C: bincode::config::Config, |
| 7 |
CMP: Fn(&V, &V) -> bool, |
| 8 |
{ |
| 9 |
let mut buffer = [0u8; 2048]; |
| 10 |
let len = bincode::encode_into_slice(element, &mut buffer, config).unwrap(); |
| 11 |
println!( |
| 12 |
"{:?} ({}): {:?} ({:?})", |
| 13 |
element, |
| 14 |
core::any::type_name::<V>(), |
| 15 |
&buffer[..len], |
| 16 |
core::any::type_name::<C>() |
| 17 |
); |
| 18 |
let (decoded, decoded_len): (V, usize) = bincode::decode_from_slice(&buffer, config).unwrap(); |
| 19 |
|
| 20 |
assert!( |
| 21 |
cmp(element, &decoded), |
| 22 |
"Comparison failed\nDecoded: {:?}\nExpected: {:?}\nBytes: {:?}", |
| 23 |
decoded, |
| 24 |
element, |
| 25 |
&buffer[..len], |
| 26 |
); |
| 27 |
assert_eq!(len, decoded_len); |
| 28 |
|
| 29 |
#[cfg(feature = "serde")] |
| 30 |
the_same_with_config_serde(element, config, cmp) |
| 31 |
} |
| 32 |
|
| 33 |
#[cfg(feature = "serde")] |
| 34 |
fn the_same_with_config_serde<V, C, CMP>(element: &V, config: C, cmp: CMP) |
| 35 |
where |
| 36 |
V: TheSameTrait, |
| 37 |
C: bincode::config::Config, |
| 38 |
CMP: Fn(&V, &V) -> bool, |
| 39 |
{ |
| 40 |
let mut buffer = [0u8; 2048]; |
| 41 |
let len = bincode::serde::encode_into_slice(element, &mut buffer, config); |
| 42 |
|
| 43 |
let decoded = bincode::serde::decode_from_slice(&buffer, config); |
| 44 |
|
| 45 |
let len = len.unwrap(); |
| 46 |
let (decoded, decoded_len): (V, usize) = decoded.unwrap(); |
| 47 |
println!( |
| 48 |
"{:?} ({}): {:?} ({:?})", |
| 49 |
element, |
| 50 |
core::any::type_name::<V>(), |
| 51 |
&buffer[..len], |
| 52 |
core::any::type_name::<C>() |
| 53 |
); |
| 54 |
|
| 55 |
assert!( |
| 56 |
cmp(element, &decoded), |
| 57 |
"Comparison failed\nDecoded: {:?}\nExpected: {:?}\nBytes: {:?}", |
| 58 |
decoded, |
| 59 |
element, |
| 60 |
&buffer[..len], |
| 61 |
); |
| 62 |
assert_eq!(len, decoded_len); |
| 63 |
} |
| 64 |
|
| 65 |
pub fn the_same_with_comparer<V, CMP>(element: V, cmp: CMP) |
| 66 |
where |
| 67 |
V: TheSameTrait, |
| 68 |
CMP: Fn(&V, &V) -> bool, |
| 69 |
{ |
| 70 |
// A matrix of each different config option possible |
| 71 |
the_same_with_config( |
| 72 |
&element, |
| 73 |
bincode::config::standard() |
| 74 |
.with_little_endian() |
| 75 |
.with_fixed_int_encoding(), |
| 76 |
&cmp, |
| 77 |
); |
| 78 |
the_same_with_config( |
| 79 |
&element, |
| 80 |
bincode::config::standard() |
| 81 |
.with_big_endian() |
| 82 |
.with_fixed_int_encoding(), |
| 83 |
&cmp, |
| 84 |
); |
| 85 |
the_same_with_config( |
| 86 |
&element, |
| 87 |
bincode::config::standard() |
| 88 |
.with_little_endian() |
| 89 |
.with_variable_int_encoding(), |
| 90 |
&cmp, |
| 91 |
); |
| 92 |
the_same_with_config( |
| 93 |
&element, |
| 94 |
bincode::config::standard() |
| 95 |
.with_big_endian() |
| 96 |
.with_variable_int_encoding(), |
| 97 |
&cmp, |
| 98 |
); |
| 99 |
the_same_with_config( |
| 100 |
&element, |
| 101 |
bincode::config::standard() |
| 102 |
.with_little_endian() |
| 103 |
.with_fixed_int_encoding(), |
| 104 |
&cmp, |
| 105 |
); |
| 106 |
the_same_with_config( |
| 107 |
&element, |
| 108 |
bincode::config::standard() |
| 109 |
.with_big_endian() |
| 110 |
.with_fixed_int_encoding(), |
| 111 |
&cmp, |
| 112 |
); |
| 113 |
the_same_with_config( |
| 114 |
&element, |
| 115 |
bincode::config::standard() |
| 116 |
.with_little_endian() |
| 117 |
.with_variable_int_encoding(), |
| 118 |
&cmp, |
| 119 |
); |
| 120 |
the_same_with_config( |
| 121 |
&element, |
| 122 |
bincode::config::standard() |
| 123 |
.with_big_endian() |
| 124 |
.with_variable_int_encoding(), |
| 125 |
&cmp, |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
#[cfg(feature = "serde")] |
| 130 |
pub trait TheSameTrait: |
| 131 |
bincode::Encode |
| 132 |
+ bincode::Decode<()> |
| 133 |
+ serde::de::DeserializeOwned |
| 134 |
+ serde::Serialize |
| 135 |
+ Debug |
| 136 |
+ 'static |
| 137 |
{ |
| 138 |
} |
| 139 |
#[cfg(feature = "serde")] |
| 140 |
impl<T> TheSameTrait for T where |
| 141 |
T: bincode::Encode |
| 142 |
+ bincode::Decode<()> |
| 143 |
+ serde::de::DeserializeOwned |
| 144 |
+ serde::Serialize |
| 145 |
+ Debug |
| 146 |
+ 'static |
| 147 |
{ |
| 148 |
} |
| 149 |
|
| 150 |
#[cfg(not(feature = "serde"))] |
| 151 |
pub trait TheSameTrait: bincode::Encode + bincode::Decode<()> + Debug + 'static {} |
| 152 |
#[cfg(not(feature = "serde"))] |
| 153 |
impl<T> TheSameTrait for T where T: bincode::Encode + bincode::Decode<()> + Debug + 'static {} |
| 154 |
|
| 155 |
#[allow(dead_code)] // This is not used in every test |
| 156 |
pub fn the_same<V: TheSameTrait + PartialEq>(element: V) { |
| 157 |
the_same_with_comparer(element, |a, b| a == b); |
| 158 |
} |