mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-14 02:21:01 +00:00
Support deserialization of struct keys from integers
serde-cbor supports a "packed" serialization flag which causes keys to be serialized as their indices, but the deserializer currently has to hardcode support for this format. We can simply support deserialization of struct keys from integers as we already do for enum variants.
This commit is contained in:
+14
-19
@@ -1386,26 +1386,21 @@ fn deserialize_identifier(
|
|||||||
"field identifier"
|
"field identifier"
|
||||||
};
|
};
|
||||||
|
|
||||||
let visit_index = if is_variant {
|
let variant_indices = 0u32..;
|
||||||
let variant_indices = 0u32..;
|
let fallthrough_msg = format!("variant index 0 <= i < {}", fields.len());
|
||||||
let fallthrough_msg = format!("variant index 0 <= i < {}", fields.len());
|
let visit_index = quote! {
|
||||||
let visit_index = quote! {
|
fn visit_u32<__E>(self, __value: u32) -> _serde::export::Result<Self::Value, __E>
|
||||||
fn visit_u32<__E>(self, __value: u32) -> _serde::export::Result<Self::Value, __E>
|
where __E: _serde::de::Error
|
||||||
where __E: _serde::de::Error
|
{
|
||||||
{
|
match __value {
|
||||||
match __value {
|
#(
|
||||||
#(
|
#variant_indices => _serde::export::Ok(#constructors),
|
||||||
#variant_indices => _serde::export::Ok(#constructors),
|
)*
|
||||||
)*
|
_ => _serde::export::Err(_serde::de::Error::invalid_value(
|
||||||
_ => _serde::export::Err(_serde::de::Error::invalid_value(
|
_serde::de::Unexpected::Unsigned(__value as u64),
|
||||||
_serde::de::Unexpected::Unsigned(__value as u64),
|
&#fallthrough_msg))
|
||||||
&#fallthrough_msg))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
Some(visit_index)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let bytes_to_str = if fallthrough.is_some() {
|
let bytes_to_str = if fallthrough.is_some() {
|
||||||
|
|||||||
@@ -605,6 +605,17 @@ declare_tests! {
|
|||||||
Token::StructEnd,
|
Token::StructEnd,
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
test_struct_integer_keys {
|
||||||
|
Struct { a: 1, b: 2, c: 0 } => &[
|
||||||
|
Token::Struct { name: "Struct", len: 2 },
|
||||||
|
Token::U32(0),
|
||||||
|
Token::I32(1),
|
||||||
|
|
||||||
|
Token::U32(1),
|
||||||
|
Token::I32(2),
|
||||||
|
Token::StructEnd,
|
||||||
|
],
|
||||||
|
}
|
||||||
test_enum_unit {
|
test_enum_unit {
|
||||||
Enum::Unit => &[
|
Enum::Unit => &[
|
||||||
Token::UnitVariant { name: "Enum", variant: "Unit" },
|
Token::UnitVariant { name: "Enum", variant: "Unit" },
|
||||||
|
|||||||
Reference in New Issue
Block a user