#![cfg(all(feature = "std", feature = "derive"))]
use bincode_next::BitPacked;
use bincode_next::config;
#[derive(BitPacked, Debug, PartialEq, Clone, Copy)]
enum TestEnum {
#[bincode(bits = 2)]
A(#[bincode(bits = 6)] u8),
B,
}
#[test]
fn test_optional_bit_packing() {
let val = TestEnum::A(15);
let config_packed = config::standard()
.with_bit_packing()
.with_fixed_int_encoding();
let encoded_packed = bincode_next::encode_to_vec(val, config_packed).unwrap();
assert_eq!(encoded_packed.len(), 1);
let (decoded_packed, _): (TestEnum, usize) =
bincode_next::decode_from_slice(&encoded_packed, config_packed).unwrap();
assert_eq!(decoded_packed, val);
let config_unpacked = config::standard()
.with_no_bit_packing()
.with_fixed_int_encoding();
let encoded_unpacked = bincode_next::encode_to_vec(val, config_unpacked).unwrap();
assert_eq!(encoded_unpacked.len(), 5);
let (decoded_unpacked, _): (TestEnum, usize) =
bincode_next::decode_from_slice(&encoded_unpacked, config_unpacked).unwrap();
assert_eq!(decoded_unpacked, val);
let config_default = config::standard().with_fixed_int_encoding();
let encoded_default = bincode_next::encode_to_vec(val, config_default).unwrap();
assert_eq!(encoded_default.len(), 5);
}
#[derive(BitPacked, Debug, PartialEq, Clone, Copy)]
struct TestStruct {
#[bincode(bits = 4)]
a: u8,
#[bincode(bits = 4)]
b: u8,
}
#[test]
fn test_struct_optional_bit_packing() {
let val = TestStruct { a: 10, b: 5 };
let config_packed = config::standard()
.with_bit_packing()
.with_fixed_int_encoding();
let encoded_packed = bincode_next::encode_to_vec(val, config_packed).unwrap();
assert_eq!(encoded_packed.len(), 1);
let config_unpacked = config::standard()
.with_no_bit_packing()
.with_fixed_int_encoding();
let encoded_unpacked = bincode_next::encode_to_vec(val, config_unpacked).unwrap();
assert_eq!(encoded_unpacked.len(), 2);
}