]> Untitled Git - bitcoindevkit.org/blob
78a5b08b4fa700b68d6df349cd074374de062281
[bitcoindevkit.org] /
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="`VariantAccess` is a visitor that is created by the `Deserializer` and passed to the `Deserialize` to deserialize the content of a particular enum variant."><title>VariantAccess 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.20.0</span></h2></div><h2 class="location"><a href="#">VariantAccess</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.Error">Error</a></li></ul><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.newtype_variant_seed">newtype_variant_seed</a></li><li><a href="#tymethod.struct_variant">struct_variant</a></li><li><a href="#tymethod.tuple_variant">tuple_variant</a></li><li><a href="#tymethod.unit_variant">unit_variant</a></li></ul><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.newtype_variant">newtype_variant</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="#">VariantAccess</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.216/src/serde/de/mod.rs.html#2031">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><pre class="rust item-decl"><code>pub trait VariantAccess&lt;'de&gt;: <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.Error" class="associatedtype">Error</a>: <a class="trait" href="trait.Error.html" title="trait bdk_chain::bitcoin::hashes::serde::de::Error">Error</a>;
3
4 // Required methods
5 fn <a href="#tymethod.unit_variant" class="fn">unit_variant</a>(self) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, Self::<a class="associatedtype" href="trait.VariantAccess.html#associatedtype.Error" title="type bdk_chain::bitcoin::hashes::serde::de::VariantAccess::Error">Error</a>&gt;;
6 <span class="item-spacer"></span> fn <a href="#tymethod.newtype_variant_seed" class="fn">newtype_variant_seed</a>&lt;T&gt;(
7 self,
8 seed: T
9 ) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;&lt;T as <a class="trait" href="trait.DeserializeSeed.html" title="trait bdk_chain::bitcoin::hashes::serde::de::DeserializeSeed">DeserializeSeed</a>&lt;'de&gt;&gt;::<a class="associatedtype" href="trait.DeserializeSeed.html#associatedtype.Value" title="type bdk_chain::bitcoin::hashes::serde::de::DeserializeSeed::Value">Value</a>, Self::<a class="associatedtype" href="trait.VariantAccess.html#associatedtype.Error" title="type bdk_chain::bitcoin::hashes::serde::de::VariantAccess::Error">Error</a>&gt;
10 <span class="where">where T: <a class="trait" href="trait.DeserializeSeed.html" title="trait bdk_chain::bitcoin::hashes::serde::de::DeserializeSeed">DeserializeSeed</a>&lt;'de&gt;</span>;
11 <span class="item-spacer"></span> fn <a href="#tymethod.tuple_variant" class="fn">tuple_variant</a>&lt;V&gt;(
12 self,
13 len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>,
14 visitor: V
15 ) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;&lt;V as <a class="trait" href="trait.Visitor.html" title="trait bdk_chain::bitcoin::hashes::serde::de::Visitor">Visitor</a>&lt;'de&gt;&gt;::<a class="associatedtype" href="trait.Visitor.html#associatedtype.Value" title="type bdk_chain::bitcoin::hashes::serde::de::Visitor::Value">Value</a>, Self::<a class="associatedtype" href="trait.VariantAccess.html#associatedtype.Error" title="type bdk_chain::bitcoin::hashes::serde::de::VariantAccess::Error">Error</a>&gt;
16 <span class="where">where V: <a class="trait" href="trait.Visitor.html" title="trait bdk_chain::bitcoin::hashes::serde::de::Visitor">Visitor</a>&lt;'de&gt;</span>;
17 <span class="item-spacer"></span> fn <a href="#tymethod.struct_variant" class="fn">struct_variant</a>&lt;V&gt;(
18 self,
19 fields: &amp;'static [&amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>],
20 visitor: V
21 ) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;&lt;V as <a class="trait" href="trait.Visitor.html" title="trait bdk_chain::bitcoin::hashes::serde::de::Visitor">Visitor</a>&lt;'de&gt;&gt;::<a class="associatedtype" href="trait.Visitor.html#associatedtype.Value" title="type bdk_chain::bitcoin::hashes::serde::de::Visitor::Value">Value</a>, Self::<a class="associatedtype" href="trait.VariantAccess.html#associatedtype.Error" title="type bdk_chain::bitcoin::hashes::serde::de::VariantAccess::Error">Error</a>&gt;
22 <span class="where">where V: <a class="trait" href="trait.Visitor.html" title="trait bdk_chain::bitcoin::hashes::serde::de::Visitor">Visitor</a>&lt;'de&gt;</span>;
23
24 // Provided method
25 fn <a href="#method.newtype_variant" class="fn">newtype_variant</a>&lt;T&gt;(self) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, Self::<a class="associatedtype" href="trait.VariantAccess.html#associatedtype.Error" title="type bdk_chain::bitcoin::hashes::serde::de::VariantAccess::Error">Error</a>&gt;
26 <span class="where">where T: <a class="trait" href="../trait.Deserialize.html" title="trait bdk_chain::bitcoin::hashes::serde::Deserialize">Deserialize</a>&lt;'de&gt;</span> { ... }
27 }</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p><code>VariantAccess</code> is a visitor that is created by the <code>Deserializer</code> and
28 passed to the <code>Deserialize</code> to deserialize the content of a particular enum
29 variant.</p>
30 <h2 id="lifetime"><a class="doc-anchor" href="#lifetime">§</a>Lifetime</h2>
31 <p>The <code>'de</code> lifetime of this trait is the lifetime of data that may be
32 borrowed by the deserialized enum variant. See the page <a href="https://serde.rs/lifetimes.html">Understanding
33 deserializer lifetimes</a> for a more detailed explanation of these lifetimes.</p>
34 <h2 id="example-implementation"><a class="doc-anchor" href="#example-implementation">§</a>Example implementation</h2>
35 <p>The <a href="https://serde.rs/data-format.html">example data format</a> presented on the website demonstrates an
36 implementation of <code>VariantAccess</code> for a basic JSON data format.</p>
37 </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.Error" class="method"><a class="src rightside" href="https://docs.rs/serde/1.0.216/src/serde/de/mod.rs.html#2034">source</a><h4 class="code-header">type <a href="#associatedtype.Error" class="associatedtype">Error</a>: <a class="trait" href="trait.Error.html" title="trait bdk_chain::bitcoin::hashes::serde::de::Error">Error</a></h4></section></summary><div class="docblock"><p>The error type that can be returned if some error occurs during
38 deserialization. Must match the error type of our <code>EnumAccess</code>.</p>
39 </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.unit_variant" class="method"><a class="src rightside" href="https://docs.rs/serde/1.0.216/src/serde/de/mod.rs.html#2071">source</a><h4 class="code-header">fn <a href="#tymethod.unit_variant" class="fn">unit_variant</a>(self) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, Self::<a class="associatedtype" href="trait.VariantAccess.html#associatedtype.Error" title="type bdk_chain::bitcoin::hashes::serde::de::VariantAccess::Error">Error</a>&gt;</h4></section></summary><div class="docblock"><p>Called when deserializing a variant with no values.</p>
40 <p>If the data contains a different type of variant, the following
41 <code>invalid_type</code> error should be constructed:</p>
42
43 <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>unit_variant(<span class="self">self</span>) -&gt; <span class="prelude-ty">Result</span>&lt;(), <span class="self">Self</span>::Error&gt; {
44 <span class="comment">// What the data actually contained; suppose it is a tuple variant.
45 </span><span class="kw">let </span>unexp = Unexpected::TupleVariant;
46 <span class="prelude-val">Err</span>(de::Error::invalid_type(unexp, <span class="kw-2">&amp;</span><span class="string">"unit variant"</span>))
47 }</code></pre></div>
48 </div></details><details class="toggle method-toggle" open><summary><section id="tymethod.newtype_variant_seed" class="method"><a class="src rightside" href="https://docs.rs/serde/1.0.216/src/serde/de/mod.rs.html#2113-2115">source</a><h4 class="code-header">fn <a href="#tymethod.newtype_variant_seed" class="fn">newtype_variant_seed</a>&lt;T&gt;(
49 self,
50 seed: T
51 ) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;&lt;T as <a class="trait" href="trait.DeserializeSeed.html" title="trait bdk_chain::bitcoin::hashes::serde::de::DeserializeSeed">DeserializeSeed</a>&lt;'de&gt;&gt;::<a class="associatedtype" href="trait.DeserializeSeed.html#associatedtype.Value" title="type bdk_chain::bitcoin::hashes::serde::de::DeserializeSeed::Value">Value</a>, Self::<a class="associatedtype" href="trait.VariantAccess.html#associatedtype.Error" title="type bdk_chain::bitcoin::hashes::serde::de::VariantAccess::Error">Error</a>&gt;<div class="where">where
52 T: <a class="trait" href="trait.DeserializeSeed.html" title="trait bdk_chain::bitcoin::hashes::serde::de::DeserializeSeed">DeserializeSeed</a>&lt;'de&gt;,</div></h4></section></summary><div class="docblock"><p>Called when deserializing a variant with a single value.</p>
53 <p><code>Deserialize</code> implementations should typically use
54 <code>VariantAccess::newtype_variant</code> instead.</p>
55 <p>If the data contains a different type of variant, the following
56 <code>invalid_type</code> error should be constructed:</p>
57
58 <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>newtype_variant_seed&lt;T&gt;(<span class="self">self</span>, _seed: T) -&gt; <span class="prelude-ty">Result</span>&lt;T::Value, <span class="self">Self</span>::Error&gt;
59 <span class="kw">where
60 </span>T: DeserializeSeed&lt;<span class="lifetime">'de</span>&gt;,
61 {
62 <span class="comment">// What the data actually contained; suppose it is a unit variant.
63 </span><span class="kw">let </span>unexp = Unexpected::UnitVariant;
64 <span class="prelude-val">Err</span>(de::Error::invalid_type(unexp, <span class="kw-2">&amp;</span><span class="string">"newtype variant"</span>))
65 }</code></pre></div>
66 </div></details><details class="toggle method-toggle" open><summary><section id="tymethod.tuple_variant" class="method"><a class="src rightside" href="https://docs.rs/serde/1.0.216/src/serde/de/mod.rs.html#2169-2171">source</a><h4 class="code-header">fn <a href="#tymethod.tuple_variant" class="fn">tuple_variant</a>&lt;V&gt;(
67 self,
68 len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>,
69 visitor: V
70 ) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;&lt;V as <a class="trait" href="trait.Visitor.html" title="trait bdk_chain::bitcoin::hashes::serde::de::Visitor">Visitor</a>&lt;'de&gt;&gt;::<a class="associatedtype" href="trait.Visitor.html#associatedtype.Value" title="type bdk_chain::bitcoin::hashes::serde::de::Visitor::Value">Value</a>, Self::<a class="associatedtype" href="trait.VariantAccess.html#associatedtype.Error" title="type bdk_chain::bitcoin::hashes::serde::de::VariantAccess::Error">Error</a>&gt;<div class="where">where
71 V: <a class="trait" href="trait.Visitor.html" title="trait bdk_chain::bitcoin::hashes::serde::de::Visitor">Visitor</a>&lt;'de&gt;,</div></h4></section></summary><div class="docblock"><p>Called when deserializing a tuple-like variant.</p>
72 <p>The <code>len</code> is the number of fields expected in the tuple variant.</p>
73 <p>If the data contains a different type of variant, the following
74 <code>invalid_type</code> error should be constructed:</p>
75
76 <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>tuple_variant&lt;V&gt;(<span class="self">self</span>, _len: usize, _visitor: V) -&gt; <span class="prelude-ty">Result</span>&lt;V::Value, <span class="self">Self</span>::Error&gt;
77 <span class="kw">where
78 </span>V: Visitor&lt;<span class="lifetime">'de</span>&gt;,
79 {
80 <span class="comment">// What the data actually contained; suppose it is a unit variant.
81 </span><span class="kw">let </span>unexp = Unexpected::UnitVariant;
82 <span class="prelude-val">Err</span>(de::Error::invalid_type(unexp, <span class="kw-2">&amp;</span><span class="string">"tuple variant"</span>))
83 }</code></pre></div>
84 </div></details><details class="toggle method-toggle" open><summary><section id="tymethod.struct_variant" class="method"><a class="src rightside" href="https://docs.rs/serde/1.0.216/src/serde/de/mod.rs.html#2216-2222">source</a><h4 class="code-header">fn <a href="#tymethod.struct_variant" class="fn">struct_variant</a>&lt;V&gt;(
85 self,
86 fields: &amp;'static [&amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>],
87 visitor: V
88 ) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;&lt;V as <a class="trait" href="trait.Visitor.html" title="trait bdk_chain::bitcoin::hashes::serde::de::Visitor">Visitor</a>&lt;'de&gt;&gt;::<a class="associatedtype" href="trait.Visitor.html#associatedtype.Value" title="type bdk_chain::bitcoin::hashes::serde::de::Visitor::Value">Value</a>, Self::<a class="associatedtype" href="trait.VariantAccess.html#associatedtype.Error" title="type bdk_chain::bitcoin::hashes::serde::de::VariantAccess::Error">Error</a>&gt;<div class="where">where
89 V: <a class="trait" href="trait.Visitor.html" title="trait bdk_chain::bitcoin::hashes::serde::de::Visitor">Visitor</a>&lt;'de&gt;,</div></h4></section></summary><div class="docblock"><p>Called when deserializing a struct-like variant.</p>
90 <p>The <code>fields</code> are the names of the fields of the struct variant.</p>
91 <p>If the data contains a different type of variant, the following
92 <code>invalid_type</code> error should be constructed:</p>
93
94 <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>struct_variant&lt;V&gt;(
95 <span class="self">self</span>,
96 _fields: <span class="kw-2">&amp;</span><span class="lifetime">'static </span>[<span class="kw-2">&amp;</span><span class="lifetime">'static </span>str],
97 _visitor: V,
98 ) -&gt; <span class="prelude-ty">Result</span>&lt;V::Value, <span class="self">Self</span>::Error&gt;
99 <span class="kw">where
100 </span>V: Visitor&lt;<span class="lifetime">'de</span>&gt;,
101 {
102 <span class="comment">// What the data actually contained; suppose it is a unit variant.
103 </span><span class="kw">let </span>unexp = Unexpected::UnitVariant;
104 <span class="prelude-val">Err</span>(de::Error::invalid_type(unexp, <span class="kw-2">&amp;</span><span class="string">"struct variant"</span>))
105 }</code></pre></div>
106 </div></details></div><h2 id="provided-methods" class="section-header">Provided Methods<a href="#provided-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="method.newtype_variant" class="method"><a class="src rightside" href="https://docs.rs/serde/1.0.216/src/serde/de/mod.rs.html#2123-2125">source</a><h4 class="code-header">fn <a href="#method.newtype_variant" class="fn">newtype_variant</a>&lt;T&gt;(self) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, Self::<a class="associatedtype" href="trait.VariantAccess.html#associatedtype.Error" title="type bdk_chain::bitcoin::hashes::serde::de::VariantAccess::Error">Error</a>&gt;<div class="where">where
107 T: <a class="trait" href="../trait.Deserialize.html" title="trait bdk_chain::bitcoin::hashes::serde::Deserialize">Deserialize</a>&lt;'de&gt;,</div></h4></section></summary><div class="docblock"><p>Called when deserializing a variant with a single value.</p>
108 <p>This method exists as a convenience for <code>Deserialize</code> implementations.
109 <code>VariantAccess</code> implementations should not override the default
110 behavior.</p>
111 </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="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"></div><script src="../../../../../trait.impl/serde/de/trait.VariantAccess.js" async></script></section></div></main></body></html>