mentat/serde/ser/trait.Serializer.html

944 lines
135 KiB
HTML
Raw Normal View History

2018-08-22 17:04:13 +00:00
<!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="API documentation for the Rust `Serializer` trait in crate `serde`.">
<meta name="keywords" content="rust, rustlang, rust-lang, Serializer">
<title>serde::ser::Serializer - Rust</title>
<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="../../dark.css">
<link rel="stylesheet" type="text/css" href="../../main.css" id="themeStyle">
<script src="../../storage.js"></script>
</head>
<body class="rustdoc trait">
<!--[if lte IE 8]>
<div class="warning">
This old browser is unsupported and will most likely display funky
things.
</div>
<![endif]-->
<nav class="sidebar">
<div class="sidebar-menu">&#9776;</div>
<p class='location'>Trait Serializer</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#associated-types">Associated Types</a><div class="sidebar-links"><a href="#associatedtype.Ok">Ok</a><a href="#associatedtype.Error">Error</a><a href="#associatedtype.SerializeSeq">SerializeSeq</a><a href="#associatedtype.SerializeTuple">SerializeTuple</a><a href="#associatedtype.SerializeTupleStruct">SerializeTupleStruct</a><a href="#associatedtype.SerializeTupleVariant">SerializeTupleVariant</a><a href="#associatedtype.SerializeMap">SerializeMap</a><a href="#associatedtype.SerializeStruct">SerializeStruct</a><a href="#associatedtype.SerializeStructVariant">SerializeStructVariant</a></div><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.serialize_bool">serialize_bool</a><a href="#tymethod.serialize_i8">serialize_i8</a><a href="#tymethod.serialize_i16">serialize_i16</a><a href="#tymethod.serialize_i32">serialize_i32</a><a href="#tymethod.serialize_i64">serialize_i64</a><a href="#tymethod.serialize_u8">serialize_u8</a><a href="#tymethod.serialize_u16">serialize_u16</a><a href="#tymethod.serialize_u32">serialize_u32</a><a href="#tymethod.serialize_u64">serialize_u64</a><a href="#tymethod.serialize_f32">serialize_f32</a><a href="#tymethod.serialize_f64">serialize_f64</a><a href="#tymethod.serialize_char">serialize_char</a><a href="#tymethod.serialize_str">serialize_str</a><a href="#tymethod.serialize_bytes">serialize_bytes</a><a href="#tymethod.serialize_none">serialize_none</a><a href="#tymethod.serialize_some">serialize_some</a><a href="#tymethod.serialize_unit">serialize_unit</a><a href="#tymethod.serialize_unit_struct">serialize_unit_struct</a><a href="#tymethod.serialize_unit_variant">serialize_unit_variant</a><a href="#tymethod.serialize_newtype_struct">serialize_newtype_struct</a><a href="#tymethod.serialize_newtype_variant">serialize_newtype_variant</a><a href="#tymethod.serialize_seq">serialize_seq</a><a href="#tymethod.serialize_tuple">serialize_tuple</a><a href="#tymethod.serialize_tuple_struct">serialize_tuple_struct</a><a href="#tymethod.serialize_tuple_variant">serialize_tuple_variant</a><a href="#tymethod.serialize_map">serialize_map</a><a href="#tymethod.serialize_struct">serialize_struct</a><a href="#tymethod.serialize_struct_variant">serialize_struct_variant</a></div><a class="sidebar-title" href="#provided-methods">Provided Methods</a><div class="sidebar-links"><a href="#method.collect_seq">collect_seq</a><a href="#method.collect_map">collect_map</a><a href="#method.collect_str">collect_str</a><a href="#method.is_human_readable">is_human_readable</a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='../index.html'>serde</a>::<wbr><a href='index.html'>ser</a></p><script>window.sidebarCurrent = {name: 'Serializer', ty: 'trait', relpath: ''};</script><script defer src="sidebar-items.js"></script></div>
</nav>
<div class="theme-picker">
<button id="theme-picker" aria-label="Pick another theme!">
<img src="../../brush.svg" width="18" alt="Pick another theme!">
</button>
<div id="theme-choices"></div>
</div>
<script src="../../theme.js"></script>
<nav class="sub">
<form class="search-form js-only">
<div class="search-container">
<input class="search-input" name="search"
autocomplete="off"
placeholder="Click or press S to search, ? for more options…"
type="search">
</div>
</form>
</nav>
<section id='main' class="content">
<h1 class='fqn'><span class='in-band'>Trait <a href='../index.html'>serde</a>::<wbr><a href='index.html'>ser</a>::<wbr><a class="trait" href=''>Serializer</a></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'>&#x2212;</span>]
</a>
</span><a class='srclink' href='../../src/serde/ser/mod.rs.html#330-1541' title='goto source code'>[src]</a></span></h1>
<pre class='rust trait'>pub trait Serializer: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> {
type <a href='#associatedtype.Ok' class="type">Ok</a>;
type <a href='#associatedtype.Error' class="type">Error</a>: <a class="trait" href="../../serde/ser/trait.Error.html" title="trait serde::ser::Error">Error</a>;
type <a href='#associatedtype.SerializeSeq' class="type">SerializeSeq</a>: <a class="trait" href="../../serde/ser/trait.SerializeSeq.html" title="trait serde::ser::SerializeSeq">SerializeSeq</a>&lt;Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
type <a href='#associatedtype.SerializeTuple' class="type">SerializeTuple</a>: <a class="trait" href="../../serde/ser/trait.SerializeTuple.html" title="trait serde::ser::SerializeTuple">SerializeTuple</a>&lt;Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
type <a href='#associatedtype.SerializeTupleStruct' class="type">SerializeTupleStruct</a>: <a class="trait" href="../../serde/ser/trait.SerializeTupleStruct.html" title="trait serde::ser::SerializeTupleStruct">SerializeTupleStruct</a>&lt;Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
type <a href='#associatedtype.SerializeTupleVariant' class="type">SerializeTupleVariant</a>: <a class="trait" href="../../serde/ser/trait.SerializeTupleVariant.html" title="trait serde::ser::SerializeTupleVariant">SerializeTupleVariant</a>&lt;Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
type <a href='#associatedtype.SerializeMap' class="type">SerializeMap</a>: <a class="trait" href="../../serde/ser/trait.SerializeMap.html" title="trait serde::ser::SerializeMap">SerializeMap</a>&lt;Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
type <a href='#associatedtype.SerializeStruct' class="type">SerializeStruct</a>: <a class="trait" href="../../serde/ser/trait.SerializeStruct.html" title="trait serde::ser::SerializeStruct">SerializeStruct</a>&lt;Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
type <a href='#associatedtype.SerializeStructVariant' class="type">SerializeStructVariant</a>: <a class="trait" href="../../serde/ser/trait.SerializeStructVariant.html" title="trait serde::ser::SerializeStructVariant">SerializeStructVariant</a>&lt;Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
fn <a href='#tymethod.serialize_bool' class='fnname'>serialize_bool</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_i8' class='fnname'>serialize_i8</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i8.html">i8</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_i16' class='fnname'>serialize_i16</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i16.html">i16</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_i32' class='fnname'>serialize_i32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i32.html">i32</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_i64' class='fnname'>serialize_i64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i64.html">i64</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_u8' class='fnname'>serialize_u8</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_u16' class='fnname'>serialize_u16</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u16.html">u16</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_u32' class='fnname'>serialize_u32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_u64' class='fnname'>serialize_u64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u64.html">u64</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_f32' class='fnname'>serialize_f32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f32.html">f32</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_f64' class='fnname'>serialize_f64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f64.html">f64</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_char' class='fnname'>serialize_char</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.char.html">char</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_str' class='fnname'>serialize_str</a>(self, v: &amp;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_bytes' class='fnname'>serialize_bytes</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&amp;[</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_none' class='fnname'>serialize_none</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;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_some' class='fnname'>serialize_some</a>&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>T<br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a></span>;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_unit' class='fnname'>serialize_unit</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;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_unit_struct' class='fnname'>serialize_unit_struct</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a><br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_unit_variant' class='fnname'>serialize_unit_variant</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;variant: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a><br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_newtype_struct' class='fnname'>serialize_newtype_struct</a>&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>T<br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a></span>;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_newtype_variant' class='fnname'>serialize_newtype_variant</a>&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;variant: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>T<br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a></span>;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_seq' class='fnname'>serialize_seq</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len: <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeSeq" title="type serde::Serializer::SerializeSeq">SerializeSeq</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_tuple' class='fnname'>serialize_tuple</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeTuple" title="type serde::Serializer::SerializeTuple">SerializeTuple</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_tuple_struct' class='fnname'>serialize_tuple_struct</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeTupleStruct" title="type serde::Serializer::SerializeTupleStruct">SerializeTupleStruct</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_tuple_variant' class='fnname'>serialize_tuple_variant</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;variant: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeTupleVariant" title="type serde::Serializer::SerializeTupleVariant">SerializeTupleVariant</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_map' class='fnname'>serialize_map</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len: <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeMap" title="type serde::Serializer::SerializeMap">SerializeMap</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_struct' class='fnname'>serialize_struct</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeStruct" title="type serde::Serializer::SerializeStruct">SerializeStruct</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_struct_variant' class='fnname'>serialize_struct_variant</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;variant: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeStructVariant" title="type serde::Serializer::SerializeStructVariant">SerializeStructVariant</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;;
fn <a href='#method.collect_seq' class='fnname'>collect_seq</a>&lt;I&gt;(self, iter: I) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html" title="trait core::iter::traits::IntoIterator">IntoIterator</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;I as <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html" title="trait core::iter::traits::IntoIterator">IntoIterator</a>&gt;::<a class="type" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html#associatedtype.Item" title="type core::iter::traits::IntoIterator::Item">Item</a>: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a></span>,
{ ... }
<div class='item-spacer'></div> fn <a href='#method.collect_map' class='fnname'>collect_map</a>&lt;K, V, I&gt;(self, iter: I) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;K: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;V: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html" title="trait core::iter::traits::IntoIterator">IntoIterator</a>&lt;Item = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a>K, V<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a>&gt;</span>,
{ ... }
<div class='item-spacer'></div> fn <a href='#method.collect_str' class='fnname'>collect_str</a>&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(self, value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>T) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a></span>,
{ ... }
<div class='item-spacer'></div> fn <a href='#method.is_human_readable' class='fnname'>is_human_readable</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a> { ... }
}</pre><div class='docblock'><p>A <strong>data format</strong> that can serialize any data structure supported by Serde.</p>
<p>The role of this trait is to define the serialization half of the <a href="https://serde.rs/data-model.html">Serde
data model</a>, which is a way to categorize every Rust data structure into one
of 29 possible types. Each method of the <code>Serializer</code> trait corresponds to
one of the types of the data model.</p>
<p>Implementations of <code>Serialize</code> map themselves into this data model by
invoking exactly one of the <code>Serializer</code> methods.</p>
<p>The types that make up the Serde data model are:</p>
<ul>
<li><strong>14 primitive types</strong>
<ul>
<li>bool</li>
<li>i8, i16, i32, i64, i128</li>
<li>u8, u16, u32, u64, u128</li>
<li>f32, f64</li>
<li>char</li>
</ul>
</li>
<li><strong>string</strong>
<ul>
<li>UTF-8 bytes with a length and no null terminator.</li>
<li>When serializing, all strings are handled equally. When deserializing,
there are three flavors of strings: transient, owned, and borrowed.</li>
</ul>
</li>
<li><strong>byte array</strong> - [u8]
<ul>
<li>Similar to strings, during deserialization byte arrays can be
transient, owned, or borrowed.</li>
</ul>
</li>
<li><strong>option</strong>
<ul>
<li>Either none or some value.</li>
</ul>
</li>
<li><strong>unit</strong>
<ul>
<li>The type of <code>()</code> in Rust. It represents an anonymous value containing
no data.</li>
</ul>
</li>
<li><strong>unit_struct</strong>
<ul>
<li>For example <code>struct Unit</code> or <code>PhantomData&lt;T&gt;</code>. It represents a named
value containing no data.</li>
</ul>
</li>
<li><strong>unit_variant</strong>
<ul>
<li>For example the <code>E::A</code> and <code>E::B</code> in <code>enum E { A, B }</code>.</li>
</ul>
</li>
<li><strong>newtype_struct</strong>
<ul>
<li>For example <code>struct Millimeters(u8)</code>.</li>
</ul>
</li>
<li><strong>newtype_variant</strong>
<ul>
<li>For example the <code>E::N</code> in <code>enum E { N(u8) }</code>.</li>
</ul>
</li>
<li><strong>seq</strong>
<ul>
<li>A variably sized heterogeneous sequence of values, for example
<code>Vec&lt;T&gt;</code> or <code>HashSet&lt;T&gt;</code>. When serializing, the length may or may not
be known before iterating through all the data. When deserializing,
the length is determined by looking at the serialized data.</li>
</ul>
</li>
<li><strong>tuple</strong>
<ul>
<li>A statically sized heterogeneous sequence of values for which the
length will be known at deserialization time without looking at the
serialized data, for example <code>(u8,)</code> or <code>(String, u64, Vec&lt;T&gt;)</code> or
<code>[u64; 10]</code>.</li>
</ul>
</li>
<li><strong>tuple_struct</strong>
<ul>
<li>A named tuple, for example <code>struct Rgb(u8, u8, u8)</code>.</li>
</ul>
</li>
<li><strong>tuple_variant</strong>
<ul>
<li>For example the <code>E::T</code> in <code>enum E { T(u8, u8) }</code>.</li>
</ul>
</li>
<li><strong>map</strong>
<ul>
<li>A heterogeneous key-value pairing, for example <code>BTreeMap&lt;K, V&gt;</code>.</li>
</ul>
</li>
<li><strong>struct</strong>
<ul>
<li>A heterogeneous key-value pairing in which the keys are strings and
will be known at deserialization time without looking at the
serialized data, for example <code>struct S { r: u8, g: u8, b: u8 }</code>.</li>
</ul>
</li>
<li><strong>struct_variant</strong>
<ul>
<li>For example the <code>E::S</code> in <code>enum E { S { r: u8, g: u8, b: u8 } }</code>.</li>
</ul>
</li>
</ul>
<p>Many Serde serializers produce text or binary data as output, for example
JSON or Bincode. This is not a requirement of the <code>Serializer</code> trait, and
there are serializers that do not produce text or binary output. One example
is the <code>serde_json::value::Serializer</code> (distinct from the main <code>serde_json</code>
serializer) that produces a <code>serde_json::Value</code> data structure in memory as
output.</p>
<h1 id="example-implementation" class="section-header"><a href="#example-implementation">Example implementation</a></h1>
<p>The <a href="https://serde.rs/data-format.html">example data format</a> presented on the website contains example code for
a basic JSON <code>Serializer</code>.</p>
</div>
<h2 id='associated-types' class='small-section-header'>
Associated Types<a href='#associated-types' class='anchor'></a>
</h2>
<div class='methods'>
<h3 id='associatedtype.Ok' class='method'><span id='Ok.t' class='invisible'><code>type <a href='#associatedtype.Ok' class="type">Ok</a></code></span></h3><div class='docblock'><p>The output type produced by this <code>Serializer</code> during successful
serialization. Most serializers that produce text or binary output
should set <code>Ok = ()</code> and serialize into an <a href="https://doc.rust-lang.org/std/io/trait.Write.html"><code>io::Write</code></a> or buffer
contained within the <code>Serializer</code> instance. Serializers that build
in-memory data structures may be simplified by using <code>Ok</code> to propagate
the data structure around.</p>
</div><h3 id='associatedtype.Error' class='method'><span id='Error.t' class='invisible'><code>type <a href='#associatedtype.Error' class="type">Error</a>: <a class="trait" href="../../serde/ser/trait.Error.html" title="trait serde::ser::Error">Error</a></code></span></h3><div class='docblock'><p>The error type when some error occurs during serialization.</p>
</div><h3 id='associatedtype.SerializeSeq' class='method'><span id='SerializeSeq.t' class='invisible'><code>type <a href='#associatedtype.SerializeSeq' class="type">SerializeSeq</a>: <a class="trait" href="../../serde/ser/trait.SerializeSeq.html" title="trait serde::ser::SerializeSeq">SerializeSeq</a>&lt;Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_seq"><code>serialize_seq</code></a> for serializing the content of the
sequence.</p>
</div><h3 id='associatedtype.SerializeTuple' class='method'><span id='SerializeTuple.t' class='invisible'><code>type <a href='#associatedtype.SerializeTuple' class="type">SerializeTuple</a>: <a class="trait" href="../../serde/ser/trait.SerializeTuple.html" title="trait serde::ser::SerializeTuple">SerializeTuple</a>&lt;Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_tuple"><code>serialize_tuple</code></a> for serializing the content of
the tuple.</p>
</div><h3 id='associatedtype.SerializeTupleStruct' class='method'><span id='SerializeTupleStruct.t' class='invisible'><code>type <a href='#associatedtype.SerializeTupleStruct' class="type">SerializeTupleStruct</a>: <a class="trait" href="../../serde/ser/trait.SerializeTupleStruct.html" title="trait serde::ser::SerializeTupleStruct">SerializeTupleStruct</a>&lt;Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_tuple_struct"><code>serialize_tuple_struct</code></a> for serializing the
content of the tuple struct.</p>
</div><h3 id='associatedtype.SerializeTupleVariant' class='method'><span id='SerializeTupleVariant.t' class='invisible'><code>type <a href='#associatedtype.SerializeTupleVariant' class="type">SerializeTupleVariant</a>: <a class="trait" href="../../serde/ser/trait.SerializeTupleVariant.html" title="trait serde::ser::SerializeTupleVariant">SerializeTupleVariant</a>&lt;Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_tuple_variant"><code>serialize_tuple_variant</code></a> for serializing the
content of the tuple variant.</p>
</div><h3 id='associatedtype.SerializeMap' class='method'><span id='SerializeMap.t' class='invisible'><code>type <a href='#associatedtype.SerializeMap' class="type">SerializeMap</a>: <a class="trait" href="../../serde/ser/trait.SerializeMap.html" title="trait serde::ser::SerializeMap">SerializeMap</a>&lt;Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_map"><code>serialize_map</code></a> for serializing the content of the
map.</p>
</div><h3 id='associatedtype.SerializeStruct' class='method'><span id='SerializeStruct.t' class='invisible'><code>type <a href='#associatedtype.SerializeStruct' class="type">SerializeStruct</a>: <a class="trait" href="../../serde/ser/trait.SerializeStruct.html" title="trait serde::ser::SerializeStruct">SerializeStruct</a>&lt;Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_struct"><code>serialize_struct</code></a> for serializing the content of
the struct.</p>
</div><h3 id='associatedtype.SerializeStructVariant' class='method'><span id='SerializeStructVariant.t' class='invisible'><code>type <a href='#associatedtype.SerializeStructVariant' class="type">SerializeStructVariant</a>: <a class="trait" href="../../serde/ser/trait.SerializeStructVariant.html" title="trait serde::ser::SerializeStructVariant">SerializeStructVariant</a>&lt;Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_struct_variant"><code>serialize_struct_variant</code></a> for serializing the
content of the struct variant.</p>
</div></div>
<h2 id='required-methods' class='small-section-header'>
Required Methods<a href='#required-methods' class='anchor'></a>
</h2>
<div class='methods'>
<h3 id='tymethod.serialize_bool' class='method'><span id='serialize_bool.v' class='invisible'><code>fn <a href='#tymethod.serialize_bool' class='fnname'>serialize_bool</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize a <code>bool</code> value.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">bool</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_bool</span>(<span class="kw-2">*</span><span class="self">self</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_i8' class='method'><span id='serialize_i8.v' class='invisible'><code>fn <a href='#tymethod.serialize_i8' class='fnname'>serialize_i8</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i8.html">i8</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize an <code>i8</code> value.</p>
<p>If the format does not differentiate between <code>i8</code> and <code>i64</code>, a
reasonable implementation would be to cast the value to <code>i64</code> and
forward to <code>serialize_i64</code>.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">i8</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_i8</span>(<span class="kw-2">*</span><span class="self">self</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_i16' class='method'><span id='serialize_i16.v' class='invisible'><code>fn <a href='#tymethod.serialize_i16' class='fnname'>serialize_i16</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i16.html">i16</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize an <code>i16</code> value.</p>
<p>If the format does not differentiate between <code>i16</code> and <code>i64</code>, a
reasonable implementation would be to cast the value to <code>i64</code> and
forward to <code>serialize_i64</code>.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">i16</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_i16</span>(<span class="kw-2">*</span><span class="self">self</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_i32' class='method'><span id='serialize_i32.v' class='invisible'><code>fn <a href='#tymethod.serialize_i32' class='fnname'>serialize_i32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i32.html">i32</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize an <code>i32</code> value.</p>
<p>If the format does not differentiate between <code>i32</code> and <code>i64</code>, a
reasonable implementation would be to cast the value to <code>i64</code> and
forward to <code>serialize_i64</code>.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">i32</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_i32</span>(<span class="kw-2">*</span><span class="self">self</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_i64' class='method'><span id='serialize_i64.v' class='invisible'><code>fn <a href='#tymethod.serialize_i64' class='fnname'>serialize_i64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i64.html">i64</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize an <code>i64</code> value.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">i64</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_i64</span>(<span class="kw-2">*</span><span class="self">self</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_u8' class='method'><span id='serialize_u8.v' class='invisible'><code>fn <a href='#tymethod.serialize_u8' class='fnname'>serialize_u8</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize a <code>u8</code> value.</p>
<p>If the format does not differentiate between <code>u8</code> and <code>u64</code>, a
reasonable implementation would be to cast the value to <code>u64</code> and
forward to <code>serialize_u64</code>.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">u8</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_u8</span>(<span class="kw-2">*</span><span class="self">self</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_u16' class='method'><span id='serialize_u16.v' class='invisible'><code>fn <a href='#tymethod.serialize_u16' class='fnname'>serialize_u16</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u16.html">u16</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize a <code>u16</code> value.</p>
<p>If the format does not differentiate between <code>u16</code> and <code>u64</code>, a
reasonable implementation would be to cast the value to <code>u64</code> and
forward to <code>serialize_u64</code>.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">u16</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_u16</span>(<span class="kw-2">*</span><span class="self">self</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_u32' class='method'><span id='serialize_u32.v' class='invisible'><code>fn <a href='#tymethod.serialize_u32' class='fnname'>serialize_u32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize a <code>u32</code> value.</p>
<p>If the format does not differentiate between <code>u32</code> and <code>u64</code>, a
reasonable implementation would be to cast the value to <code>u64</code> and
forward to <code>serialize_u64</code>.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">u32</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_u32</span>(<span class="kw-2">*</span><span class="self">self</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_u64' class='method'><span id='serialize_u64.v' class='invisible'><code>fn <a href='#tymethod.serialize_u64' class='fnname'>serialize_u64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u64.html">u64</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize a <code>u64</code> value.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">u64</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_u64</span>(<span class="kw-2">*</span><span class="self">self</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_f32' class='method'><span id='serialize_f32.v' class='invisible'><code>fn <a href='#tymethod.serialize_f32' class='fnname'>serialize_f32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f32.html">f32</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize an <code>f32</code> value.</p>
<p>If the format does not differentiate between <code>f32</code> and <code>f64</code>, a
reasonable implementation would be to cast the value to <code>f64</code> and
forward to <code>serialize_f64</code>.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">f32</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_f32</span>(<span class="kw-2">*</span><span class="self">self</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_f64' class='method'><span id='serialize_f64.v' class='invisible'><code>fn <a href='#tymethod.serialize_f64' class='fnname'>serialize_f64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f64.html">f64</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize an <code>f64</code> value.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">f64</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_f64</span>(<span class="kw-2">*</span><span class="self">self</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_char' class='method'><span id='serialize_char.v' class='invisible'><code>fn <a href='#tymethod.serialize_char' class='fnname'>serialize_char</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.char.html">char</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize a character.</p>
<p>If the format does not support characters, it is reasonable to serialize
it as a single element <code>str</code> or a <code>u32</code>.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">char</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_char</span>(<span class="kw-2">*</span><span class="self">self</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_str' class='method'><span id='serialize_str.v' class='invisible'><code>fn <a href='#tymethod.serialize_str' class='fnname'>serialize_str</a>(self, v: &amp;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize a <code>&amp;str</code>.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">str</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_str</span>(<span class="self">self</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_bytes' class='method'><span id='serialize_bytes.v' class='invisible'><code>fn <a href='#tymethod.serialize_bytes' class='fnname'>serialize_bytes</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&amp;[</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize a chunk of raw byte data.</p>
<p>Enables serializers to serialize byte slices more compactly or more
efficiently than other types of slices. If no efficient implementation
is available, a reasonable implementation would be to forward to
<code>serialize_seq</code>. If forwarded, the implementation looks usually just
like this:</p>
<pre class="rust rust-example-rendered">
<span class="kw">fn</span> <span class="ident">serialize_bytes</span>(<span class="self">self</span>, <span class="ident">v</span>: <span class="kw-2">&amp;</span>[<span class="ident">u8</span>]) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="self">Self</span>::<span class="prelude-val">Ok</span>, <span class="self">Self</span>::<span class="ident">Error</span><span class="op">&gt;</span> {
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">seq</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">serialize_seq</span>(<span class="prelude-val">Some</span>(<span class="ident">v</span>.<span class="ident">len</span>()))<span class="question-mark">?</span>;
<span class="kw">for</span> <span class="ident">b</span> <span class="kw">in</span> <span class="ident">v</span> {
<span class="ident">seq</span>.<span class="ident">serialize_element</span>(<span class="ident">b</span>)<span class="question-mark">?</span>;
}
<span class="ident">seq</span>.<span class="ident">end</span>()
}</pre>
</div><h3 id='tymethod.serialize_none' class='method'><span id='serialize_none.v' class='invisible'><code>fn <a href='#tymethod.serialize_none' class='fnname'>serialize_none</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;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize a <a href="https://doc.rust-lang.org/std/option/enum.Option.html#variant.None"><code>None</code></a> value.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">T</span>: <span class="ident">Serialize</span>,
{
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
<span class="prelude-val">Some</span>(<span class="kw-2">ref</span> <span class="ident">value</span>) <span class="op">=&gt;</span> <span class="ident">serializer</span>.<span class="ident">serialize_some</span>(<span class="ident">value</span>),
<span class="prelude-val">None</span> <span class="op">=&gt;</span> <span class="ident">serializer</span>.<span class="ident">serialize_none</span>(),
}
}
}</pre>
</div><h3 id='tymethod.serialize_some' class='method'><span id='serialize_some.v' class='invisible'><code>fn <a href='#tymethod.serialize_some' class='fnname'>serialize_some</a>&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(self, value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>T) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,&nbsp;</span></code></span></h3><div class='docblock'><p>Serialize a <a href="https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some"><code>Some(T)</code></a> value.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">T</span>: <span class="ident">Serialize</span>,
{
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
<span class="prelude-val">Some</span>(<span class="kw-2">ref</span> <span class="ident">value</span>) <span class="op">=&gt;</span> <span class="ident">serializer</span>.<span class="ident">serialize_some</span>(<span class="ident">value</span>),
<span class="prelude-val">None</span> <span class="op">=&gt;</span> <span class="ident">serializer</span>.<span class="ident">serialize_none</span>(),
}
}
}</pre>
</div><h3 id='tymethod.serialize_unit' class='method'><span id='serialize_unit.v' class='invisible'><code>fn <a href='#tymethod.serialize_unit' class='fnname'>serialize_unit</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;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize a <code>()</code> value.</p>
<pre class="rust rust-example-rendered">
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> () {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_unit</span>()
}
}</pre>
</div><h3 id='tymethod.serialize_unit_struct' class='method'><span id='serialize_unit_struct.v' class='invisible'><code>fn <a href='#tymethod.serialize_unit_struct' class='fnname'>serialize_unit_struct</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a><br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize a unit struct like <code>struct Unit</code> or <code>PhantomData&lt;T&gt;</code>.</p>
<p>A reasonable implementation would be to forward to <code>serialize_unit</code>.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
<span class="kw">struct</span> <span class="ident">Nothing</span>;
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">Nothing</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_unit_struct</span>(<span class="string">&quot;Nothing&quot;</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_unit_variant' class='method'><span id='serialize_unit_variant.v' class='invisible'><code>fn <a href='#tymethod.serialize_unit_variant' class='fnname'>serialize_unit_variant</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;variant: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a><br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Serialize a unit variant like <code>E::A</code> in <code>enum E { A, B }</code>.</p>
<p>The <code>name</code> is the name of the enum, the <code>variant_index</code> is the index of
this variant within the enum, and the <code>variant</code> is the name of the
variant.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
<span class="kw">enum</span> <span class="ident">E</span> {
<span class="ident">A</span>,
<span class="ident">B</span>,
}
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">E</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
<span class="ident">E</span>::<span class="ident">A</span> <span class="op">=&gt;</span> <span class="ident">serializer</span>.<span class="ident">serialize_unit_variant</span>(<span class="string">&quot;E&quot;</span>, <span class="number">0</span>, <span class="string">&quot;A&quot;</span>),
<span class="ident">E</span>::<span class="ident">B</span> <span class="op">=&gt;</span> <span class="ident">serializer</span>.<span class="ident">serialize_unit_variant</span>(<span class="string">&quot;E&quot;</span>, <span class="number">1</span>, <span class="string">&quot;B&quot;</span>),
}
}
}</pre>
</div><h3 id='tymethod.serialize_newtype_struct' class='method'><span id='serialize_newtype_struct.v' class='invisible'><code>fn <a href='#tymethod.serialize_newtype_struct' class='fnname'>serialize_newtype_struct</a>&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>T<br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,&nbsp;</span></code></span></h3><div class='docblock'><p>Serialize a newtype struct like <code>struct Millimeters(u8)</code>.</p>
<p>Serializers are encouraged to treat newtype structs as insignificant
wrappers around the data they contain. A reasonable implementation would
be to forward to <code>value.serialize(self)</code>.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
<span class="kw">struct</span> <span class="ident">Millimeters</span>(<span class="ident">u8</span>);
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">Millimeters</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">serialize_newtype_struct</span>(<span class="string">&quot;Millimeters&quot;</span>, <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="number">0</span>)
}
}</pre>
</div><h3 id='tymethod.serialize_newtype_variant' class='method'><span id='serialize_newtype_variant.v' class='invisible'><code>fn <a href='#tymethod.serialize_newtype_variant' class='fnname'>serialize_newtype_variant</a>&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;variant: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>T<br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,&nbsp;</span></code></span></h3><div class='docblock'><p>Serialize a newtype variant like <code>E::N</code> in <code>enum E { N(u8) }</code>.</p>
<p>The <code>name</code> is the name of the enum, the <code>variant_index</code> is the index of
this variant within the enum, and the <code>variant</code> is the name of the
variant. The <code>value</code> is the data contained within this newtype variant.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
<span class="kw">enum</span> <span class="ident">E</span> {
<span class="ident">M</span>(<span class="ident">String</span>),
<span class="ident">N</span>(<span class="ident">u8</span>),
}
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">E</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
<span class="ident">E</span>::<span class="ident">M</span>(<span class="kw-2">ref</span> <span class="ident">s</span>) <span class="op">=&gt;</span> <span class="ident">serializer</span>.<span class="ident">serialize_newtype_variant</span>(<span class="string">&quot;E&quot;</span>, <span class="number">0</span>, <span class="string">&quot;M&quot;</span>, <span class="ident">s</span>),
<span class="ident">E</span>::<span class="ident">N</span>(<span class="ident">n</span>) <span class="op">=&gt;</span> <span class="ident">serializer</span>.<span class="ident">serialize_newtype_variant</span>(<span class="string">&quot;E&quot;</span>, <span class="number">1</span>, <span class="string">&quot;N&quot;</span>, <span class="kw-2">&amp;</span><span class="ident">n</span>),
}
}
}</pre>
</div><h3 id='tymethod.serialize_seq' class='method'><span id='serialize_seq.v' class='invisible'><code>fn <a href='#tymethod.serialize_seq' class='fnname'>serialize_seq</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;len: <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>&gt;<br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeSeq" title="type serde::Serializer::SerializeSeq">SerializeSeq</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Begin to serialize a variably sized sequence. This call must be
followed by zero or more calls to <code>serialize_element</code>, then a call to
<code>end</code>.</p>
<p>The argument is the number of elements in the sequence, which may or may
not be computable before the sequence is iterated. Some serializers only
support sequences whose length is known up front.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>, <span class="ident">SerializeSeq</span>};
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">T</span>: <span class="ident">Serialize</span>,
{
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">seq</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_seq</span>(<span class="prelude-val">Some</span>(<span class="self">self</span>.<span class="ident">len</span>()))<span class="question-mark">?</span>;
<span class="kw">for</span> <span class="ident">element</span> <span class="kw">in</span> <span class="self">self</span> {
<span class="ident">seq</span>.<span class="ident">serialize_element</span>(<span class="ident">element</span>)<span class="question-mark">?</span>;
}
<span class="ident">seq</span>.<span class="ident">end</span>()
}
}</pre>
</div><h3 id='tymethod.serialize_tuple' class='method'><span id='serialize_tuple.v' class='invisible'><code>fn <a href='#tymethod.serialize_tuple' class='fnname'>serialize_tuple</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeTuple" title="type serde::Serializer::SerializeTuple">SerializeTuple</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Begin to serialize a statically sized sequence whose length will be
known at deserialization time without looking at the serialized data.
This call must be followed by zero or more calls to <code>serialize_element</code>,
then a call to <code>end</code>.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>, <span class="ident">SerializeTuple</span>};
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">A</span>, <span class="ident">B</span>, <span class="ident">C</span><span class="op">&gt;</span> <span class="ident">Serialize</span> <span class="kw">for</span> (<span class="ident">A</span>, <span class="ident">B</span>, <span class="ident">C</span>)
<span class="kw">where</span>
<span class="ident">A</span>: <span class="ident">Serialize</span>,
<span class="ident">B</span>: <span class="ident">Serialize</span>,
<span class="ident">C</span>: <span class="ident">Serialize</span>,
{
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">tup</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_tuple</span>(<span class="number">3</span>)<span class="question-mark">?</span>;
<span class="ident">tup</span>.<span class="ident">serialize_element</span>(<span class="kw-2">&amp;</span><span class="self">self</span>.<span class="number">0</span>)<span class="question-mark">?</span>;
<span class="ident">tup</span>.<span class="ident">serialize_element</span>(<span class="kw-2">&amp;</span><span class="self">self</span>.<span class="number">1</span>)<span class="question-mark">?</span>;
<span class="ident">tup</span>.<span class="ident">serialize_element</span>(<span class="kw-2">&amp;</span><span class="self">self</span>.<span class="number">2</span>)<span class="question-mark">?</span>;
<span class="ident">tup</span>.<span class="ident">end</span>()
}
}</pre>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">SerializeTuple</span>, <span class="ident">Serializer</span>};
<span class="kw">const</span> <span class="ident">VRAM_SIZE</span>: <span class="ident">usize</span> <span class="op">=</span> <span class="number">386</span>;
<span class="kw">struct</span> <span class="ident">Vram</span>([<span class="ident">u16</span>; <span class="ident">VRAM_SIZE</span>]);
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">Vram</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">seq</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_tuple</span>(<span class="ident">VRAM_SIZE</span>)<span class="question-mark">?</span>;
<span class="kw">for</span> <span class="ident">element</span> <span class="kw">in</span> <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="number">0</span>[..] {
<span class="ident">seq</span>.<span class="ident">serialize_element</span>(<span class="ident">element</span>)<span class="question-mark">?</span>;
}
<span class="ident">seq</span>.<span class="ident">end</span>()
}
}</pre>
</div><h3 id='tymethod.serialize_tuple_struct' class='method'><span id='serialize_tuple_struct.v' class='invisible'><code>fn <a href='#tymethod.serialize_tuple_struct' class='fnname'>serialize_tuple_struct</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeTupleStruct" title="type serde::Serializer::SerializeTupleStruct">SerializeTupleStruct</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Begin to serialize a tuple struct like <code>struct Rgb(u8, u8, u8)</code>. This
call must be followed by zero or more calls to <code>serialize_field</code>, then a
call to <code>end</code>.</p>
<p>The <code>name</code> is the name of the tuple struct and the <code>len</code> is the number
of data fields that will be serialized.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">SerializeTupleStruct</span>, <span class="ident">Serializer</span>};
<span class="kw">struct</span> <span class="ident">Rgb</span>(<span class="ident">u8</span>, <span class="ident">u8</span>, <span class="ident">u8</span>);
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">Rgb</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">ts</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_tuple_struct</span>(<span class="string">&quot;Rgb&quot;</span>, <span class="number">3</span>)<span class="question-mark">?</span>;
<span class="ident">ts</span>.<span class="ident">serialize_field</span>(<span class="kw-2">&amp;</span><span class="self">self</span>.<span class="number">0</span>)<span class="question-mark">?</span>;
<span class="ident">ts</span>.<span class="ident">serialize_field</span>(<span class="kw-2">&amp;</span><span class="self">self</span>.<span class="number">1</span>)<span class="question-mark">?</span>;
<span class="ident">ts</span>.<span class="ident">serialize_field</span>(<span class="kw-2">&amp;</span><span class="self">self</span>.<span class="number">2</span>)<span class="question-mark">?</span>;
<span class="ident">ts</span>.<span class="ident">end</span>()
}
}</pre>
</div><h3 id='tymethod.serialize_tuple_variant' class='method'><span id='serialize_tuple_variant.v' class='invisible'><code>fn <a href='#tymethod.serialize_tuple_variant' class='fnname'>serialize_tuple_variant</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;variant: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeTupleVariant" title="type serde::Serializer::SerializeTupleVariant">SerializeTupleVariant</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Begin to serialize a tuple variant like <code>E::T</code> in <code>enum E { T(u8, u8) }</code>. This call must be followed by zero or more calls to
<code>serialize_field</code>, then a call to <code>end</code>.</p>
<p>The <code>name</code> is the name of the enum, the <code>variant_index</code> is the index of
this variant within the enum, the <code>variant</code> is the name of the variant,
and the <code>len</code> is the number of data fields that will be serialized.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">SerializeTupleVariant</span>, <span class="ident">Serializer</span>};
<span class="kw">enum</span> <span class="ident">E</span> {
<span class="ident">T</span>(<span class="ident">u8</span>, <span class="ident">u8</span>),
<span class="ident">U</span>(<span class="ident">String</span>, <span class="ident">u32</span>, <span class="ident">u32</span>),
}
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">E</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
<span class="ident">E</span>::<span class="ident">T</span>(<span class="kw-2">ref</span> <span class="ident">a</span>, <span class="kw-2">ref</span> <span class="ident">b</span>) <span class="op">=&gt;</span> {
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">tv</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_tuple_variant</span>(<span class="string">&quot;E&quot;</span>, <span class="number">0</span>, <span class="string">&quot;T&quot;</span>, <span class="number">2</span>)<span class="question-mark">?</span>;
<span class="ident">tv</span>.<span class="ident">serialize_field</span>(<span class="ident">a</span>)<span class="question-mark">?</span>;
<span class="ident">tv</span>.<span class="ident">serialize_field</span>(<span class="ident">b</span>)<span class="question-mark">?</span>;
<span class="ident">tv</span>.<span class="ident">end</span>()
}
<span class="ident">E</span>::<span class="ident">U</span>(<span class="kw-2">ref</span> <span class="ident">a</span>, <span class="kw-2">ref</span> <span class="ident">b</span>, <span class="kw-2">ref</span> <span class="ident">c</span>) <span class="op">=&gt;</span> {
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">tv</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_tuple_variant</span>(<span class="string">&quot;E&quot;</span>, <span class="number">1</span>, <span class="string">&quot;U&quot;</span>, <span class="number">3</span>)<span class="question-mark">?</span>;
<span class="ident">tv</span>.<span class="ident">serialize_field</span>(<span class="ident">a</span>)<span class="question-mark">?</span>;
<span class="ident">tv</span>.<span class="ident">serialize_field</span>(<span class="ident">b</span>)<span class="question-mark">?</span>;
<span class="ident">tv</span>.<span class="ident">serialize_field</span>(<span class="ident">c</span>)<span class="question-mark">?</span>;
<span class="ident">tv</span>.<span class="ident">end</span>()
}
}
}
}</pre>
</div><h3 id='tymethod.serialize_map' class='method'><span id='serialize_map.v' class='invisible'><code>fn <a href='#tymethod.serialize_map' class='fnname'>serialize_map</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;len: <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>&gt;<br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeMap" title="type serde::Serializer::SerializeMap">SerializeMap</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Begin to serialize a map. This call must be followed by zero or more
calls to <code>serialize_key</code> and <code>serialize_value</code>, then a call to <code>end</code>.</p>
<p>The argument is the number of elements in the map, which may or may not
be computable before the map is iterated. Some serializers only support
maps whose length is known up front.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>, <span class="ident">SerializeMap</span>};
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">K</span>, <span class="ident">V</span><span class="op">&gt;</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">HashMap</span><span class="op">&lt;</span><span class="ident">K</span>, <span class="ident">V</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">K</span>: <span class="ident">Serialize</span>,
<span class="ident">V</span>: <span class="ident">Serialize</span>,
{
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">map</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_map</span>(<span class="prelude-val">Some</span>(<span class="self">self</span>.<span class="ident">len</span>()))<span class="question-mark">?</span>;
<span class="kw">for</span> (<span class="ident">k</span>, <span class="ident">v</span>) <span class="kw">in</span> <span class="self">self</span> {
<span class="ident">map</span>.<span class="ident">serialize_entry</span>(<span class="ident">k</span>, <span class="ident">v</span>)<span class="question-mark">?</span>;
}
<span class="ident">map</span>.<span class="ident">end</span>()
}
}</pre>
</div><h3 id='tymethod.serialize_struct' class='method'><span id='serialize_struct.v' class='invisible'><code>fn <a href='#tymethod.serialize_struct' class='fnname'>serialize_struct</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeStruct" title="type serde::Serializer::SerializeStruct">SerializeStruct</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Begin to serialize a struct like <code>struct Rgb { r: u8, g: u8, b: u8 }</code>.
This call must be followed by zero or more calls to <code>serialize_field</code>,
then a call to <code>end</code>.</p>
<p>The <code>name</code> is the name of the struct and the <code>len</code> is the number of
data fields that will be serialized.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">SerializeStruct</span>, <span class="ident">Serializer</span>};
<span class="kw">struct</span> <span class="ident">Rgb</span> {
<span class="ident">r</span>: <span class="ident">u8</span>,
<span class="ident">g</span>: <span class="ident">u8</span>,
<span class="ident">b</span>: <span class="ident">u8</span>,
}
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">Rgb</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rgb</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_struct</span>(<span class="string">&quot;Rgb&quot;</span>, <span class="number">3</span>)<span class="question-mark">?</span>;
<span class="ident">rgb</span>.<span class="ident">serialize_field</span>(<span class="string">&quot;r&quot;</span>, <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">r</span>)<span class="question-mark">?</span>;
<span class="ident">rgb</span>.<span class="ident">serialize_field</span>(<span class="string">&quot;g&quot;</span>, <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">g</span>)<span class="question-mark">?</span>;
<span class="ident">rgb</span>.<span class="ident">serialize_field</span>(<span class="string">&quot;b&quot;</span>, <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">b</span>)<span class="question-mark">?</span>;
<span class="ident">rgb</span>.<span class="ident">end</span>()
}
}</pre>
</div><h3 id='tymethod.serialize_struct_variant' class='method'><span id='serialize_struct_variant.v' class='invisible'><code>fn <a href='#tymethod.serialize_struct_variant' class='fnname'>serialize_struct_variant</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;name: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;variant: &amp;'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br>&nbsp;&nbsp;&nbsp;&nbsp;len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeStructVariant" title="type serde::Serializer::SerializeStructVariant">SerializeStructVariant</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt;</code></span></h3><div class='docblock'><p>Begin to serialize a struct variant like <code>E::S</code> in <code>enum E { S { r: u8, g: u8, b: u8 } }</code>. This call must be followed by zero or more calls to
<code>serialize_field</code>, then a call to <code>end</code>.</p>
<p>The <code>name</code> is the name of the enum, the <code>variant_index</code> is the index of
this variant within the enum, the <code>variant</code> is the name of the variant,
and the <code>len</code> is the number of data fields that will be serialized.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">SerializeStructVariant</span>, <span class="ident">Serializer</span>};
<span class="kw">enum</span> <span class="ident">E</span> {
<span class="ident">S</span> { <span class="ident">r</span>: <span class="ident">u8</span>, <span class="ident">g</span>: <span class="ident">u8</span>, <span class="ident">b</span>: <span class="ident">u8</span> },
}
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">E</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
<span class="ident">E</span>::<span class="ident">S</span> {
<span class="kw-2">ref</span> <span class="ident">r</span>,
<span class="kw-2">ref</span> <span class="ident">g</span>,
<span class="kw-2">ref</span> <span class="ident">b</span>,
} <span class="op">=&gt;</span> {
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">sv</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_struct_variant</span>(<span class="string">&quot;E&quot;</span>, <span class="number">0</span>, <span class="string">&quot;S&quot;</span>, <span class="number">3</span>)<span class="question-mark">?</span>;
<span class="ident">sv</span>.<span class="ident">serialize_field</span>(<span class="string">&quot;r&quot;</span>, <span class="ident">r</span>)<span class="question-mark">?</span>;
<span class="ident">sv</span>.<span class="ident">serialize_field</span>(<span class="string">&quot;g&quot;</span>, <span class="ident">g</span>)<span class="question-mark">?</span>;
<span class="ident">sv</span>.<span class="ident">serialize_field</span>(<span class="string">&quot;b&quot;</span>, <span class="ident">b</span>)<span class="question-mark">?</span>;
<span class="ident">sv</span>.<span class="ident">end</span>()
}
}
}
}</pre>
</div></div>
<h2 id='provided-methods' class='small-section-header'>
Provided Methods<a href='#provided-methods' class='anchor'></a>
</h2>
<div class='methods'>
<h3 id='method.collect_seq' class='method'><span id='collect_seq.v' class='invisible'><code>fn <a href='#method.collect_seq' class='fnname'>collect_seq</a>&lt;I&gt;(self, iter: I) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html" title="trait core::iter::traits::IntoIterator">IntoIterator</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;I as <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html" title="trait core::iter::traits::IntoIterator">IntoIterator</a>&gt;::<a class="type" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html#associatedtype.Item" title="type core::iter::traits::IntoIterator::Item">Item</a>: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,&nbsp;</span></code></span></h3><div class='docblock'><p>Collect an iterator as a sequence.</p>
<p>The default implementation serializes each item yielded by the iterator
using <a href="#tymethod.serialize_seq"><code>serialize_seq</code></a>. Implementors should not need to override this
method.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
<span class="kw">struct</span> <span class="ident">SecretlyOneHigher</span> {
<span class="ident">data</span>: <span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">i32</span><span class="op">&gt;</span>,
}
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">SecretlyOneHigher</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">collect_seq</span>(<span class="self">self</span>.<span class="ident">data</span>.<span class="ident">iter</span>().<span class="ident">map</span>(<span class="op">|</span><span class="ident">x</span><span class="op">|</span> <span class="ident">x</span> <span class="op">+</span> <span class="number">1</span>))
}
}</pre>
</div><h3 id='method.collect_map' class='method'><span id='collect_map.v' class='invisible'><code>fn <a href='#method.collect_map' class='fnname'>collect_map</a>&lt;K, V, I&gt;(self, iter: I) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;K: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;V: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html" title="trait core::iter::traits::IntoIterator">IntoIterator</a>&lt;Item = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a>K, V<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a>&gt;,&nbsp;</span></code></span></h3><div class='docblock'><p>Collect an iterator as a map.</p>
<p>The default implementation serializes each pair yielded by the iterator
using <a href="#tymethod.serialize_map"><code>serialize_map</code></a>. Implementors should not need to override this
method.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BTreeSet</span>;
<span class="kw">struct</span> <span class="ident">MapToUnit</span> {
<span class="ident">keys</span>: <span class="ident">BTreeSet</span><span class="op">&lt;</span><span class="ident">i32</span><span class="op">&gt;</span>,
}
<span class="comment">// Serializes as a map in which the values are all unit.</span>
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">MapToUnit</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">collect_map</span>(<span class="self">self</span>.<span class="ident">keys</span>.<span class="ident">iter</span>().<span class="ident">map</span>(<span class="op">|</span><span class="ident">k</span><span class="op">|</span> (<span class="ident">k</span>, ())))
}
}</pre>
</div><h3 id='method.collect_str' class='method'><span id='collect_str.v' class='invisible'><code>fn <a href='#method.collect_str' class='fnname'>collect_str</a>&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(self, value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>T) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a>,&nbsp;</span></code></span></h3><div class='docblock'><p>Serialize a string produced by an implementation of <code>Display</code>.</p>
<p>The default implementation builds a heap-allocated <a href="https://doc.rust-lang.org/std/string/struct.String.html"><code>String</code></a> and
delegates to <a href="#tymethod.serialize_str"><code>serialize_str</code></a>. Serializers are encouraged to provide a
more efficient implementation if possible.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">DateTime</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="ident">serializer</span>.<span class="ident">collect_str</span>(<span class="kw-2">&amp;</span><span class="macro">format_args</span><span class="macro">!</span>(<span class="string">&quot;{:?}{:?}&quot;</span>,
<span class="self">self</span>.<span class="ident">naive_local</span>(),
<span class="self">self</span>.<span class="ident">offset</span>()))
}
}</pre>
</div><h3 id='method.is_human_readable' class='method'><span id='is_human_readable.v' class='invisible'><code>fn <a href='#method.is_human_readable' class='fnname'>is_human_readable</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a></code></span></h3><div class='docblock'><p>Determine whether <code>Serialize</code> implementations should serialize in
human-readable form.</p>
<p>Some types have a human-readable form that may be somewhat expensive to
construct, as well as a binary form that is compact and efficient.
Generally text-based formats like JSON and YAML will prefer to use the
human-readable one and binary formats like Bincode will prefer the
compact one.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">Timestamp</span> {
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Serializer</span>,
{
<span class="kw">if</span> <span class="ident">serializer</span>.<span class="ident">is_human_readable</span>() {
<span class="comment">// Serialize to a human-readable string &quot;2015-05-15T17:01:00Z&quot;.</span>
<span class="self">self</span>.<span class="ident">to_string</span>().<span class="ident">serialize</span>(<span class="ident">serializer</span>)
} <span class="kw">else</span> {
<span class="comment">// Serialize to a compact binary representation.</span>
<span class="self">self</span>.<span class="ident">seconds_since_epoch</span>().<span class="ident">serialize</span>(<span class="ident">serializer</span>)
}
}
}</pre>
<p>The default implementation of this method returns <code>true</code>. Data formats
may override this to <code>false</code> to request a compact form for types that
support one. Note that modifying this method to change a format from
human-readable to compact or vice versa should be regarded as a breaking
change, as a value serialized in human-readable mode is not required to
deserialize from the same data in compact mode.</p>
</div></div>
<h2 id='implementors' class='small-section-header'>
Implementors<a href='#implementors' class='anchor'></a>
</h2>
<ul class='item-list' id='implementors-list'>
</ul><script type="text/javascript" async
src="../../implementors/serde/ser/trait.Serializer.js">
</script></section>
<section id='search' class="content hidden"></section>
<section class="footer"></section>
<aside id="help" class="hidden">
<div>
<h1 class="hidden">Help</h1>
<div class="shortcuts">
<h2>Keyboard Shortcuts</h2>
<dl>
<dt><kbd>?</kbd></dt>
<dd>Show this help dialog</dd>
<dt><kbd>S</kbd></dt>
<dd>Focus the search field</dd>
<dt><kbd></kbd></dt>
<dd>Move up in search results</dd>
<dt><kbd></kbd></dt>
<dd>Move down in search results</dd>
<dt><kbd></kbd></dt>
<dd>Switch tab</dd>
<dt><kbd>&#9166;</kbd></dt>
<dd>Go to active search result</dd>
<dt><kbd>+</kbd></dt>
<dd>Expand all sections</dd>
<dt><kbd>-</kbd></dt>
<dd>Collapse all sections</dd>
</dl>
</div>
<div class="infos">
<h2>Search Tricks</h2>
<p>
Prefix searches with a type followed by a colon (e.g.
<code>fn:</code>) to restrict the search to a given type.
</p>
<p>
Accepted types are: <code>fn</code>, <code>mod</code>,
<code>struct</code>, <code>enum</code>,
<code>trait</code>, <code>type</code>, <code>macro</code>,
and <code>const</code>.
</p>
<p>
Search functions by type signature (e.g.
<code>vec -> usize</code> or <code>* -> vec</code>)
</p>
</div>
</div>
</aside>
<script>
window.rootPath = "../../";
window.currentCrate = "serde";
</script>
<script src="../../main.js"></script>
<script defer src="../../search-index.js"></script>
</body>
</html>