Created
November 16, 2017 12:33
-
-
Save MaikKlein/33d8b058a4a21e6095ec57c40e780bd4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| impl<'de, T> Deserialize<'de> for Message<T> | |
| where | |
| T: Deserialize<'de>, | |
| { | |
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
| where | |
| D: Deserializer<'de>, | |
| { | |
| struct Visitor<'de, T>(PhantomData<T>, PhantomData<&'de ()>); | |
| impl<'de, T> serde::de::Visitor<'de> for Visitor<'de, T> | |
| where | |
| T: Deserialize<'de>, | |
| { | |
| type Value = Message<T>; | |
| fn expecting( | |
| &self, | |
| formatter: &mut serde::export::Formatter, | |
| ) -> serde::export::fmt::Result { | |
| serde::export::Formatter::write_str(formatter, "Message") | |
| } | |
| #[inline] | |
| fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> | |
| where | |
| A: SeqAccess<'de>, | |
| { | |
| // Custom error here if the magic number is not correct | |
| let magic_number = seq.next_element::<u32>()?; | |
| let data = seq.next_element::<T>()?; | |
| let data = data.unwrap(); | |
| Ok(Message { data }) | |
| } | |
| } | |
| deserializer.deserialize_tuple_struct("Message", 2, Visitor(PhantomData, PhantomData)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment