]> Untitled Git - bitcoindevkit.org/blob
66447f80278e81acb1852db485f33e946eb1dce0
[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-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.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-9960930a.css"><link rel="stylesheet" href="../../../../../static.files/rustdoc-5d1d17d0.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.86.0-nightly (f3d1d47fd 2025-01-20)" data-channel="nightly" data-search-js="search-ccb196c1.js" data-settings-js="settings-0f613d39.js" ><script src="../../../../../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../../../../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../../../../../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../../../../../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../../../../../static.files/favicon-044be391.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_<wbr>chain</a><span class="version">0.21.1</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Variant<wbr>Access</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#lifetime" title="Lifetime">Lifetime</a></li><li><a href="#example-implementation" title="Example implementation">Example implementation</a></li></ul><h3><a href="#required-associated-types">Required Associated Types</a></h3><ul class="block"><li><a href="#associatedtype.Error" title="Error">Error</a></li></ul><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.newtype_variant_seed" title="newtype_variant_seed">newtype_variant_seed</a></li><li><a href="#tymethod.struct_variant" title="struct_variant">struct_variant</a></li><li><a href="#tymethod.tuple_variant" title="tuple_variant">tuple_variant</a></li><li><a href="#tymethod.unit_variant" title="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" title="newtype_variant">newtype_variant</a></li></ul><h3><a href="#dyn-compatibility">Dyn Compatibility</a></h3><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2><a href="index.html">In bdk_<wbr>chain::<wbr>bitcoin::<wbr>hashes::<wbr>serde::<wbr>de</a></h2></div></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"><span class="rustdoc-breadcrumbs"><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></span><h1>Trait <span class="trait">VariantAccess</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="https://docs.rs/serde/1.0.217/src/serde/de/mod.rs.html#2031">Source</a> </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.217/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.217/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.217/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.217/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.217/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.217/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="dyn-compatibility" class="section-header">Dyn Compatibility<a href="#dyn-compatibility" class="anchor">§</a></h2><div class="dyn-compatibility-info"><p>This trait is <b>not</b> <a href="https://doc.rust-lang.org/nightly/reference/items/traits.html#object-safety">dyn compatible</a>.</p><p><i>In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.</i></p></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>