From d156c65778aa38a9bcdaf7c7d77e59bae55187f9 Mon Sep 17 00:00:00 2001 From: Andrew Cann Date: Thu, 26 Dec 2019 12:39:03 +0800 Subject: [PATCH] Fix serde struct macros deserialization impls The Deserialize impls generated by serde_struct_impl and serde_struct_human_string_impl need to be able to handle serialization formats which serialize structs as sequences (such as bincode). This commit adds visit_seq methods to the Visitor types defined by these macros, in addition to the existing visit_map methods. The implementation is taken directly from the serde docs: https://serde.rs/deserialize-struct.html --- src/internal_macros.rs | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/internal_macros.rs b/src/internal_macros.rs index 362a2290..6307e330 100644 --- a/src/internal_macros.rs +++ b/src/internal_macros.rs @@ -282,6 +282,28 @@ macro_rules! serde_struct_impl { formatter.write_str("a struct") } + fn visit_seq(self, mut seq: V) -> Result + where + V: $crate::serde::de::SeqAccess<'de>, + { + use $crate::serde::de::Error; + + let length = 0; + $( + let $fe = seq.next_element()?.ok_or_else(|| { + Error::invalid_length(length, &self) + })?; + #[allow(unused_variables)] + let length = length + 1; + )* + + let ret = $name { + $($fe: $fe),* + }; + + Ok(ret) + } + fn visit_map(self, mut map: A) -> Result where A: $crate::serde::de::MapAccess<'de>, @@ -498,6 +520,28 @@ macro_rules! serde_struct_human_string_impl { formatter.write_str("a struct") } + fn visit_seq(self, mut seq: V) -> Result + where + V: $crate::serde::de::SeqAccess<'de>, + { + use $crate::serde::de::Error; + + let length = 0; + $( + let $fe = seq.next_element()?.ok_or_else(|| { + Error::invalid_length(length, &self) + })?; + #[allow(unused_variables)] + let length = length + 1; + )* + + let ret = $name { + $($fe: $fe),* + }; + + Ok(ret) + } + fn visit_map(self, mut map: A) -> Result where A: $crate::serde::de::MapAccess<'de>,