Merge pull request #2305 from serde-rs/enumaccessdeserializer

Add EnumAccessDeserializer to turn EnumAccess into a Deserializer
This commit is contained in:
David Tolnay 2022-10-21 10:04:08 -07:00 committed by GitHub
commit 6d009711a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1510,6 +1510,41 @@ where
////////////////////////////////////////////////////////////////////////////////
/// A deserializer holding an `EnumAccess`.
#[derive(Clone, Debug)]
pub struct EnumAccessDeserializer<A> {
access: A,
}
impl<A> EnumAccessDeserializer<A> {
/// Construct a new `EnumAccessDeserializer<A>`.
pub fn new(access: A) -> Self {
EnumAccessDeserializer { access: access }
}
}
impl<'de, A> de::Deserializer<'de> for EnumAccessDeserializer<A>
where
A: de::EnumAccess<'de>,
{
type Error = A::Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_enum(self.access)
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}
}
////////////////////////////////////////////////////////////////////////////////
mod private {
use lib::*;