1 <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="`DeserializeSeed` is the stateful form of the `Deserialize` trait. If you ever find yourself looking for a way to pass data into a `Deserialize` impl, this trait is the way to do it."><title>DeserializeSeed in bdk_chain::bitcoin::hashes::serde::de - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../../../../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../../../../../static.files/rustdoc-dd39b87e5fcfba68.css"><meta name="rustdoc-vars" data-root-path="../../../../../" data-static-root-path="../../../../../static.files/" data-current-crate="bdk_chain" data-themes="" data-resource-suffix="" data-rustdoc-version="1.80.0-nightly (78a775127 2024-05-11)" data-channel="nightly" data-search-js="search-d52510db62a78183.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../../../../../static.files/storage-118b08c4c78b968e.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../../../../static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="../../../../../static.files/noscript-df360f571f6edeae.css"></noscript><link rel="alternate icon" type="image/png" href="../../../../../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../../../../../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc trait"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="../../../../../bdk_chain/index.html"><img src="https://github.com/bitcoindevkit/bdk/raw/master/static/bdk.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../../../../../bdk_chain/index.html"><img src="https://github.com/bitcoindevkit/bdk/raw/master/static/bdk.png" alt="logo"></a><h2><a href="../../../../../bdk_chain/index.html">bdk_chain</a><span class="version">0.18.0</span></h2></div><h2 class="location"><a href="#">DeserializeSeed</a></h2><div class="sidebar-elems"><section><h3><a href="#required-associated-types">Required Associated Types</a></h3><ul class="block"><li><a href="#associatedtype.Value">Value</a></li></ul><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.deserialize">deserialize</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-DeserializeSeed%3C'de%3E-for-PhantomData%3CT%3E">PhantomData<T></a></li></ul><h3><a href="#object-safety">Object Safety</a></h3><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In bdk_chain::bitcoin::hashes::serde::de</a></h2></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Trait <a href="../../../../index.html">bdk_chain</a>::<wbr><a href="../../../index.html">bitcoin</a>::<wbr><a href="../../index.html">hashes</a>::<wbr><a href="../index.html">serde</a>::<wbr><a href="index.html">de</a>::<wbr><a class="trait" href="#">DeserializeSeed</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="https://docs.rs/serde/1.0.209/src/serde/de/mod.rs.html#779">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><pre class="rust item-decl"><code>pub trait DeserializeSeed<'de>: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> {
2 type <a href="#associatedtype.Value" class="associatedtype">Value</a>;
5 fn <a href="#tymethod.deserialize" class="fn">deserialize</a><D>(
8 ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="associatedtype" href="trait.DeserializeSeed.html#associatedtype.Value" title="type bdk_chain::bitcoin::hashes::serde::de::DeserializeSeed::Value">Value</a>, <D as <a class="trait" href="../trait.Deserializer.html" title="trait bdk_chain::bitcoin::hashes::serde::Deserializer">Deserializer</a><'de>>::<a class="associatedtype" href="../trait.Deserializer.html#associatedtype.Error" title="type bdk_chain::bitcoin::hashes::serde::Deserializer::Error">Error</a>>
9 <span class="where">where D: <a class="trait" href="../trait.Deserializer.html" title="trait bdk_chain::bitcoin::hashes::serde::Deserializer">Deserializer</a><'de></span>;
10 }</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p><code>DeserializeSeed</code> is the stateful form of the <code>Deserialize</code> trait. If you
11 ever find yourself looking for a way to pass data into a <code>Deserialize</code> impl,
12 this trait is the way to do it.</p>
13 <p>As one example of stateful deserialization consider deserializing a JSON
14 array into an existing buffer. Using the <code>Deserialize</code> trait we could
15 deserialize a JSON array into a <code>Vec<T></code> but it would be a freshly allocated
16 <code>Vec<T></code>; there is no way for <code>Deserialize</code> to reuse a previously allocated
17 buffer. Using <code>DeserializeSeed</code> instead makes this possible as in the
18 example code below.</p>
19 <p>The canonical API for stateless deserialization looks like this:</p>
21 <div class="example-wrap edition"><a href="#" class="tooltip" title="This example runs with edition 2021">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>func<<span class="lifetime">'de</span>, T: Deserialize<<span class="lifetime">'de</span>>>() -> <span class="prelude-ty">Result</span><T, Error></code></pre></div>
22 <p>Adjusting an API like this to support stateful deserialization is a matter
23 of accepting a seed as input:</p>
25 <div class="example-wrap edition"><a href="#" class="tooltip" title="This example runs with edition 2021">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>func_seed<<span class="lifetime">'de</span>, T: DeserializeSeed<<span class="lifetime">'de</span>>>(seed: T) -> <span class="prelude-ty">Result</span><T::Value, Error></code></pre></div>
26 <p>In practice the majority of deserialization is stateless. An API expecting a
27 seed can be appeased by passing <code>std::marker::PhantomData</code> as a seed in the
28 case of stateless deserialization.</p>
29 <h2 id="lifetime"><a class="doc-anchor" href="#lifetime">§</a>Lifetime</h2>
30 <p>The <code>'de</code> lifetime of this trait is the lifetime of data that may be
31 borrowed by <code>Self::Value</code> when deserialized. See the page <a href="https://serde.rs/lifetimes.html">Understanding
32 deserializer lifetimes</a> for a more detailed explanation of these lifetimes.</p>
33 <h2 id="example"><a class="doc-anchor" href="#example">§</a>Example</h2>
34 <p>Suppose we have JSON that looks like <code>[[1, 2], [3, 4, 5], [6]]</code> and we need
35 to deserialize it into a flat representation like <code>vec![1, 2, 3, 4, 5, 6]</code>.
36 Allocating a brand new <code>Vec<T></code> for each subarray would be slow. Instead we
37 would like to allocate a single <code>Vec<T></code> and then deserialize each subarray
38 into it. This requires stateful deserialization using the <code>DeserializeSeed</code>
41 <div class="example-wrap edition"><a href="#" class="tooltip" title="This example runs with edition 2021">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="kw">use </span>serde::de::{Deserialize, DeserializeSeed, Deserializer, SeqAccess, Visitor};
42 <span class="kw">use </span>std::fmt;
43 <span class="kw">use </span>std::marker::PhantomData;
45 <span class="comment">// A DeserializeSeed implementation that uses stateful deserialization to
46 // append array elements onto the end of an existing vector. The preexisting
47 // state ("seed") in this case is the Vec<T>. The `deserialize` method of
48 // `ExtendVec` will be traversing the inner arrays of the JSON input and
49 // appending each integer into the existing Vec.
50 </span><span class="kw">struct </span>ExtendVec<<span class="lifetime">'a</span>, T: <span class="lifetime">'a</span>>(<span class="kw-2">&</span><span class="lifetime">'a </span><span class="kw-2">mut </span>Vec<T>);
52 <span class="kw">impl</span><<span class="lifetime">'de</span>, <span class="lifetime">'a</span>, T> DeserializeSeed<<span class="lifetime">'de</span>> <span class="kw">for </span>ExtendVec<<span class="lifetime">'a</span>, T>
53 <span class="kw">where
54 </span>T: Deserialize<<span class="lifetime">'de</span>>,
56 <span class="comment">// The return type of the `deserialize` method. This implementation
57 // appends onto an existing vector but does not create any new data
58 // structure, so the return type is ().
59 </span><span class="kw">type </span>Value = ();
61 <span class="kw">fn </span>deserialize<D>(<span class="self">self</span>, deserializer: D) -> <span class="prelude-ty">Result</span><<span class="self">Self</span>::Value, D::Error>
62 <span class="kw">where
63 </span>D: Deserializer<<span class="lifetime">'de</span>>,
65 <span class="comment">// Visitor implementation that will walk an inner array of the JSON
67 </span><span class="kw">struct </span>ExtendVecVisitor<<span class="lifetime">'a</span>, T: <span class="lifetime">'a</span>>(<span class="kw-2">&</span><span class="lifetime">'a </span><span class="kw-2">mut </span>Vec<T>);
69 <span class="kw">impl</span><<span class="lifetime">'de</span>, <span class="lifetime">'a</span>, T> Visitor<<span class="lifetime">'de</span>> <span class="kw">for </span>ExtendVecVisitor<<span class="lifetime">'a</span>, T>
70 <span class="kw">where
71 </span>T: Deserialize<<span class="lifetime">'de</span>>,
73 <span class="kw">type </span>Value = ();
75 <span class="kw">fn </span>expecting(<span class="kw-2">&</span><span class="self">self</span>, formatter: <span class="kw-2">&mut </span>fmt::Formatter) -> fmt::Result {
76 <span class="macro">write!</span>(formatter, <span class="string">"an array of integers"</span>)
79 <span class="kw">fn </span>visit_seq<A>(<span class="self">self</span>, <span class="kw-2">mut </span>seq: A) -> <span class="prelude-ty">Result</span><(), A::Error>
80 <span class="kw">where
81 </span>A: SeqAccess<<span class="lifetime">'de</span>>,
83 <span class="comment">// Decrease the number of reallocations if there are many elements
84 </span><span class="kw">if let </span><span class="prelude-val">Some</span>(size_hint) = seq.size_hint() {
85 <span class="self">self</span>.<span class="number">0</span>.reserve(size_hint);
88 <span class="comment">// Visit each element in the inner array and push it onto
89 // the existing vector.
90 </span><span class="kw">while let </span><span class="prelude-val">Some</span>(elem) = seq.next_element()<span class="question-mark">? </span>{
91 <span class="self">self</span>.<span class="number">0</span>.push(elem);
93 <span class="prelude-val">Ok</span>(())
97 deserializer.deserialize_seq(ExtendVecVisitor(<span class="self">self</span>.<span class="number">0</span>))
101 <span class="comment">// Visitor implementation that will walk the outer array of the JSON input.
102 </span><span class="kw">struct </span>FlattenedVecVisitor<T>(PhantomData<T>);
104 <span class="kw">impl</span><<span class="lifetime">'de</span>, T> Visitor<<span class="lifetime">'de</span>> <span class="kw">for </span>FlattenedVecVisitor<T>
105 <span class="kw">where
106 </span>T: Deserialize<<span class="lifetime">'de</span>>,
108 <span class="comment">// This Visitor constructs a single Vec<T> to hold the flattened
109 // contents of the inner arrays.
110 </span><span class="kw">type </span>Value = Vec<T>;
112 <span class="kw">fn </span>expecting(<span class="kw-2">&</span><span class="self">self</span>, formatter: <span class="kw-2">&mut </span>fmt::Formatter) -> fmt::Result {
113 <span class="macro">write!</span>(formatter, <span class="string">"an array of arrays"</span>)
116 <span class="kw">fn </span>visit_seq<A>(<span class="self">self</span>, <span class="kw-2">mut </span>seq: A) -> <span class="prelude-ty">Result</span><Vec<T>, A::Error>
117 <span class="kw">where
118 </span>A: SeqAccess<<span class="lifetime">'de</span>>,
120 <span class="comment">// Create a single Vec to hold the flattened contents.
121 </span><span class="kw">let </span><span class="kw-2">mut </span>vec = Vec::new();
123 <span class="comment">// Each iteration through this loop is one inner array.
124 </span><span class="kw">while let </span><span class="prelude-val">Some</span>(()) = seq.next_element_seed(ExtendVec(<span class="kw-2">&mut </span>vec))<span class="question-mark">? </span>{
125 <span class="comment">// Nothing to do; inner array has been appended into `vec`.
128 <span class="comment">// Return the finished vec.
129 </span><span class="prelude-val">Ok</span>(vec)
133 <span class="kw">let </span>visitor = FlattenedVecVisitor(PhantomData);
134 <span class="kw">let </span>flattened: Vec<u64> = deserializer.deserialize_seq(visitor)<span class="question-mark">?</span>;</code></pre></div>
135 </div></details><h2 id="required-associated-types" class="section-header">Required Associated Types<a href="#required-associated-types" class="anchor">§</a></h2><div class="methods"><details class="toggle" open><summary><section id="associatedtype.Value" class="method"><a class="src rightside" href="https://docs.rs/serde/1.0.209/src/serde/de/mod.rs.html#781">source</a><h4 class="code-header">type <a href="#associatedtype.Value" class="associatedtype">Value</a></h4></section></summary><div class="docblock"><p>The type produced by using this seed.</p>
136 </div></details></div><h2 id="required-methods" class="section-header">Required Methods<a href="#required-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="tymethod.deserialize" class="method"><a class="src rightside" href="https://docs.rs/serde/1.0.209/src/serde/de/mod.rs.html#785-787">source</a><h4 class="code-header">fn <a href="#tymethod.deserialize" class="fn">deserialize</a><D>(
139 ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="associatedtype" href="trait.DeserializeSeed.html#associatedtype.Value" title="type bdk_chain::bitcoin::hashes::serde::de::DeserializeSeed::Value">Value</a>, <D as <a class="trait" href="../trait.Deserializer.html" title="trait bdk_chain::bitcoin::hashes::serde::Deserializer">Deserializer</a><'de>>::<a class="associatedtype" href="../trait.Deserializer.html#associatedtype.Error" title="type bdk_chain::bitcoin::hashes::serde::Deserializer::Error">Error</a>><div class="where">where
140 D: <a class="trait" href="../trait.Deserializer.html" title="trait bdk_chain::bitcoin::hashes::serde::Deserializer">Deserializer</a><'de>,</div></h4></section></summary><div class="docblock"><p>Equivalent to the more common <code>Deserialize::deserialize</code> method, except
141 with some initial piece of data (the seed) passed in.</p>
142 </div></details></div><h2 id="object-safety" class="section-header">Object Safety<a href="#object-safety" class="anchor">§</a></h2><div class="object-safety-info">This trait is <b>not</b> <a href="https://doc.rust-lang.org/nightly/reference/items/traits.html#object-safety">object safe</a>.</div><h2 id="foreign-impls" class="section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor">§</a></h2><details class="toggle implementors-toggle"><summary><section id="impl-DeserializeSeed%3C'de%3E-for-PhantomData%3CT%3E" class="impl"><a class="src rightside" href="https://docs.rs/serde/1.0.209/src/serde/de/mod.rs.html#790-792">source</a><a href="#impl-DeserializeSeed%3C'de%3E-for-PhantomData%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<'de, T> <a class="trait" href="trait.DeserializeSeed.html" title="trait bdk_chain::bitcoin::hashes::serde::de::DeserializeSeed">DeserializeSeed</a><'de> for <a class="struct" href="https://doc.rust-lang.org/nightly/core/marker/struct.PhantomData.html" title="struct core::marker::PhantomData">PhantomData</a><T><div class="where">where
143 T: <a class="trait" href="../trait.Deserialize.html" title="trait bdk_chain::bitcoin::hashes::serde::Deserialize">Deserialize</a><'de>,</div></h3></section></summary><div class="impl-items"><section id="associatedtype.Value-1" class="associatedtype trait-impl"><a href="#associatedtype.Value-1" class="anchor">§</a><h4 class="code-header">type <a href="#associatedtype.Value" class="associatedtype">Value</a> = T</h4></section><section id="method.deserialize" class="method trait-impl"><a class="src rightside" href="https://docs.rs/serde/1.0.209/src/serde/de/mod.rs.html#797-799">source</a><a href="#method.deserialize" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.deserialize" class="fn">deserialize</a><D>(
146 ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <D as <a class="trait" href="../trait.Deserializer.html" title="trait bdk_chain::bitcoin::hashes::serde::Deserializer">Deserializer</a><'de>>::<a class="associatedtype" href="../trait.Deserializer.html#associatedtype.Error" title="type bdk_chain::bitcoin::hashes::serde::Deserializer::Error">Error</a>><div class="where">where
147 D: <a class="trait" href="../trait.Deserializer.html" title="trait bdk_chain::bitcoin::hashes::serde::Deserializer">Deserializer</a><'de>,</div></h4></section></div></details><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"></div><script src="../../../../../trait.impl/serde/de/trait.DeserializeSeed.js" data-ignore-extern-crates="core" async></script></section></div></main></body></html>