Restore visit_usize for identifying variants

This commit is contained in:
David Tolnay 2017-01-17 23:52:06 -08:00
parent aa88f01cdc
commit b1fbbfd3ce
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 31 additions and 3 deletions

View File

@ -657,6 +657,25 @@ fn deserialize_field_visitor(
Some(quote!(__ignore,))
};
let visit_usize = if is_variant {
let variant_indices = 0usize..;
let fallthrough_msg = format!("expected variant index 0 <= i < {}", fields.len());
Some(quote! {
fn visit_usize<__E>(self, value: usize) -> ::std::result::Result<__Field, __E>
where __E: _serde::de::Error
{
match value {
#(
#variant_indices => Ok(__Field::#field_idents),
)*
_ => Err(_serde::de::Error::invalid_value(#fallthrough_msg))
}
}
})
} else {
None
};
let fallthrough_arm = if is_variant {
quote! {
Err(_serde::de::Error::unknown_variant(value))
@ -688,6 +707,8 @@ fn deserialize_field_visitor(
impl _serde::de::Visitor for __FieldVisitor {
type Value = __Field;
#visit_usize
fn visit_str<__E>(self, value: &str) -> ::std::result::Result<__Field, __E>
where __E: _serde::de::Error
{

View File

@ -774,6 +774,13 @@ declare_tests! {
Token::EnumMapEnd,
],
}
test_enum_unit_usize {
Enum::Unit => &[
Token::EnumStart("Enum"),
Token::Usize(0),
Token::Unit,
],
}
test_box {
Box::new(0i32) => &[Token::I32(0)],
}
@ -918,13 +925,13 @@ declare_error_tests! {
],
Error::DuplicateField("a"),
}
test_enum_unit_usize<Enum> {
test_enum_out_of_range<Enum> {
&[
Token::EnumStart("Enum"),
Token::Usize(0),
Token::Usize(4),
Token::Unit,
],
Error::InvalidType(Type::U64),
Error::InvalidValue("expected variant index 0 <= i < 4".to_owned()),
}
test_enum_unit_bytes<Enum> {
&[