From ffd2017c6ff7fc2b971207d0b4b0bac6acd123d4 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 10 Dec 2017 21:27:42 -0800 Subject: [PATCH] Use the default deserialize_from for Option The custom one was functionally identical to the default implementation given by the Deserialize trait. If someone has benchmarks that the custom one performs better, we can put it back. --- serde/src/de/impls.rs | 55 ++++--------------------------------------- 1 file changed, 5 insertions(+), 50 deletions(-) diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index c7ea2ede..494540b0 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -485,7 +485,6 @@ forwarded_impl!((), Box, CString::into_boxed_c_str); struct OptionVisitor { marker: PhantomData, } -struct OptionFromVisitor<'a, T: 'a>(&'a mut Option); impl<'de, T> Visitor<'de> for OptionVisitor where @@ -522,49 +521,6 @@ where } } -impl<'a, 'de, T> Visitor<'de> for OptionFromVisitor<'a, T> -where - T: Deserialize<'de>, -{ - type Value = (); - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("option") - } - - #[inline] - fn visit_unit(self) -> Result<(), E> - where - E: Error, - { - *self.0 = None; - Ok(()) - } - - #[inline] - fn visit_none(self) -> Result<(), E> - where - E: Error, - { - *self.0 = None; - Ok(()) - } - - #[inline] - fn visit_some(self, deserializer: D) -> Result<(), D::Error> - where - D: Deserializer<'de>, - { - // The some enum's repr is opaque, so we can't play cute tricks with - // its tag to build this in place unconditionally. - // - // FIXME: investigate whether branching on the old value being Some to - // deserialize_from the value is profitable (probably data-dependent?) - *self.0 = try!(T::deserialize(deserializer).map(Some)); - Ok(()) - } -} - impl<'de, T> Deserialize<'de> for Option where T: Deserialize<'de>, @@ -576,12 +532,11 @@ where deserializer.deserialize_option(OptionVisitor { marker: PhantomData }) } - fn deserialize_from(&mut self, deserializer: D) -> Result<(), D::Error> - where - D: Deserializer<'de>, - { - deserializer.deserialize_option(OptionFromVisitor(self)) - } + // The Some variant's repr is opaque, so we can't play cute tricks with its + // tag to have deserialize_from build the content in place unconditionally. + // + // FIXME: investigate whether branching on the old value being Some to + // deserialize_from the value is profitable (probably data-dependent?) } ////////////////////////////////////////////////////////////////////////////////