| 1 |
#![cfg(all(feature = "serde", feature = "alloc", feature = "derive"))] |
| 2 |
|
| 3 |
extern crate alloc; |
| 4 |
|
| 5 |
use serde_derive::{Deserialize, Serialize}; |
| 6 |
|
| 7 |
#[derive(Serialize, Deserialize, bincode::Encode, bincode::Decode)] |
| 8 |
pub struct SerdeRoundtrip { |
| 9 |
pub a: u32, |
| 10 |
#[serde(skip)] |
| 11 |
pub b: u32, |
| 12 |
pub c: TupleS, |
| 13 |
} |
| 14 |
|
| 15 |
#[derive(Serialize, Deserialize, bincode::Encode, bincode::Decode, PartialEq, Debug)] |
| 16 |
pub struct TupleS(f32, f32, f32); |
| 17 |
|
| 18 |
#[test] |
| 19 |
fn test_serde_round_trip() { |
| 20 |
// validate serde attribute working |
| 21 |
let json = serde_json::to_string(&SerdeRoundtrip { |
| 22 |
a: 5, |
| 23 |
b: 5, |
| 24 |
c: TupleS(2.0, 3.0, 4.0), |
| 25 |
}) |
| 26 |
.unwrap(); |
| 27 |
assert_eq!("{\"a\":5,\"c\":[2.0,3.0,4.0]}", json); |
| 28 |
|
| 29 |
let result: SerdeRoundtrip = serde_json::from_str(&json).unwrap(); |
| 30 |
assert_eq!(result.a, 5); |
| 31 |
assert_eq!(result.b, 0); |
| 32 |
|
| 33 |
// validate bincode working |
| 34 |
let bytes = bincode::serde::encode_to_vec( |
| 35 |
SerdeRoundtrip { |
| 36 |
a: 15, |
| 37 |
b: 15, |
| 38 |
c: TupleS(2.0, 3.0, 4.0), |
| 39 |
}, |
| 40 |
bincode::config::standard(), |
| 41 |
) |
| 42 |
.unwrap(); |
| 43 |
assert_eq!(bytes, &[15, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 128, 64]); |
| 44 |
let (result, len): (SerdeRoundtrip, usize) = |
| 45 |
bincode::serde::decode_from_slice(&bytes, bincode::config::standard()).unwrap(); |
| 46 |
assert_eq!(result.a, 15); |
| 47 |
assert_eq!(result.b, 0); // remember: b is skipped |
| 48 |
assert_eq!(result.c, TupleS(2.0, 3.0, 4.0)); |
| 49 |
assert_eq!(len, 13); |
| 50 |
} |
| 51 |
|
| 52 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] |
| 53 |
pub struct SerdeWithBorrowedData<'a> { |
| 54 |
pub a: u32, |
| 55 |
#[serde(skip)] |
| 56 |
pub b: u32, |
| 57 |
pub str: &'a str, |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
fn test_serialize_deserialize_borrowed_data() { |
| 62 |
let input = SerdeWithBorrowedData { |
| 63 |
a: 5, |
| 64 |
b: 5, |
| 65 |
str: "Hello world", |
| 66 |
}; |
| 67 |
|
| 68 |
#[rustfmt::skip] |
| 69 |
let expected = &[ |
| 70 |
5, // a |
| 71 |
// b is skipped |
| 72 |
11, // str length |
| 73 |
b'H', b'e', b'l', b'l', b'o', b' ', b'w', b'o', b'r', b'l', b'd' // str |
| 74 |
]; |
| 75 |
|
| 76 |
let mut result = [0u8; 20]; |
| 77 |
let len = bincode::serde::encode_into_slice(&input, &mut result, bincode::config::standard()) |
| 78 |
.unwrap(); |
| 79 |
let result = &result[..len]; |
| 80 |
assert_eq!(result, expected); |
| 81 |
|
| 82 |
let result = bincode::serde::encode_to_vec(&input, bincode::config::standard()).unwrap(); |
| 83 |
|
| 84 |
assert_eq!(result, expected); |
| 85 |
|
| 86 |
let (output, len): (SerdeWithBorrowedData, usize) = |
| 87 |
bincode::serde::borrow_decode_from_slice(&result, bincode::config::standard()).unwrap(); |
| 88 |
assert_eq!( |
| 89 |
SerdeWithBorrowedData { |
| 90 |
b: 0, // remember: b is skipped |
| 91 |
..input |
| 92 |
}, |
| 93 |
output |
| 94 |
); |
| 95 |
assert_eq!(len, 13); |
| 96 |
} |
| 97 |
|
| 98 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] |
| 99 |
pub struct SerdeWithOwnedData { |
| 100 |
pub a: u32, |
| 101 |
#[serde(skip)] |
| 102 |
pub b: u32, |
| 103 |
pub str: String, |
| 104 |
} |
| 105 |
|
| 106 |
#[test] |
| 107 |
fn test_serialize_deserialize_owned_data() { |
| 108 |
let input = SerdeWithOwnedData { |
| 109 |
a: 5, |
| 110 |
b: 5, |
| 111 |
str: String::from("Hello world"), |
| 112 |
}; |
| 113 |
|
| 114 |
#[rustfmt::skip] |
| 115 |
let expected = &[ |
| 116 |
5, // a |
| 117 |
// b is skipped |
| 118 |
11, // str length |
| 119 |
b'H', b'e', b'l', b'l', b'o', b' ', b'w', b'o', b'r', b'l', b'd' // str |
| 120 |
]; |
| 121 |
|
| 122 |
let mut result = [0u8; 20]; |
| 123 |
let len = bincode::serde::encode_into_slice(&input, &mut result, bincode::config::standard()) |
| 124 |
.unwrap(); |
| 125 |
let result = &result[..len]; |
| 126 |
assert_eq!(result, expected); |
| 127 |
|
| 128 |
let result = bincode::serde::encode_to_vec(&input, bincode::config::standard()).unwrap(); |
| 129 |
|
| 130 |
assert_eq!(result, expected); |
| 131 |
|
| 132 |
let (output, len): (SerdeWithOwnedData, usize) = |
| 133 |
bincode::serde::decode_from_slice(&result, bincode::config::standard()).unwrap(); |
| 134 |
assert_eq!( |
| 135 |
SerdeWithOwnedData { |
| 136 |
b: 0, // remember: b is skipped |
| 137 |
..input |
| 138 |
}, |
| 139 |
output |
| 140 |
); |
| 141 |
assert_eq!(len, 13); |
| 142 |
} |
| 143 |
|
| 144 |
#[cfg(feature = "derive")] |
| 145 |
mod derive { |
| 146 |
use bincode::{ |
| 147 |
serde::{BorrowCompat, Compat}, |
| 148 |
Decode, Encode, |
| 149 |
}; |
| 150 |
use serde_derive::{Deserialize, Serialize}; |
| 151 |
|
| 152 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] |
| 153 |
pub struct SerdeType { |
| 154 |
pub a: u32, |
| 155 |
} |
| 156 |
|
| 157 |
#[derive(Decode, Encode, PartialEq, Eq, Debug)] |
| 158 |
pub struct StructWithSerde { |
| 159 |
#[bincode(with_serde)] |
| 160 |
pub serde: SerdeType, |
| 161 |
} |
| 162 |
|
| 163 |
#[derive(Decode, Encode, PartialEq, Eq, Debug)] |
| 164 |
pub enum EnumWithSerde { |
| 165 |
Unit(#[bincode(with_serde)] SerdeType), |
| 166 |
Struct { |
| 167 |
#[bincode(with_serde)] |
| 168 |
serde: SerdeType, |
| 169 |
}, |
| 170 |
} |
| 171 |
|
| 172 |
#[test] |
| 173 |
fn test_serde_derive() { |
| 174 |
fn test_encode_decode<T>(start: T, expected_len: usize) |
| 175 |
where |
| 176 |
T: bincode::Encode + bincode::Decode<()> + PartialEq + core::fmt::Debug, |
| 177 |
{ |
| 178 |
let mut slice = [0u8; 100]; |
| 179 |
let len = bincode::encode_into_slice(&start, &mut slice, bincode::config::standard()) |
| 180 |
.unwrap(); |
| 181 |
assert_eq!(len, expected_len); |
| 182 |
let slice = &slice[..len]; |
| 183 |
let (result, len): (T, usize) = |
| 184 |
bincode::decode_from_slice(slice, bincode::config::standard()).unwrap(); |
| 185 |
|
| 186 |
assert_eq!(start, result); |
| 187 |
assert_eq!(len, expected_len); |
| 188 |
} |
| 189 |
test_encode_decode( |
| 190 |
StructWithSerde { |
| 191 |
serde: SerdeType { a: 5 }, |
| 192 |
}, |
| 193 |
1, |
| 194 |
); |
| 195 |
test_encode_decode(EnumWithSerde::Unit(SerdeType { a: 5 }), 2); |
| 196 |
test_encode_decode( |
| 197 |
EnumWithSerde::Struct { |
| 198 |
serde: SerdeType { a: 5 }, |
| 199 |
}, |
| 200 |
2, |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
#[test] |
| 205 |
fn test_vec_compat_debug() { |
| 206 |
let compat = Compat(vec![0, 1, 2, 3]); |
| 207 |
let debug_view = format!("{:?}", compat); |
| 208 |
assert_eq!(debug_view, "Compat([0, 1, 2, 3])") |
| 209 |
} |
| 210 |
|
| 211 |
#[test] |
| 212 |
fn test_i32_compat_debug() { |
| 213 |
let compat = Compat(1337_i32); |
| 214 |
let debug_view = format!("{:?}", compat); |
| 215 |
assert_eq!(debug_view, "Compat(1337)") |
| 216 |
} |
| 217 |
|
| 218 |
#[test] |
| 219 |
fn test_i32_compat_display() { |
| 220 |
let compat = Compat(1337_i32); |
| 221 |
let debug_view = format!("{}", compat); |
| 222 |
assert_eq!(debug_view, "1337") |
| 223 |
} |
| 224 |
|
| 225 |
#[test] |
| 226 |
fn test_f32_compat_display() { |
| 227 |
let compat = Compat(1.5_f32); |
| 228 |
let debug_view = format!("{}", compat); |
| 229 |
assert_eq!(debug_view, "1.5") |
| 230 |
} |
| 231 |
|
| 232 |
#[test] |
| 233 |
fn test_vec_borrow_compat_debug() { |
| 234 |
let vector = vec![0, 1, 2, 3]; |
| 235 |
let borrow_compat = BorrowCompat(&vector); |
| 236 |
let debug_view = format!("{:?}", borrow_compat); |
| 237 |
assert_eq!(debug_view, "BorrowCompat([0, 1, 2, 3])") |
| 238 |
} |
| 239 |
|
| 240 |
#[test] |
| 241 |
fn test_str_borrow_compat_debug() { |
| 242 |
let borrow_compat = BorrowCompat("Hello World!"); |
| 243 |
let debug_view = format!("{:?}", borrow_compat); |
| 244 |
assert_eq!(debug_view, "BorrowCompat(\"Hello World!\")") |
| 245 |
} |
| 246 |
|
| 247 |
#[test] |
| 248 |
fn test_str_borrow_compat_display() { |
| 249 |
let borrow_compat = BorrowCompat("Hello World!"); |
| 250 |
let debug_view = format!("{}", borrow_compat); |
| 251 |
assert_eq!(debug_view, "Hello World!") |
| 252 |
} |
| 253 |
} |