<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Théodore&#39;s blog - rust</title>
    <subtitle>Writings about Rust, networking and systems programming.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://totodore.github.io/tags/rust/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://totodore.github.io"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2025-05-08T00:00:00+00:00</updated>
    <id>https://totodore.github.io/tags/rust/atom.xml</id>
    <entry xml:lang="en">
        <title>Inside Serde: Building a Custom JSON Deserializer with binary support and more</title>
        <published>2025-05-08T00:00:00+00:00</published>
        <updated>2025-05-08T00:00:00+00:00</updated>
        
        <author>
          <name>Théodore Prévot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://totodore.github.io/serde-wrapper/"/>
        <id>https://totodore.github.io/serde-wrapper/</id>
        
        <content type="html" xml:base="https://totodore.github.io/serde-wrapper/">&lt;h1 id=&quot;tl-dr&quot;&gt;TL;DR&lt;/h1&gt;
&lt;p&gt;Socket.IO is a JavaScript library for real-time, bidirectional communication between clients and servers, built on top of WebSockets and featuring a custom event-based protocol.
It uses a custom packet format that doesn’t map cleanly to Rust’s type system or serde’s model — especially with variadic arguments and out-of-band binary payloads. This post walks through how I extended &lt;code&gt;serde_json&lt;/code&gt; by wrapping its deserializer to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Lazily extract the event name without full deserialization&lt;/li&gt;
&lt;li&gt;Automatically reinject binary payloads into placeholders&lt;/li&gt;
&lt;li&gt;Differentiate between tuples and vectors for correct variadic decoding&lt;/li&gt;
&lt;li&gt;Eliminate the need for intermediate &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/serde_json/latest/serde_json/value/enum.Value.html&quot;&gt;&lt;code&gt;Value&lt;/code&gt;&lt;/a&gt; representation and two-phase parsing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The result is a clean, ergonomic, and performant single-phase deserialization system — with a 10× speedup on packet routing.&lt;/p&gt;
&lt;h1 id=&quot;socketioxide-introduction&quot;&gt;Socketioxide introduction&lt;/h1&gt;
&lt;p&gt;While working on &lt;a rel=&quot;external&quot; href=&quot;https://github.com/totodore/socketioxide&quot;&gt;Socketioxide&lt;/a&gt;, an Axum-like Socket.IO server implementation in Rust, I encountered an issue I initially thought would be trivial. Spoiler: it wasn’t. It led me to rethink how &lt;code&gt;serde&lt;/code&gt; works and ultimately dive deep into customizing it for the socketioxide needs.&lt;/p&gt;
&lt;h2 id=&quot;the-socket-io-protocol-quirks&quot;&gt;     The Socket.IO Protocol Quirks&lt;/h2&gt;
&lt;p&gt;Socket.IO comes with its own protocol. Under the hood, it’s mostly JSON, but with some custom rules:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;event name&lt;/strong&gt; is encoded as the &lt;strong&gt;first argument&lt;/strong&gt; of a JSON array.&lt;/li&gt;
&lt;li&gt;It supports &lt;strong&gt;variadic arguments&lt;/strong&gt;, where any number of payloads follow the event name.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Binary data&lt;/strong&gt; isn’t inlined — instead, it’s replaced by a placeholder and sent separately (e.g., in a WebSocket binary frame).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;em&gt;packet format&lt;/em&gt; looks something like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;packet type&amp;gt;[&amp;lt;# of binary attachments&amp;gt;-][&amp;lt;namespace&amp;gt;,][&amp;lt;acknowledgment id&amp;gt;][JSON-stringified payload without binary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;+ binary attachments extracted&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example, the following packet:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;52-/admin,[&amp;quot;foo&amp;quot;,[&amp;quot;message&amp;quot;,{&amp;quot;_placeholder&amp;quot;:true,&amp;quot;num&amp;quot;:0},{&amp;quot;_placeholder&amp;quot;:true,&amp;quot;num&amp;quot;:1}]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;+ &amp;lt;Buffer 01 02 ...&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;+ &amp;lt;Buffer 03 04 ...&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;should be parsed as a &lt;em&gt;binary event (packet id &lt;code&gt;5&lt;/code&gt;)&lt;/em&gt; that is part of the &lt;em&gt;“/admin”&lt;/em&gt; namespace with &lt;em&gt;&lt;code&gt;2&lt;/code&gt; binary attachments&lt;/em&gt;. The event name is &lt;em&gt;“foo”&lt;/em&gt; and the payload is the sequence &lt;code&gt;[&quot;message&quot;, &amp;lt;Buffer 01 02 ...&amp;gt;, &amp;lt;Buffer 03 04 ...&amp;gt;]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We want the user to be able to specify the whole spectrum of serde possibilities without being limited by socketioxide.&lt;/p&gt;
&lt;p&gt;The final API for writing a handler with socketioxide looks like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; my_foo_handler&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;payload&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Data&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Bytes&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Bytes&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h1 id=&quot;serde-where-it-breaks-down&quot;&gt;Serde: where it breaks down&lt;/h1&gt;
&lt;p&gt;The first emerging idea to solve this issue would be to write a custom JSON-parser that will partially decode our data. However that’s a &lt;strong&gt;terrible&lt;/strong&gt; idea for &lt;strong&gt;many&lt;/strong&gt; reasons.
It’s error prone, complex, the resulting code will be inefficent and insecure.&lt;/p&gt;
&lt;p&gt;Rust and &lt;code&gt;serde_json&lt;/code&gt; give us great tools to work with structured data, but they hit limitations in this protocol:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Tuple vs Vec ambiguity&lt;/strong&gt;: A tuple (e.g. &lt;code&gt;(i32, String)&lt;/code&gt;) and a vector (&lt;code&gt;Vec&amp;lt;Value&amp;gt;&lt;/code&gt;) are both serialized to a &lt;code&gt;Value::Array&lt;/code&gt;, making it hard to distinguish variadic arguments properly. Ideally, we’d treat tuples as argument lists, but not expand vectors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Binary placeholders&lt;/strong&gt;: There’s no clean way to inject binary buffers back into their correct place in the deserialized structure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Two-phase deserialization&lt;/strong&gt;: We must first parse the event to determine the correct handler, but the default approach fully deserializes the entire message too early, often unnecessarily.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Memory fragmentation&lt;/strong&gt;: Using &lt;code&gt;serde_json::Value&lt;/code&gt; for intermediate deserialization leads to lots of heap allocations.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;1-naive-solution-dynamic-value-handling-and-two-phase-deserialization&quot;&gt;     1. Naive solution: dynamic value handling and two-phase deserialization&lt;/h2&gt;
&lt;p&gt;My first approach was to use &lt;code&gt;serde_json::Value&lt;/code&gt; as a generic stand-in:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Extract the event name from the first element of the array.&lt;/li&gt;
&lt;li&gt;Recursively walk the structure to locate binary placeholders and associate them with actual payloads.&lt;/li&gt;
&lt;li&gt;Represent remaining arguments as a &lt;code&gt;Vec&amp;lt;Value&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Deserialize the &lt;code&gt;serde_json::Value&lt;/code&gt; to the user provided type.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This mostly worked and had the following trade-offs:&lt;/p&gt;
&lt;h3 id=&quot;advantages&quot;&gt;&lt;strong&gt;Advantages&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Simple and dynamic: Everything is handled at runtime.&lt;/li&gt;
&lt;li&gt;No need for users to write custom serializers/deserializers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;drawbacks&quot;&gt;&lt;strong&gt;Drawbacks&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Lacks precision: Can’t distinguish tuples from vectors — a critical protocol mismatch.&lt;/li&gt;
&lt;li&gt;No binary reinjection support: Once separated, placeholders and actual buffers are hard to correlate and rebind.
They are provided in an adjacent vector.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Value&lt;/code&gt; is heap-heavy: Parsing large messages fragments memory and reduces throughput.&lt;/li&gt;
&lt;li&gt;Two-pass deserialization means doubling the amount of work.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;2-another-naive-solution-annotated-fields&quot;&gt;     2. Another naive solution: annotated fields&lt;/h2&gt;
&lt;p&gt;Users could put &lt;code&gt;#[serde(deserialize_with = &quot;...&quot;)]&lt;/code&gt; on binary fields to inject the binary buffer manually during deserialization. Unfortunately, Serde does not allow custom deserialization functions to access external state (like the binary buffer queue) in a clean way. This would also force the user to think about specifying this.
And it doesn’t solve the other issues such as the support for multiple arguments.&lt;/p&gt;
&lt;p&gt;I wanted to stick with Serde’s ecosystem but needed more control. Thankfully serde is incredibly flexible.&lt;/p&gt;
&lt;h1 id=&quot;the-optimal-solution-a-custom-serializer-deserializer&quot;&gt;The optimal solution: a custom serializer/deserializer&lt;/h1&gt;
&lt;p&gt;Hence I had three main issues:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Tuples/arrays should be used for variadics and &lt;code&gt;Vec&lt;/code&gt; for sequences,
an issue induced by the use of an intermediate representation like &lt;code&gt;serde_json::Value&lt;/code&gt; which makes it impossible to differentiate, as they are both represented as sequences.&lt;/li&gt;
&lt;li&gt;Binary payload reinjection should happen at any level without iterating over an intermediate representation.&lt;/li&gt;
&lt;li&gt;In order to get rid of the intermediate representation, the event name should be deserialized in a lazy manner without parsing the full payload up front.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Eventually, I decided to write a custom deserializer that wraps &lt;code&gt;serde_json::Deserializer&lt;/code&gt; — this gave me:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Lazy deserialization&lt;/strong&gt;: I can extract just the event name without parsing the full payload up front, and defer deeper parsing until we’ve routed the event.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Precise handling of tuples vs arrays&lt;/strong&gt;: I can control how sequences are interpreted and enforce variadic behavior only where expected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Binary buffer reinjection&lt;/strong&gt;: I can track and inject external binary buffers at the correct locations with stateful deserialization.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Serde uses the &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Visitor_pattern&quot;&gt;visitor pattern&lt;/a&gt; for deserialization which makes it extremely modular by separating the incoming data side from the type defintion side. This pattern will be used for all the following features.&lt;/p&gt;
&lt;h2 id=&quot;1-lazy-deserialization&quot;&gt;     1. Lazy deserialization&lt;/h2&gt;
&lt;p&gt;Here is a quick example on how we can deserialize only the first element of a sequence by using a &lt;code&gt;FirstElement&lt;/code&gt; struct that will be a serde &lt;a rel=&quot;external&quot; href=&quot;https://serde.rs/impl-deserialize.html#the-visitor-trait&quot;&gt;visitor&lt;/a&gt; and a &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/serde/latest/serde/de/trait.DeserializeSeed.html&quot;&gt;deserialize seed&lt;/a&gt;.
This allows us to map any kind of data to any type in a statically defined manner.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; A seed that can be used to deserialize only the 1st element of a sequence&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; FirstElement&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;marker&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;PhantomData&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; We implement the Visitor for [`FirstElement`]. It will only be able to be visited by a sequence as we only expect this.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Deserialize&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Visitor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; FirstElement&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; We expect only a sequence, in any other case we will error something.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; visit_seq&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;A&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; mut&lt;/span&gt;&lt;span&gt; seq&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;        A&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;SeqAccess&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        use&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        let&lt;/span&gt;&lt;span&gt; data&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span&gt; seq&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;next_element&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;ok_or&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;custom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;first element not found&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Consume the rest of the sequence&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        while&lt;/span&gt;&lt;span&gt; seq&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;next_element&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;IgnoredAny&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;is_some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; DeserializeSeed is the stateful form of Deserialize to hold the custom type we are deserializing to.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Deserialize&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;DeserializeSeed&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; FirstElement&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; deserialize&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;D&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Deserializer&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; deserializer&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; D&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; D&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;        ///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; We are expecting a sequence of values.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        deserializer&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;deserialize_seq&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we can easily use our &lt;code&gt;FirstElement&lt;/code&gt;. It’s even in a zero-copy way!&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Extract the event name from a JSON array with the event being&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; the first element: [&amp;quot;foo&amp;quot;,...]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; read_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde_json&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;str&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; mut&lt;/span&gt;&lt;span&gt; de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde_json&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Deserializer&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;from_str&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;    FirstElement&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;str&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;default&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;deserialize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;mut&lt;/span&gt;&lt;span&gt; de&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But what about deserializing the real payload? How do you skip the event name? Well, as we are wrapping the &lt;code&gt;serde_json&lt;/code&gt; deserializer, we can implement custom behavior such as skipping the first element of the root JSON array:&lt;/p&gt;
&lt;p&gt;We start by building a custom visitor wrapper to skip the first element in a sequence.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; This custom [`SeqVisitor`] implementation is used to skip the first element of the sequence.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; This is useful when the first element of the sequence is an event value that we want to ignore.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; SeqVisitor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;V&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; V&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; V&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Visitor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Visitor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; SeqVisitor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;V&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; visit_seq&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;A&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; mut&lt;/span&gt;&lt;span&gt; seq&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;        A&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;SeqAccess&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        let&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span&gt; seq&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;next_element&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;IgnoredAny&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; We ignore the event value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;        ///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; We defer the rest of the sequence visits to the inner visitor.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;        self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;visit_seq&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;seq&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And we can create our visitor in our deserializer wrapper.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Deserializer&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;D&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; D&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; D&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Deserializer&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Deserializer&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Deserializer&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;D&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    type&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Error&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; D&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; deserialize_seq&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;V&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Visitor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; visitor&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; V&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;V&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt; Self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;        self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;deserialize_seq&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;SeqVisitor&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;visitor&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this we don’t need to deserialize our data to an intermediate dynamic value, we can immediately find the corresponding event handler and deserialize the raw JSON string to a user provided type and skip the event.&lt;/p&gt;
&lt;h2 id=&quot;2-precise-handling-of-tuples-vs-arrays-for-variadic-arguments&quot;&gt;     2. Precise handling of tuples vs arrays for variadic arguments&lt;/h2&gt;
&lt;p&gt;In Socket.IO if you send a vec with &lt;code&gt;socket.emit(&quot;event&quot;, [1, 2, 3, 4])&lt;/code&gt;, it will be serialized like this: &lt;code&gt;[event, [1, 2, 3, 4]]&lt;/code&gt; but if you send variadics with &lt;code&gt;socket.emit(&quot;event&quot;, 1, 2, 3, 4)&lt;/code&gt;, it will be serialized as: &lt;code&gt;[event, 1, 2, 3, 4]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But, how can we do the difference on the Rust-side? So that a multi-argument payload is deserialized to a tuple and a vector of data is deserialized to a vec?&lt;/p&gt;
&lt;p&gt;Well, that’s the user responsibility. They will know whether they are expecting a vec or tuple!&lt;/p&gt;
&lt;p&gt;So here is what we could do. First we need to know if the user provided a tuple or something else, prepare yourself—it’s kind of hacky.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;IsTupleSerde&lt;/code&gt; has an implementation for serializer and deserializer that will simply error immediately with a boolean saying
if the root type appears to be a tuple or not (without visiting anything or any data):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Deserializer&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; IsTupleSerde&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    type&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Error&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; IsTupleSerdeError&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; All these types are non-tuple&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;    serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;forward_to_deserialize_any!&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;        bool&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; i8&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; i16&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; i32&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; i64&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; i128&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u8&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u16&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u32&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u64&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u128&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; f32&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; f64&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; char&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        string&lt;/span&gt;&lt;span&gt; unit&lt;/span&gt;&lt;span&gt; unit_struct&lt;/span&gt;&lt;span&gt; seq&lt;/span&gt;&lt;span&gt;  map&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        struct&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; enum&lt;/span&gt;&lt;span&gt; identifier&lt;/span&gt;&lt;span&gt; ignored_any&lt;/span&gt;&lt;span&gt; bytes&lt;/span&gt;&lt;span&gt; byte_buf&lt;/span&gt;&lt;span&gt; option&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; The root type is not a tuple! We stop the deserialization process immediately by returning a stub error.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; deserialize_any&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;V&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Visitor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; _visitor&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; V&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;V&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt; Self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;        Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;IsTupleSerdeError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt;false&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; The root type is a tuple! We stop the deserialization process immediately by returning a stub error.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; deserialize_tuple&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;V&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Visitor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;        self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        _len&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; usize&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        _visitor&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; V&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;V&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt; Self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;        Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;IsTupleSerdeError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt;true&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can then make a little function to check if a type is a tuple or not:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Returns true if the type is a tuple-like type according to the serde model.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; is_de_tuple&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Deserialize&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; bool&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    match&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;deserialize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;IsTupleSerde&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;        Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; unreachable!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;        Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;IsTupleSerdeError&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;v&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; v&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then use it.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; from_value&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Deserialize&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    data&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde_json&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; We create a json deserialize and we wrap it with our own&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; to override some behaviors.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span&gt; inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde_json&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Deserializer&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;from_str&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span&gt; de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Deserializer&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; inner&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    if&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; is_de_tuple&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;        ///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Our type T is a tuple we deserialize everything incoming except the first element as it is the event.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;        T&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;deserialize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;de&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;        ///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Our type T is not a tuple we deserialize only the first element&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;        ///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; (it should be the second but the deserializer first skip the event).&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;        FirstElement&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;default&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;deserialize&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;de&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;3-binary-buffer-reinjection&quot;&gt;     3. Binary buffer reinjection&lt;/h2&gt;
&lt;p&gt;As you saw before, with the visitor pattern we can completely separate the incoming data side with the mapped user types.
This is what we are going to do! We are going to map serde maps (the binary placeholders) to user binary types.&lt;/p&gt;
&lt;p&gt;We can make a custom &lt;code&gt;BinaryVisitor&lt;/code&gt; wrapper that will be instantiated for any user provided bytes types.
But with a twist! When &lt;code&gt;serde_json&lt;/code&gt; will ask our binary visitor to visit a map, rather than bailing out, it will happily accept it and:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;check that it is a placeholder, e.g.: &lt;code&gt;{ &quot;_placeholder&quot;: true, &quot;num&quot;: 1 }&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;take the corresponding binary from the queue and return it.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; BinaryVisitor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;a&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; V&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    binary_payloads&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; VecDeque&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Bytes&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; V&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; V&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Visitor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Visitor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; BinaryVisitor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; V&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; visit_map&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;MapAccess&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; mut&lt;/span&gt;&lt;span&gt; map&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        #&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;derive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Deserialize&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        enum&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Val&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;            Placeholder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;bool&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;            Num&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;usize&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        let&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;key&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; val&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span&gt; map&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;next_entry&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Val&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;ok_or&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;custom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;expected a binary placeholder&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        let&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;key1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; val1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span&gt; map&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;next_entry&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Val&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;ok_or&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;custom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;expected a binary placeholder&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        match&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;key&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; val&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; key1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; val1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;_placeholder&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Val&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;Placeholder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt;true&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Val&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;Num&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;idx&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            |&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Val&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;Num&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;idx&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;_placeholder&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Val&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;Placeholder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt;true&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;                let&lt;/span&gt;&lt;span&gt; payload&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt; self&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;                    .&lt;/span&gt;&lt;span&gt;binary_payloads&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;                    .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;pop_front&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;                    .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;ok_or_else&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;custom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;format!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;binary payload &lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; not found&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; idx&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;                self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;visit_byte_buf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Vec&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;from&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;payload&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            _&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Err&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;custom&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;expected a binary placeholder&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; visit_borrowed_bytes&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;u8&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; E&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;        self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;visit_borrowed_bytes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;v&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; visit_byte_buf&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;u8&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; E&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;        self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;visit_byte_buf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;v&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; visit_bytes&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; serde&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;u8&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; E&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;        self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;visit_bytes&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;v&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we can use our &lt;code&gt;BinaryVisitor&lt;/code&gt; wrapper for any call to &lt;code&gt;deserialize_bytes&lt;/code&gt; or &lt;code&gt;deserialize_byte_buf&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Deserializer&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;a&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; D&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; D&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    binary_payloads&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; mut&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; VecDeque&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Bytes&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; D&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Deserializer&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; de&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Deserializer&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Deserializer&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;_&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; D&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    type&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Error&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; D&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    #&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;inline&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; deserialize_bytes&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;V&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Visitor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; visitor&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; V&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;V&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt; Self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;        self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;deserialize_map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;BinaryVisitor&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span&gt; visitor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            binary_payloads&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt; self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;binary_payloads&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    #&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;inline&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; deserialize_byte_buf&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;V&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Visitor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;de&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; visitor&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; V&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;V&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Value&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt; Self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;        self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;deserialize_map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;BinaryVisitor&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            inner&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span&gt; visitor&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            binary_payloads&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt; self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;binary_payloads&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;Et voilà! We have extended/twisted serde to do custom deserialization without writing any parsing code (I will happily let that for serde_json)!&lt;/p&gt;
&lt;p&gt;We went from a two-phase dynamic system to a single-phase, type-safe, performant deserialization pipeline to a one-phase deserialization process that handles:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;External binary data re-injection.&lt;/li&gt;
&lt;li&gt;First element (the event name) extracted for routing and skipped for user deserialization.&lt;/li&gt;
&lt;li&gt;Differentiation between tuples and vecs to support deserializing from JS sent variadics.&lt;/li&gt;
&lt;li&gt;Zero-copy deserialization! Don’t get me wrong—it’s still ‘partial’ zero-copy, since we’re using JSON. But for basic strings at least.&lt;/li&gt;
&lt;li&gt;Performance (because we all like the little thrill of perf improvements):
We reduced processing time for a basic packet from 600ns to just 60ns. Memory usage is also significantly more efficient, as no unnecessary allocations are made. Additionally, if the user chooses not to deserialize the incoming data—or if it doesn’t match the expected user types—it simply isn’t deserialized.&lt;/li&gt;
&lt;/ul&gt;
</content>
        
    </entry>
</feed>
