131 lines
24 KiB
HTML
131 lines
24 KiB
HTML
<!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."><meta name="keywords" content="rust, rustlang, rust-lang, DeserializeSeed"><title>DeserializeSeed in serde::de - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script src="../../crates.js"></script><script defer src="../../main.js"></script>
|
||
<noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.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="sidebar"><div class="sidebar-menu" role="button">☰</div><a class="sidebar-logo" href="../../serde/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.png" alt="logo"></div>
|
||
</a><h2 class="location">Trait DeserializeSeed</h2><div class="sidebar-elems"><div class="block items"><h3 class="sidebar-title"><a href="#associated-types">Associated Types</a></h3><div class="sidebar-links"><a href="#associatedtype.Value">Value</a></div><h3 class="sidebar-title"><a href="#required-methods">Required Methods</a></h3><div class="sidebar-links"><a href="#tymethod.deserialize">deserialize</a></div><h3 class="sidebar-title"><a href="#foreign-impls">Implementations on Foreign Types</a></h3><div class="sidebar-links"><a href="#impl-DeserializeSeed%3C%27de%3E-for-PhantomData%3CT%3E">PhantomData<T></a></div><h3 class="sidebar-title"><a href="#implementors">Implementors</a></h3></div><h2 class="location">Other items in<br><a href="../index.html">serde</a>::<wbr><a href="index.html">de</a></h2><div id="sidebar-vars" data-name="DeserializeSeed" data-ty="trait" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../serde/index.html"><img class="rust-logo" src="../../rust-logo.png" alt="logo"></a><nav class="sub"><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img width="18" height="18" alt="Pick another theme!" src="../../brush.svg"></button><div id="theme-choices" role="menu"></div></div><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../../settings.html" title="settings"><img width="18" height="18" alt="Change settings" src="../../wheel.svg"></a></div></form></nav></div><section id="main-content" class="content"><h1 class="fqn"><span class="in-band">Trait <a href="../index.html">serde</a>::<wbr><a href="index.html">de</a>::<wbr><a class="trait" href="#">DeserializeSeed</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span><span class="out-of-band"><span id="render-detail"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span><a class="srclink" href="../../src/serde/de/mod.rs.html#765-774" title="goto source code">[src]</a></span></h1><div class="docblock item-decl"><pre class="rust trait"><code>pub trait DeserializeSeed<'de>: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> {
|
||
type <a href="#associatedtype.Value" class="associatedtype">Value</a>;
|
||
fn <a href="#tymethod.deserialize" class="fnname">deserialize</a><D>(self, deserializer: D) -> <a class="enum" href="https://doc.rust-lang.org/1.59.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="associatedtype" href="trait.DeserializeSeed.html#associatedtype.Value" title="type serde::de::DeserializeSeed::Value">Value</a>, D::<a class="associatedtype" href="../trait.Deserializer.html#associatedtype.Error" title="type serde::Deserializer::Error">Error</a>><br> <span class="where">where<br> D: <a class="trait" href="../trait.Deserializer.html" title="trait serde::Deserializer">Deserializer</a><'de></span>;
|
||
}</code></pre></div><details class="rustdoc-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
|
||
ever find yourself looking for a way to pass data into a <code>Deserialize</code> impl,
|
||
this trait is the way to do it.</p>
|
||
<p>As one example of stateful deserialization consider deserializing a JSON
|
||
array into an existing buffer. Using the <code>Deserialize</code> trait we could
|
||
deserialize a JSON array into a <code>Vec<T></code> but it would be a freshly allocated
|
||
<code>Vec<T></code>; there is no way for <code>Deserialize</code> to reuse a previously allocated
|
||
buffer. Using <code>DeserializeSeed</code> instead makes this possible as in the
|
||
example code below.</p>
|
||
<p>The canonical API for stateless deserialization looks like this:</p>
|
||
|
||
<div class='information'><div class='tooltip edition' data-edition="2018">ⓘ</div></div><div class="example-wrap"><pre class="rust rust-example-rendered edition"><code><span class="kw">fn</span> <span class="ident">func</span><span class="op"><</span><span class="lifetime">'de</span>, <span class="ident">T</span>: <span class="ident">Deserialize</span><span class="op"><</span><span class="lifetime">'de</span><span class="op">></span><span class="op">></span>() -> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">T</span>, <span class="ident">Error</span><span class="op">></span></code></pre></div>
|
||
<p>Adjusting an API like this to support stateful deserialization is a matter
|
||
of accepting a seed as input:</p>
|
||
|
||
<div class='information'><div class='tooltip edition' data-edition="2018">ⓘ</div></div><div class="example-wrap"><pre class="rust rust-example-rendered edition"><code><span class="kw">fn</span> <span class="ident">func_seed</span><span class="op"><</span><span class="lifetime">'de</span>, <span class="ident">T</span>: <span class="ident">DeserializeSeed</span><span class="op"><</span><span class="lifetime">'de</span><span class="op">></span><span class="op">></span>(<span class="ident">seed</span>: <span class="ident">T</span>) -> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">T::Value</span>, <span class="ident">Error</span><span class="op">></span></code></pre></div>
|
||
<p>In practice the majority of deserialization is stateless. An API expecting a
|
||
seed can be appeased by passing <code>std::marker::PhantomData</code> as a seed in the
|
||
case of stateless deserialization.</p>
|
||
<h2 id="lifetime" class="section-header"><a href="#lifetime">Lifetime</a></h2>
|
||
<p>The <code>'de</code> lifetime of this trait is the lifetime of data that may be
|
||
borrowed by <code>Self::Value</code> when deserialized. See the page <a href="https://serde.rs/lifetimes.html">Understanding
|
||
deserializer lifetimes</a> for a more detailed explanation of these lifetimes.</p>
|
||
<h2 id="example" class="section-header"><a href="#example">Example</a></h2>
|
||
<p>Suppose we have JSON that looks like <code>[[1, 2], [3, 4, 5], [6]]</code> and we need
|
||
to deserialize it into a flat representation like <code>vec![1, 2, 3, 4, 5, 6]</code>.
|
||
Allocating a brand new <code>Vec<T></code> for each subarray would be slow. Instead we
|
||
would like to allocate a single <code>Vec<T></code> and then deserialize each subarray
|
||
into it. This requires stateful deserialization using the <code>DeserializeSeed</code>
|
||
trait.</p>
|
||
|
||
<div class='information'><div class='tooltip edition' data-edition="2018">ⓘ</div></div><div class="example-wrap"><pre class="rust rust-example-rendered edition"><code><span class="kw">use</span> <span class="ident">std::fmt</span>;
|
||
<span class="kw">use</span> <span class="ident">std::marker::PhantomData</span>;
|
||
|
||
<span class="kw">use</span> <span class="ident">serde::de</span>::{<span class="ident">Deserialize</span>, <span class="ident">DeserializeSeed</span>, <span class="ident">Deserializer</span>, <span class="ident">SeqAccess</span>, <span class="ident">Visitor</span>};
|
||
|
||
<span class="comment">// A DeserializeSeed implementation that uses stateful deserialization to</span>
|
||
<span class="comment">// append array elements onto the end of an existing vector. The preexisting</span>
|
||
<span class="comment">// state ("seed") in this case is the Vec<T>. The `deserialize` method of</span>
|
||
<span class="comment">// `ExtendVec` will be traversing the inner arrays of the JSON input and</span>
|
||
<span class="comment">// appending each integer into the existing Vec.</span>
|
||
<span class="kw">struct</span> <span class="ident">ExtendVec</span><span class="op"><</span><span class="lifetime">'a</span>, <span class="ident">T</span>: <span class="lifetime">'a</span><span class="op">></span>(<span class="kw-2">&</span><span class="lifetime">'a</span> <span class="kw-2">mut</span> <span class="ident">Vec</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>);
|
||
|
||
<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'de</span>, <span class="lifetime">'a</span>, <span class="ident">T</span><span class="op">></span> <span class="ident">DeserializeSeed</span><span class="op"><</span><span class="lifetime">'de</span><span class="op">></span> <span class="kw">for</span> <span class="ident">ExtendVec</span><span class="op"><</span><span class="lifetime">'a</span>, <span class="ident">T</span><span class="op">></span>
|
||
<span class="kw">where</span>
|
||
<span class="ident">T</span>: <span class="ident">Deserialize</span><span class="op"><</span><span class="lifetime">'de</span><span class="op">></span>,
|
||
{
|
||
<span class="comment">// The return type of the `deserialize` method. This implementation</span>
|
||
<span class="comment">// appends onto an existing vector but does not create any new data</span>
|
||
<span class="comment">// structure, so the return type is ().</span>
|
||
<span class="kw">type</span> <span class="ident">Value</span> <span class="op">=</span> ();
|
||
|
||
<span class="kw">fn</span> <span class="ident">deserialize</span><span class="op"><</span><span class="ident">D</span><span class="op">></span>(<span class="self">self</span>, <span class="ident">deserializer</span>: <span class="ident">D</span>) -> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident"><span class="self">Self</span>::Value</span>, <span class="ident">D::Error</span><span class="op">></span>
|
||
<span class="kw">where</span>
|
||
<span class="ident">D</span>: <span class="ident">Deserializer</span><span class="op"><</span><span class="lifetime">'de</span><span class="op">></span>,
|
||
{
|
||
<span class="comment">// Visitor implementation that will walk an inner array of the JSON</span>
|
||
<span class="comment">// input.</span>
|
||
<span class="kw">struct</span> <span class="ident">ExtendVecVisitor</span><span class="op"><</span><span class="lifetime">'a</span>, <span class="ident">T</span>: <span class="lifetime">'a</span><span class="op">></span>(<span class="kw-2">&</span><span class="lifetime">'a</span> <span class="kw-2">mut</span> <span class="ident">Vec</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>);
|
||
|
||
<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'de</span>, <span class="lifetime">'a</span>, <span class="ident">T</span><span class="op">></span> <span class="ident">Visitor</span><span class="op"><</span><span class="lifetime">'de</span><span class="op">></span> <span class="kw">for</span> <span class="ident">ExtendVecVisitor</span><span class="op"><</span><span class="lifetime">'a</span>, <span class="ident">T</span><span class="op">></span>
|
||
<span class="kw">where</span>
|
||
<span class="ident">T</span>: <span class="ident">Deserialize</span><span class="op"><</span><span class="lifetime">'de</span><span class="op">></span>,
|
||
{
|
||
<span class="kw">type</span> <span class="ident">Value</span> <span class="op">=</span> ();
|
||
|
||
<span class="kw">fn</span> <span class="ident">expecting</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">formatter</span>: <span class="kw-2">&mut</span> <span class="ident">fmt::Formatter</span>) -> <span class="ident">fmt::Result</span> {
|
||
<span class="macro">write!</span>(<span class="ident">formatter</span>, <span class="string">"an array of integers"</span>)
|
||
}
|
||
|
||
<span class="kw">fn</span> <span class="ident">visit_seq</span><span class="op"><</span><span class="ident">A</span><span class="op">></span>(<span class="self">self</span>, <span class="kw-2">mut</span> <span class="ident">seq</span>: <span class="ident">A</span>) -> <span class="prelude-ty">Result</span><span class="op"><</span>(), <span class="ident">A::Error</span><span class="op">></span>
|
||
<span class="kw">where</span>
|
||
<span class="ident">A</span>: <span class="ident">SeqAccess</span><span class="op"><</span><span class="lifetime">'de</span><span class="op">></span>,
|
||
{
|
||
<span class="comment">// Visit each element in the inner array and push it onto</span>
|
||
<span class="comment">// the existing vector.</span>
|
||
<span class="kw">while</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">elem</span>) <span class="op">=</span> <span class="ident">seq</span>.<span class="ident">next_element</span>()<span class="question-mark">?</span> {
|
||
<span class="self">self</span>.<span class="number">0</span>.<span class="ident">push</span>(<span class="ident">elem</span>);
|
||
}
|
||
<span class="prelude-val">Ok</span>(())
|
||
}
|
||
}
|
||
|
||
<span class="ident">deserializer</span>.<span class="ident">deserialize_seq</span>(<span class="ident">ExtendVecVisitor</span>(<span class="self">self</span>.<span class="number">0</span>))
|
||
}
|
||
}
|
||
|
||
<span class="comment">// Visitor implementation that will walk the outer array of the JSON input.</span>
|
||
<span class="kw">struct</span> <span class="ident">FlattenedVecVisitor</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>(<span class="ident">PhantomData</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>);
|
||
|
||
<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'de</span>, <span class="ident">T</span><span class="op">></span> <span class="ident">Visitor</span><span class="op"><</span><span class="lifetime">'de</span><span class="op">></span> <span class="kw">for</span> <span class="ident">FlattenedVecVisitor</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>
|
||
<span class="kw">where</span>
|
||
<span class="ident">T</span>: <span class="ident">Deserialize</span><span class="op"><</span><span class="lifetime">'de</span><span class="op">></span>,
|
||
{
|
||
<span class="comment">// This Visitor constructs a single Vec<T> to hold the flattened</span>
|
||
<span class="comment">// contents of the inner arrays.</span>
|
||
<span class="kw">type</span> <span class="ident">Value</span> <span class="op">=</span> <span class="ident">Vec</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>;
|
||
|
||
<span class="kw">fn</span> <span class="ident">expecting</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">formatter</span>: <span class="kw-2">&mut</span> <span class="ident">fmt::Formatter</span>) -> <span class="ident">fmt::Result</span> {
|
||
<span class="macro">write!</span>(<span class="ident">formatter</span>, <span class="string">"an array of arrays"</span>)
|
||
}
|
||
|
||
<span class="kw">fn</span> <span class="ident">visit_seq</span><span class="op"><</span><span class="ident">A</span><span class="op">></span>(<span class="self">self</span>, <span class="kw-2">mut</span> <span class="ident">seq</span>: <span class="ident">A</span>) -> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">Vec</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>, <span class="ident">A::Error</span><span class="op">></span>
|
||
<span class="kw">where</span>
|
||
<span class="ident">A</span>: <span class="ident">SeqAccess</span><span class="op"><</span><span class="lifetime">'de</span><span class="op">></span>,
|
||
{
|
||
<span class="comment">// Create a single Vec to hold the flattened contents.</span>
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">vec</span> <span class="op">=</span> <span class="ident">Vec::new</span>();
|
||
|
||
<span class="comment">// Each iteration through this loop is one inner array.</span>
|
||
<span class="kw">while</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(()) <span class="op">=</span> <span class="ident">seq</span>.<span class="ident">next_element_seed</span>(<span class="ident">ExtendVec</span>(<span class="kw-2">&mut</span> <span class="ident">vec</span>))<span class="question-mark">?</span> {
|
||
<span class="comment">// Nothing to do; inner array has been appended into `vec`.</span>
|
||
}
|
||
|
||
<span class="comment">// Return the finished vec.</span>
|
||
<span class="prelude-val">Ok</span>(<span class="ident">vec</span>)
|
||
}
|
||
}
|
||
|
||
<span class="kw">let</span> <span class="ident">visitor</span> <span class="op">=</span> <span class="ident">FlattenedVecVisitor</span>(<span class="ident">PhantomData</span>);
|
||
<span class="kw">let</span> <span class="ident">flattened</span>: <span class="ident">Vec</span><span class="op"><</span><span class="ident">u64</span><span class="op">></span> <span class="op">=</span> <span class="ident">deserializer</span>.<span class="ident">deserialize_seq</span>(<span class="ident">visitor</span>)<span class="question-mark">?</span>;</code></pre></div>
|
||
</div></details><h2 id="associated-types" class="small-section-header">Associated Types<a href="#associated-types" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle" open><summary><div id="associatedtype.Value" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/serde/de/mod.rs.html#767" title="goto source code">[src]</a></div><h4 class="code-header">type <a href="#associatedtype.Value" class="associatedtype">Value</a></h4></div></summary><div class="docblock"><p>The type produced by using this seed.</p>
|
||
</div></details></div><h2 id="required-methods" class="small-section-header">Required methods<a href="#required-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle" open><summary><div id="tymethod.deserialize" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/serde/de/mod.rs.html#771-773" title="goto source code">[src]</a></div><h4 class="code-header">fn <a href="#tymethod.deserialize" class="fnname">deserialize</a><D>(self, deserializer: D) -> <a class="enum" href="https://doc.rust-lang.org/1.59.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="associatedtype" href="trait.DeserializeSeed.html#associatedtype.Value" title="type serde::de::DeserializeSeed::Value">Value</a>, D::<a class="associatedtype" href="../trait.Deserializer.html#associatedtype.Error" title="type serde::Deserializer::Error">Error</a>> <span class="where fmt-newline">where<br> D: <a class="trait" href="../trait.Deserializer.html" title="trait serde::Deserializer">Deserializer</a><'de>, </span></h4></div></summary><div class="docblock"><p>Equivalent to the more common <code>Deserialize::deserialize</code> method, except
|
||
with some initial piece of data (the seed) passed in.</p>
|
||
</div></details></div><h2 id="foreign-impls" class="small-section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor"></a></h2><details class="rustdoc-toggle implementors-toggle"><summary><div id="impl-DeserializeSeed%3C%27de%3E-for-PhantomData%3CT%3E" class="impl has-srclink"><div class="rightside"><a class="srclink" href="../../src/serde/de/mod.rs.html#776-789" title="goto source code">[src]</a></div><a href="#impl-DeserializeSeed%3C%27de%3E-for-PhantomData%3CT%3E" class="anchor"></a><h3 class="code-header in-band">impl<'de, T> <a class="trait" href="trait.DeserializeSeed.html" title="trait serde::de::DeserializeSeed">DeserializeSeed</a><'de> for <a class="struct" href="https://doc.rust-lang.org/1.59.0/core/marker/struct.PhantomData.html" title="struct core::marker::PhantomData">PhantomData</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../trait.Deserialize.html" title="trait serde::Deserialize">Deserialize</a><'de>, </span></h3></div></summary><div class="impl-items"><div id="associatedtype.Value-1" class="type trait-impl has-srclink"><a href="#associatedtype.Value-1" class="anchor"></a><h4 class="code-header">type <a href="#associatedtype.Value" class="associatedtype">Value</a> = T</h4></div><div id="method.deserialize" class="method trait-impl has-srclink"><div class="rightside"><a class="srclink" href="../../src/serde/de/mod.rs.html#783-788" title="goto source code">[src]</a></div><a href="#method.deserialize" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.deserialize" class="fnname">deserialize</a><D>(self, deserializer: D) -> <a class="enum" href="https://doc.rust-lang.org/1.59.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, D::<a class="associatedtype" href="../trait.Deserializer.html#associatedtype.Error" title="type serde::Deserializer::Error">Error</a>> <span class="where fmt-newline">where<br> D: <a class="trait" href="../trait.Deserializer.html" title="trait serde::Deserializer">Deserializer</a><'de>, </span></h4></div></div></details><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div class="item-list" id="implementors-list"></div><script type="text/javascript" src="../../implementors/serde/de/trait.DeserializeSeed.js" async></script></section><section id="search" class="content hidden"></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="serde" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.59.0 (9d1b2106e 2022-02-23)" ></div>
|
||
</body></html> |