Deserialize Enum discriminant as Uint

The serializer encodes the variant of an enum as a BARE Uint, so the
deserializer should do the inverse when trying to deserialize a message.

Serde's serialize interface only receives variant indexes as u32,
despite the compiler allowing a cast to virtually any primitive integer
type.
This commit is contained in:
Richard Bradfield 2020-07-08 13:46:23 +01:00 committed by Tadeo Kondrak
parent 6695663b5b
commit cde8ea7f52
No known key found for this signature in database
GPG Key ID: D41E092CA43F1D8B
1 changed files with 7 additions and 3 deletions

View File

@ -516,12 +516,16 @@ where
visitor.visit_enum(Enum::<'a, R>(self)) visitor.visit_enum(Enum::<'a, R>(self))
} }
/// Returns Error::IdentifierUnsupported. /// Deserialize the enum discriminant as a BARE Uint
fn deserialize_identifier<V>(self, _visitor: V) -> Result<V::Value, Self::Error> fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where where
V: de::Visitor<'de>, V: de::Visitor<'de>,
{ {
Err(Error::IdentifierUnsupported) let Uint(id) = <Uint as de::Deserialize>::deserialize(&mut *self)?;
let variant: u32 = id.try_into().map_err(|_| {
Error::Message("Enum identifiers larger than u32 are not supported".to_string())
})?;
visitor.visit_u32(variant)
} }
/// Returns Error::AnyUnsupported. /// Returns Error::AnyUnsupported.