You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Trac3r-rust/doc/serde/ser/trait.Serializer.html

964 lines
103 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!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="../../light.css" id="themeStyle"><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="shortcut icon" href="../../favicon.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></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><a href='../../serde/index.html'><div class='logo-container'><img src='../../rust-logo.png' alt='logo'></div></a><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_bytes">serialize_bytes</a><a href="#tymethod.serialize_char">serialize_char</a><a href="#tymethod.serialize_f32">serialize_f32</a><a href="#tymethod.serialize_f64">serialize_f64</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_i8">serialize_i8</a><a href="#tymethod.serialize_map">serialize_map</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_none">serialize_none</a><a href="#tymethod.serialize_seq">serialize_seq</a><a href="#tymethod.serialize_some">serialize_some</a><a href="#tymethod.serialize_str">serialize_str</a><a href="#tymethod.serialize_struct">serialize_struct</a><a href="#tymethod.serialize_struct_variant">serialize_struct_variant</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_u16">serialize_u16</a><a href="#tymethod.serialize_u32">serialize_u32</a><a href="#tymethod.serialize_u64">serialize_u64</a><a href="#tymethod.serialize_u8">serialize_u8</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></div><a class="sidebar-title" href="#provided-methods">Provided Methods</a><div class="sidebar-links"><a href="#method.collect_map">collect_map</a><a href="#method.collect_seq">collect_seq</a><a href="#method.collect_str">collect_str</a><a href="#method.is_human_readable">is_human_readable</a><a href="#method.serialize_i128">serialize_i128</a><a href="#method.serialize_u128">serialize_u128</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"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"></div><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><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#323-1445' title='goto source code'>[src]</a></span><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></h1><div class="docblock type-decl hidden-by-usual-hider"><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.serialize_i128' class='fnname'>serialize_i128</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i128.html">i128</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='#method.serialize_u128' class='fnname'>serialize_u128</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u128.html">u128</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='#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/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::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/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::IntoIterator">IntoIterator</a>&gt;::<a class="type" href="https://doc.rust-lang.org/nightly/core/iter/traits/collect/trait.IntoIterator.html#associatedtype.Item" title="type core::iter::traits::collect::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/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::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><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'><code id='Ok.t'>type <a href='#associatedtype.Ok' class="type">Ok</a></code></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'><code id='Error.t'>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></h3><div class='docblock'><p>The error type when some error occurs during serialization.</p>
</div><h3 id='associatedtype.SerializeSeq' class='method'><code id='SerializeSeq.t'>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></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'><code id='SerializeTuple.t'>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></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'><code id='SerializeTupleStruct.t'>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></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'><code id='SerializeTupleVariant.t'>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></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'><code id='SerializeMap.t'>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></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'><code id='SerializeStruct.t'>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></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'><code id='SerializeStructVariant.t'>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></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><span class='loading-content'>Loading content...</span>
<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'><code id='serialize_bool.v'>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></h3><div class='docblock'><p>Serialize a <code>bool</code> value.</p>
<pre><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for bool {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_bool(*self)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_i8' class='method'><code id='serialize_i8.v'>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></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><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for i8 {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_i8(*self)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_i16' class='method'><code id='serialize_i16.v'>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></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><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for i16 {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_i16(*self)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_i32' class='method'><code id='serialize_i32.v'>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></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><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for i32 {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_i32(*self)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_i64' class='method'><code id='serialize_i64.v'>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></h3><div class='docblock'><p>Serialize an <code>i64</code> value.</p>
<pre><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for i64 {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_i64(*self)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_u8' class='method'><code id='serialize_u8.v'>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></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><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for u8 {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_u8(*self)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_u16' class='method'><code id='serialize_u16.v'>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></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><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for u16 {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_u16(*self)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_u32' class='method'><code id='serialize_u32.v'>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></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><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for u32 {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_u32(*self)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_u64' class='method'><code id='serialize_u64.v'>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></h3><div class='docblock'><p>Serialize a <code>u64</code> value.</p>
<pre><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for u64 {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_u64(*self)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_f32' class='method'><code id='serialize_f32.v'>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></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><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for f32 {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_f32(*self)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_f64' class='method'><code id='serialize_f64.v'>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></h3><div class='docblock'><p>Serialize an <code>f64</code> value.</p>
<pre><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for f64 {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_f64(*self)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_char' class='method'><code id='serialize_char.v'>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></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><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for char {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_char(*self)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_str' class='method'><code id='serialize_str.v'>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></h3><div class='docblock'><p>Serialize a <code>&amp;str</code>.</p>
<pre><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for str {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_str(self)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_bytes' class='method'><code id='serialize_bytes.v'>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></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><code class="language-edition2018"># use serde::ser::{Serializer, SerializeSeq};
# use serde::private::ser::Error;
#
# struct MySerializer;
#
# impl Serializer for MySerializer {
# type Ok = ();
# type Error = Error;
#
fn serialize_bytes(self, v: &amp;[u8]) -&gt; Result&lt;Self::Ok, Self::Error&gt; {
let mut seq = self.serialize_seq(Some(v.len()))?;
for b in v {
seq.serialize_element(b)?;
}
seq.end()
}
#
# serde::__serialize_unimplemented! {
# bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str none some
# unit unit_struct unit_variant newtype_struct newtype_variant
# seq tuple tuple_struct tuple_variant map struct struct_variant
# }
# }
</code></pre>
</div><h3 id='tymethod.serialize_none' class='method'><code id='serialize_none.v'>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></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><code class="language-edition2018"># use serde::{Serialize, Serializer};
#
# enum Option&lt;T&gt; {
# Some(T),
# None,
# }
#
# use self::Option::{Some, None};
#
impl&lt;T&gt; Serialize for Option&lt;T&gt;
where
T: Serialize,
{
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
match *self {
Some(ref value) =&gt; serializer.serialize_some(value),
None =&gt; serializer.serialize_none(),
}
}
}
#
# fn main() {}
</code></pre>
</div><h3 id='tymethod.serialize_some' class='method'><code id='serialize_some.v'>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></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><code class="language-edition2018"># use serde::{Serialize, Serializer};
#
# enum Option&lt;T&gt; {
# Some(T),
# None,
# }
#
# use self::Option::{Some, None};
#
impl&lt;T&gt; Serialize for Option&lt;T&gt;
where
T: Serialize,
{
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
match *self {
Some(ref value) =&gt; serializer.serialize_some(value),
None =&gt; serializer.serialize_none(),
}
}
}
#
# fn main() {}
</code></pre>
</div><h3 id='tymethod.serialize_unit' class='method'><code id='serialize_unit.v'>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></h3><div class='docblock'><p>Serialize a <code>()</code> value.</p>
<pre><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for () {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_unit()
}
}
</code></pre>
</div><h3 id='tymethod.serialize_unit_struct' class='method'><code id='serialize_unit_struct.v'>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></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><code class="language-edition2018">use serde::{Serialize, Serializer};
struct Nothing;
impl Serialize for Nothing {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_unit_struct(&quot;Nothing&quot;)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_unit_variant' class='method'><code id='serialize_unit_variant.v'>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></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><code class="language-edition2018">use serde::{Serialize, Serializer};
enum E {
A,
B,
}
impl Serialize for E {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
match *self {
E::A =&gt; serializer.serialize_unit_variant(&quot;E&quot;, 0, &quot;A&quot;),
E::B =&gt; serializer.serialize_unit_variant(&quot;E&quot;, 1, &quot;B&quot;),
}
}
}
</code></pre>
</div><h3 id='tymethod.serialize_newtype_struct' class='method'><code id='serialize_newtype_struct.v'>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></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><code class="language-edition2018">use serde::{Serialize, Serializer};
struct Millimeters(u8);
impl Serialize for Millimeters {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_newtype_struct(&quot;Millimeters&quot;, &amp;self.0)
}
}
</code></pre>
</div><h3 id='tymethod.serialize_newtype_variant' class='method'><code id='serialize_newtype_variant.v'>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></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><code class="language-edition2018">use serde::{Serialize, Serializer};
enum E {
M(String),
N(u8),
}
impl Serialize for E {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
match *self {
E::M(ref s) =&gt; serializer.serialize_newtype_variant(&quot;E&quot;, 0, &quot;M&quot;, s),
E::N(n) =&gt; serializer.serialize_newtype_variant(&quot;E&quot;, 1, &quot;N&quot;, &amp;n),
}
}
}
</code></pre>
</div><h3 id='tymethod.serialize_seq' class='method'><code id='serialize_seq.v'>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></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><code class="language-edition2018"># use std::marker::PhantomData;
#
# struct Vec&lt;T&gt;(PhantomData&lt;T&gt;);
#
# impl&lt;T&gt; Vec&lt;T&gt; {
# fn len(&amp;self) -&gt; usize {
# unimplemented!()
# }
# }
#
# impl&lt;'a, T&gt; IntoIterator for &amp;'a Vec&lt;T&gt; {
# type Item = &amp;'a T;
# type IntoIter = Box&lt;Iterator&lt;Item = &amp;'a T&gt;&gt;;
#
# fn into_iter(self) -&gt; Self::IntoIter {
# unimplemented!()
# }
# }
#
use serde::ser::{Serialize, Serializer, SerializeSeq};
impl&lt;T&gt; Serialize for Vec&lt;T&gt;
where
T: Serialize,
{
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
let mut seq = serializer.serialize_seq(Some(self.len()))?;
for element in self {
seq.serialize_element(element)?;
}
seq.end()
}
}
</code></pre>
</div><h3 id='tymethod.serialize_tuple' class='method'><code id='serialize_tuple.v'>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></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><code class="language-edition2018">use serde::ser::{Serialize, Serializer, SerializeTuple};
# mod fool {
# trait Serialize {}
impl&lt;A, B, C&gt; Serialize for (A, B, C)
# {}
# }
#
# struct Tuple3&lt;A, B, C&gt;(A, B, C);
#
# impl&lt;A, B, C&gt; Serialize for Tuple3&lt;A, B, C&gt;
where
A: Serialize,
B: Serialize,
C: Serialize,
{
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
let mut tup = serializer.serialize_tuple(3)?;
tup.serialize_element(&amp;self.0)?;
tup.serialize_element(&amp;self.1)?;
tup.serialize_element(&amp;self.2)?;
tup.end()
}
}
</code></pre>
<pre><code class="language-edition2018">use serde::ser::{Serialize, SerializeTuple, Serializer};
const VRAM_SIZE: usize = 386;
struct Vram([u16; VRAM_SIZE]);
impl Serialize for Vram {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
let mut seq = serializer.serialize_tuple(VRAM_SIZE)?;
for element in &amp;self.0[..] {
seq.serialize_element(element)?;
}
seq.end()
}
}
</code></pre>
</div><h3 id='tymethod.serialize_tuple_struct' class='method'><code id='serialize_tuple_struct.v'>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></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><code class="language-edition2018">use serde::ser::{Serialize, SerializeTupleStruct, Serializer};
struct Rgb(u8, u8, u8);
impl Serialize for Rgb {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
let mut ts = serializer.serialize_tuple_struct(&quot;Rgb&quot;, 3)?;
ts.serialize_field(&amp;self.0)?;
ts.serialize_field(&amp;self.1)?;
ts.serialize_field(&amp;self.2)?;
ts.end()
}
}
</code></pre>
</div><h3 id='tymethod.serialize_tuple_variant' class='method'><code id='serialize_tuple_variant.v'>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></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><code class="language-edition2018">use serde::ser::{Serialize, SerializeTupleVariant, Serializer};
enum E {
T(u8, u8),
U(String, u32, u32),
}
impl Serialize for E {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
match *self {
E::T(ref a, ref b) =&gt; {
let mut tv = serializer.serialize_tuple_variant(&quot;E&quot;, 0, &quot;T&quot;, 2)?;
tv.serialize_field(a)?;
tv.serialize_field(b)?;
tv.end()
}
E::U(ref a, ref b, ref c) =&gt; {
let mut tv = serializer.serialize_tuple_variant(&quot;E&quot;, 1, &quot;U&quot;, 3)?;
tv.serialize_field(a)?;
tv.serialize_field(b)?;
tv.serialize_field(c)?;
tv.end()
}
}
}
}
</code></pre>
</div><h3 id='tymethod.serialize_map' class='method'><code id='serialize_map.v'>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></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><code class="language-edition2018"># use std::marker::PhantomData;
#
# struct HashMap&lt;K, V&gt;(PhantomData&lt;K&gt;, PhantomData&lt;V&gt;);
#
# impl&lt;K, V&gt; HashMap&lt;K, V&gt; {
# fn len(&amp;self) -&gt; usize {
# unimplemented!()
# }
# }
#
# impl&lt;'a, K, V&gt; IntoIterator for &amp;'a HashMap&lt;K, V&gt; {
# type Item = (&amp;'a K, &amp;'a V);
# type IntoIter = Box&lt;Iterator&lt;Item = (&amp;'a K, &amp;'a V)&gt;&gt;;
#
# fn into_iter(self) -&gt; Self::IntoIter {
# unimplemented!()
# }
# }
#
use serde::ser::{Serialize, Serializer, SerializeMap};
impl&lt;K, V&gt; Serialize for HashMap&lt;K, V&gt;
where
K: Serialize,
V: Serialize,
{
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(self.len()))?;
for (k, v) in self {
map.serialize_entry(k, v)?;
}
map.end()
}
}
</code></pre>
</div><h3 id='tymethod.serialize_struct' class='method'><code id='serialize_struct.v'>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></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><code class="language-edition2018">use serde::ser::{Serialize, SerializeStruct, Serializer};
struct Rgb {
r: u8,
g: u8,
b: u8,
}
impl Serialize for Rgb {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
let mut rgb = serializer.serialize_struct(&quot;Rgb&quot;, 3)?;
rgb.serialize_field(&quot;r&quot;, &amp;self.r)?;
rgb.serialize_field(&quot;g&quot;, &amp;self.g)?;
rgb.serialize_field(&quot;b&quot;, &amp;self.b)?;
rgb.end()
}
}
</code></pre>
</div><h3 id='tymethod.serialize_struct_variant' class='method'><code id='serialize_struct_variant.v'>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></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><code class="language-edition2018">use serde::ser::{Serialize, SerializeStructVariant, Serializer};
enum E {
S { r: u8, g: u8, b: u8 },
}
impl Serialize for E {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
match *self {
E::S {
ref r,
ref g,
ref b,
} =&gt; {
let mut sv = serializer.serialize_struct_variant(&quot;E&quot;, 0, &quot;S&quot;, 3)?;
sv.serialize_field(&quot;r&quot;, r)?;
sv.serialize_field(&quot;g&quot;, g)?;
sv.serialize_field(&quot;b&quot;, b)?;
sv.end()
}
}
}
}
</code></pre>
</div></div><span class='loading-content'>Loading content...</span>
<h2 id='provided-methods' class='small-section-header'>Provided methods<a href='#provided-methods' class='anchor'></a></h2><div class='methods'><h3 id='method.serialize_i128' class='method'><code id='serialize_i128.v'>fn <a href='#method.serialize_i128' class='fnname'>serialize_i128</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i128.html">i128</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></h3><div class='docblock'><p>Serialize an <code>i128</code> value.</p>
<pre><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for i128 {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_i128(*self)
}
}
</code></pre>
<p>This method is available only on Rust compiler versions &gt;=1.26. The
default behavior unconditionally returns an error.</p>
</div><h3 id='method.serialize_u128' class='method'><code id='serialize_u128.v'>fn <a href='#method.serialize_u128' class='fnname'>serialize_u128</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u128.html">u128</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></h3><div class='docblock'><p>Serialize a <code>u128</code> value.</p>
<pre><code class="language-edition2018"># use serde::Serializer;
#
# serde::__private_serialize!();
#
impl Serialize for u128 {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.serialize_u128(*self)
}
}
</code></pre>
<p>This method is available only on Rust compiler versions &gt;=1.26. The
default behavior unconditionally returns an error.</p>
</div><h3 id='method.collect_seq' class='method'><code id='collect_seq.v'>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/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::IntoIterator">IntoIterator</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;I as <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::IntoIterator">IntoIterator</a>&gt;::<a class="type" href="https://doc.rust-lang.org/nightly/core/iter/traits/collect/trait.IntoIterator.html#associatedtype.Item" title="type core::iter::traits::collect::IntoIterator::Item">Item</a>: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,&nbsp;</span></code></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><code class="language-edition2018">use serde::{Serialize, Serializer};
struct SecretlyOneHigher {
data: Vec&lt;i32&gt;,
}
impl Serialize for SecretlyOneHigher {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.collect_seq(self.data.iter().map(|x| x + 1))
}
}
</code></pre>
</div><h3 id='method.collect_map' class='method'><code id='collect_map.v'>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/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::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></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><code class="language-edition2018">use serde::{Serialize, Serializer};
use std::collections::BTreeSet;
struct MapToUnit {
keys: BTreeSet&lt;i32&gt;,
}
// Serializes as a map in which the values are all unit.
impl Serialize for MapToUnit {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.collect_map(self.keys.iter().map(|k| (k, ())))
}
}
</code></pre>
</div><h3 id='method.collect_str' class='method'><code id='collect_str.v'>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></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><code class="language-edition2018"># struct DateTime;
#
# impl DateTime {
# fn naive_local(&amp;self) -&gt; () { () }
# fn offset(&amp;self) -&gt; () { () }
# }
#
use serde::{Serialize, Serializer};
impl Serialize for DateTime {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
serializer.collect_str(&amp;format_args!(&quot;{:?}{:?}&quot;,
self.naive_local(),
self.offset()))
}
}
</code></pre>
</div><h3 id='method.is_human_readable' class='method'><code id='is_human_readable.v'>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></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><code class="language-edition2018"># use std::fmt::{self, Display};
#
# struct Timestamp;
#
# impl Timestamp {
# fn seconds_since_epoch(&amp;self) -&gt; u64 { unimplemented!() }
# }
#
# impl Display for Timestamp {
# fn fmt(&amp;self, formatter: &amp;mut fmt::Formatter) -&gt; fmt::Result {
# unimplemented!()
# }
# }
#
use serde::{Serialize, Serializer};
impl Serialize for Timestamp {
fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
where
S: Serializer,
{
if serializer.is_human_readable() {
// Serialize to a human-readable string &quot;2015-05-15T17:01:00Z&quot;.
self.to_string().serialize(serializer)
} else {
// Serialize to a compact binary representation.
self.seconds_since_epoch().serialize(serializer)
}
}
}
</code></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><span class='loading-content'>Loading content...</span>
<h2 id='implementors' class='small-section-header'>Implementors<a href='#implementors' class='anchor'></a></h2><div class='item-list' id='implementors-list'></div><span class='loading-content'>Loading content...</span><script type="text/javascript">window.inlined_types=new Set([]);</script><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><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "serde";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>