From cde8ea7f52874780d8a410f499bcc7debc1da532 Mon Sep 17 00:00:00 2001 From: Richard Bradfield Date: Wed, 8 Jul 2020 13:46:23 +0100 Subject: [PATCH] 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. --- src/de.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/de.rs b/src/de.rs index 7157ced..8063944 100644 --- a/src/de.rs +++ b/src/de.rs @@ -516,12 +516,16 @@ where visitor.visit_enum(Enum::<'a, R>(self)) } - /// Returns Error::IdentifierUnsupported. - fn deserialize_identifier(self, _visitor: V) -> Result + /// Deserialize the enum discriminant as a BARE Uint + fn deserialize_identifier(self, visitor: V) -> Result where V: de::Visitor<'de>, { - Err(Error::IdentifierUnsupported) + let Uint(id) = ::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.