872 lines
126 KiB
HTML
872 lines
126 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html lang="en">
|
|||
|
<head>
|
|||
|
<meta charset="utf-8">
|
|||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|||
|
<meta name="generator" content="rustdoc">
|
|||
|
<meta name="description" content="API documentation for the Rust `Serializer` trait in crate `serde`.">
|
|||
|
<meta name="keywords" content="rust, rustlang, rust-lang, Serializer">
|
|||
|
|
|||
|
<title>serde::Serializer - Rust</title>
|
|||
|
|
|||
|
<link rel="stylesheet" type="text/css" href="../normalize.css">
|
|||
|
<link rel="stylesheet" type="text/css" href="../rustdoc.css">
|
|||
|
<link rel="stylesheet" type="text/css" href="../main.css">
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</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">
|
|||
|
|
|||
|
<p class='location'>Trait Serializer</p><div class="block items"><ul><li><a href="#associated-types">Associated Types</a></li><li><a href="#required-methods">Required Methods</a></li><li><a href="#provided-methods">Provided Methods</a></li><li><a href="#implementors">Implementors</a></li></ul></div><p class='location'><a href='index.html'>serde</a></p><script>window.sidebarCurrent = {name: 'Serializer', ty: 'trait', relpath: ''};</script><script defer src="sidebar-items.js"></script>
|
|||
|
</nav>
|
|||
|
|
|||
|
<nav class="sub">
|
|||
|
<form class="search-form js-only">
|
|||
|
<div class="search-container">
|
|||
|
<input class="search-input" name="search"
|
|||
|
autocomplete="off"
|
|||
|
placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
|
|||
|
type="search">
|
|||
|
</div>
|
|||
|
</form>
|
|||
|
</nav>
|
|||
|
|
|||
|
<section id='main' class="content">
|
|||
|
<h1 class='fqn'><span class='in-band'>Trait <a href='index.html'>serde</a>::<wbr><a class="trait" href=''>Serializer</a></span><span class='out-of-band'><span id='render-detail'>
|
|||
|
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
|
|||
|
[<span class='inner'>−</span>]
|
|||
|
</a>
|
|||
|
</span><a class='srclink' href='../src/serde/ser/mod.rs.html#311-1366' title='goto source code'>[src]</a></span></h1>
|
|||
|
<pre class='rust trait'>pub trait Serializer: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> {
|
|||
|
type <a href='#associatedtype.Ok' class="type">Ok</a>;
|
|||
|
type <a href='#associatedtype.Error' class="type">Error</a>: <a class="trait" href="../serde/ser/trait.Error.html" title="trait serde::ser::Error">Error</a>;
|
|||
|
type <a href='#associatedtype.SerializeSeq' class="type">SerializeSeq</a>: <a class="trait" href="../serde/ser/trait.SerializeSeq.html" title="trait serde::ser::SerializeSeq">SerializeSeq</a><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>>;
|
|||
|
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><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>>;
|
|||
|
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><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>>;
|
|||
|
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><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>>;
|
|||
|
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><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>>;
|
|||
|
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><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>>;
|
|||
|
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><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>>;
|
|||
|
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>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
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>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
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>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
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>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
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>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
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>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
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>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
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>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
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>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
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>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
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>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
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>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
fn <a href='#tymethod.serialize_str' class='fnname'>serialize_str</a>(self, v: &<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
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">&[</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>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
fn <a href='#tymethod.serialize_none' class='fnname'>serialize_none</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
fn <a href='#tymethod.serialize_some' class='fnname'>serialize_some</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(<br> self, <br> value: &T<br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>><br> <span class="where">where<br> T: <a class="trait" href="../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a></span>;
|
|||
|
fn <a href='#tymethod.serialize_unit' class='fnname'>serialize_unit</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
fn <a href='#tymethod.serialize_unit_struct' class='fnname'>serialize_unit_struct</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
fn <a href='#tymethod.serialize_unit_variant' class='fnname'>serialize_unit_variant</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
fn <a href='#tymethod.serialize_newtype_struct' class='fnname'>serialize_newtype_struct</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> value: &T<br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>><br> <span class="where">where<br> T: <a class="trait" href="../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a></span>;
|
|||
|
fn <a href='#tymethod.serialize_newtype_variant' class='fnname'>serialize_newtype_variant</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> value: &T<br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>><br> <span class="where">where<br> T: <a class="trait" href="../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a></span>;
|
|||
|
fn <a href='#tymethod.serialize_seq' class='fnname'>serialize_seq</a>(<br> self, <br> len: <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
fn <a href='#tymethod.serialize_tuple' class='fnname'>serialize_tuple</a>(<br> self, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
fn <a href='#tymethod.serialize_tuple_struct' class='fnname'>serialize_tuple_struct</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
fn <a href='#tymethod.serialize_tuple_variant' class='fnname'>serialize_tuple_variant</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
fn <a href='#tymethod.serialize_map' class='fnname'>serialize_map</a>(<br> self, <br> len: <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
fn <a href='#tymethod.serialize_struct' class='fnname'>serialize_struct</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
fn <a href='#tymethod.serialize_struct_variant' class='fnname'>serialize_struct_variant</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>>;
|
|||
|
|
|||
|
fn <a href='#method.collect_seq' class='fnname'>collect_seq</a><I>(self, iter: I) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>><br> <span class="where">where<br> I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html" title="trait core::iter::traits::IntoIterator">IntoIterator</a>,<br> <I as <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html" title="trait core::iter::traits::IntoIterator">IntoIterator</a>>::<a class="type" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html#associatedtype.Item" title="type core::iter::traits::IntoIterator::Item">Item</a>: <a class="trait" href="../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a></span>,
|
|||
|
{ ... }
|
|||
|
fn <a href='#method.collect_map' class='fnname'>collect_map</a><K, V, I>(self, iter: I) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>><br> <span class="where">where<br> K: <a class="trait" href="../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,<br> V: <a class="trait" href="../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,<br> I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html" title="trait core::iter::traits::IntoIterator">IntoIterator</a><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>></span>,
|
|||
|
{ ... }
|
|||
|
fn <a href='#method.collect_str' class='fnname'>collect_str</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(self, value: &T) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>><br> <span class="where">where<br> T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a></span>,
|
|||
|
{ ... }
|
|||
|
}</pre><div class='docblock'><p>A <strong>data format</strong> that can serialize any data structure supported by Serde.</p>
|
|||
|
|
|||
|
<p>The role of this trait is to define the serialization half of the Serde data
|
|||
|
model, which is a way to categorize every Rust data structure into one of 27
|
|||
|
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>12 primitive types</strong>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>bool</li>
|
|||
|
<li>i8, i16, i32, i64</li>
|
|||
|
<li>u8, u16, u32, u64</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<T></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<T></code> or
|
|||
|
<code>HashSet<T></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<T>)</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<K, V></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>
|
|||
|
</div>
|
|||
|
<h2 id='associated-types'>Associated Types</h2>
|
|||
|
<div class='methods'>
|
|||
|
<h3 id='associatedtype.Ok' class='method'><span id='Ok.t' class='invisible'><code>type <a href='#associatedtype.Ok' class="type">Ok</a></code></span></h3><div class='docblock'><p>The output type produced by this <code>Serializer</code> during successful
|
|||
|
serialization. Most serializers that produce text or binary output
|
|||
|
should set <code>Ok = ()</code> and serialize into an <a href="https://doc.rust-lang.org/std/io/trait.Write.html"><code>io::Write</code></a> or buffer
|
|||
|
contained within the <code>Serializer</code> instance. Serializers that build
|
|||
|
in-memory data structures may be simplified by using <code>Ok</code> to propagate
|
|||
|
the data structure around.</p>
|
|||
|
</div><h3 id='associatedtype.Error' class='method'><span id='Error.t' class='invisible'><code>type <a href='#associatedtype.Error' class="type">Error</a>: <a class="trait" href="../serde/ser/trait.Error.html" title="trait serde::ser::Error">Error</a></code></span></h3><div class='docblock'><p>The error type when some error occurs during serialization.</p>
|
|||
|
</div><h3 id='associatedtype.SerializeSeq' class='method'><span id='SerializeSeq.t' class='invisible'><code>type <a href='#associatedtype.SerializeSeq' class="type">SerializeSeq</a>: <a class="trait" href="../serde/ser/trait.SerializeSeq.html" title="trait serde::ser::SerializeSeq">SerializeSeq</a><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>></code></span></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_seq"><code>serialize_seq</code></a> for serializing the content of the
|
|||
|
sequence.</p>
|
|||
|
</div><h3 id='associatedtype.SerializeTuple' class='method'><span id='SerializeTuple.t' class='invisible'><code>type <a href='#associatedtype.SerializeTuple' class="type">SerializeTuple</a>: <a class="trait" href="../serde/ser/trait.SerializeTuple.html" title="trait serde::ser::SerializeTuple">SerializeTuple</a><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>></code></span></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_tuple"><code>serialize_tuple</code></a> for serializing the content of
|
|||
|
the tuple.</p>
|
|||
|
</div><h3 id='associatedtype.SerializeTupleStruct' class='method'><span id='SerializeTupleStruct.t' class='invisible'><code>type <a href='#associatedtype.SerializeTupleStruct' class="type">SerializeTupleStruct</a>: <a class="trait" href="../serde/ser/trait.SerializeTupleStruct.html" title="trait serde::ser::SerializeTupleStruct">SerializeTupleStruct</a><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>></code></span></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_tuple_struct"><code>serialize_tuple_struct</code></a> for serializing the
|
|||
|
content of the tuple struct.</p>
|
|||
|
</div><h3 id='associatedtype.SerializeTupleVariant' class='method'><span id='SerializeTupleVariant.t' class='invisible'><code>type <a href='#associatedtype.SerializeTupleVariant' class="type">SerializeTupleVariant</a>: <a class="trait" href="../serde/ser/trait.SerializeTupleVariant.html" title="trait serde::ser::SerializeTupleVariant">SerializeTupleVariant</a><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>></code></span></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_tuple_variant"><code>serialize_tuple_variant</code></a> for serializing the
|
|||
|
content of the tuple variant.</p>
|
|||
|
</div><h3 id='associatedtype.SerializeMap' class='method'><span id='SerializeMap.t' class='invisible'><code>type <a href='#associatedtype.SerializeMap' class="type">SerializeMap</a>: <a class="trait" href="../serde/ser/trait.SerializeMap.html" title="trait serde::ser::SerializeMap">SerializeMap</a><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>></code></span></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_map"><code>serialize_map</code></a> for serializing the content of the
|
|||
|
map.</p>
|
|||
|
</div><h3 id='associatedtype.SerializeStruct' class='method'><span id='SerializeStruct.t' class='invisible'><code>type <a href='#associatedtype.SerializeStruct' class="type">SerializeStruct</a>: <a class="trait" href="../serde/ser/trait.SerializeStruct.html" title="trait serde::ser::SerializeStruct">SerializeStruct</a><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>></code></span></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_struct"><code>serialize_struct</code></a> for serializing the content of
|
|||
|
the struct.</p>
|
|||
|
</div><h3 id='associatedtype.SerializeStructVariant' class='method'><span id='SerializeStructVariant.t' class='invisible'><code>type <a href='#associatedtype.SerializeStructVariant' class="type">SerializeStructVariant</a>: <a class="trait" href="../serde/ser/trait.SerializeStructVariant.html" title="trait serde::ser::SerializeStructVariant">SerializeStructVariant</a><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>></code></span></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_struct_variant"><code>serialize_struct_variant</code></a> for serializing the
|
|||
|
content of the struct variant.</p>
|
|||
|
</div></div>
|
|||
|
<h2 id='required-methods'>Required Methods</h2>
|
|||
|
<div class='methods'>
|
|||
|
<h3 id='tymethod.serialize_bool' class='method'><span id='serialize_bool.v' class='invisible'><code>fn <a href='#tymethod.serialize_bool' class='fnname'>serialize_bool</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize a <code>bool</code> value.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">bool</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_bool</span>(<span class="kw-2">*</span><span class="self">self</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_i8' class='method'><span id='serialize_i8.v' class='invisible'><code>fn <a href='#tymethod.serialize_i8' class='fnname'>serialize_i8</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i8.html">i8</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize an <code>i8</code> value.</p>
|
|||
|
|
|||
|
<p>If the format does not differentiate between <code>i8</code> and <code>i64</code>, a
|
|||
|
reasonable implementation would be to cast the value to <code>i64</code> and
|
|||
|
forward to <code>serialize_i64</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">i8</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_i8</span>(<span class="kw-2">*</span><span class="self">self</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_i16' class='method'><span id='serialize_i16.v' class='invisible'><code>fn <a href='#tymethod.serialize_i16' class='fnname'>serialize_i16</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i16.html">i16</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize an <code>i16</code> value.</p>
|
|||
|
|
|||
|
<p>If the format does not differentiate between <code>i16</code> and <code>i64</code>, a
|
|||
|
reasonable implementation would be to cast the value to <code>i64</code> and
|
|||
|
forward to <code>serialize_i64</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">i16</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_i16</span>(<span class="kw-2">*</span><span class="self">self</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_i32' class='method'><span id='serialize_i32.v' class='invisible'><code>fn <a href='#tymethod.serialize_i32' class='fnname'>serialize_i32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i32.html">i32</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize an <code>i32</code> value.</p>
|
|||
|
|
|||
|
<p>If the format does not differentiate between <code>i32</code> and <code>i64</code>, a
|
|||
|
reasonable implementation would be to cast the value to <code>i64</code> and
|
|||
|
forward to <code>serialize_i64</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">i32</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_i32</span>(<span class="kw-2">*</span><span class="self">self</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_i64' class='method'><span id='serialize_i64.v' class='invisible'><code>fn <a href='#tymethod.serialize_i64' class='fnname'>serialize_i64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i64.html">i64</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize an <code>i64</code> value.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">i64</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_i64</span>(<span class="kw-2">*</span><span class="self">self</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_u8' class='method'><span id='serialize_u8.v' class='invisible'><code>fn <a href='#tymethod.serialize_u8' class='fnname'>serialize_u8</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize a <code>u8</code> value.</p>
|
|||
|
|
|||
|
<p>If the format does not differentiate between <code>u8</code> and <code>u64</code>, a
|
|||
|
reasonable implementation would be to cast the value to <code>u64</code> and
|
|||
|
forward to <code>serialize_u64</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">u8</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_u8</span>(<span class="kw-2">*</span><span class="self">self</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_u16' class='method'><span id='serialize_u16.v' class='invisible'><code>fn <a href='#tymethod.serialize_u16' class='fnname'>serialize_u16</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u16.html">u16</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize a <code>u16</code> value.</p>
|
|||
|
|
|||
|
<p>If the format does not differentiate between <code>u16</code> and <code>u64</code>, a
|
|||
|
reasonable implementation would be to cast the value to <code>u64</code> and
|
|||
|
forward to <code>serialize_u64</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">u16</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_u16</span>(<span class="kw-2">*</span><span class="self">self</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_u32' class='method'><span id='serialize_u32.v' class='invisible'><code>fn <a href='#tymethod.serialize_u32' class='fnname'>serialize_u32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize a <code>u32</code> value.</p>
|
|||
|
|
|||
|
<p>If the format does not differentiate between <code>u32</code> and <code>u64</code>, a
|
|||
|
reasonable implementation would be to cast the value to <code>u64</code> and
|
|||
|
forward to <code>serialize_u64</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">u32</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_u32</span>(<span class="kw-2">*</span><span class="self">self</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_u64' class='method'><span id='serialize_u64.v' class='invisible'><code>fn <a href='#tymethod.serialize_u64' class='fnname'>serialize_u64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u64.html">u64</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize a <code>u64</code> value.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">u64</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_u64</span>(<span class="kw-2">*</span><span class="self">self</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_f32' class='method'><span id='serialize_f32.v' class='invisible'><code>fn <a href='#tymethod.serialize_f32' class='fnname'>serialize_f32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f32.html">f32</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize an <code>f32</code> value.</p>
|
|||
|
|
|||
|
<p>If the format does not differentiate between <code>f32</code> and <code>f64</code>, a
|
|||
|
reasonable implementation would be to cast the value to <code>f64</code> and
|
|||
|
forward to <code>serialize_f64</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">f32</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_f32</span>(<span class="kw-2">*</span><span class="self">self</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_f64' class='method'><span id='serialize_f64.v' class='invisible'><code>fn <a href='#tymethod.serialize_f64' class='fnname'>serialize_f64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f64.html">f64</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize an <code>f64</code> value.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">f64</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_f64</span>(<span class="kw-2">*</span><span class="self">self</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_char' class='method'><span id='serialize_char.v' class='invisible'><code>fn <a href='#tymethod.serialize_char' class='fnname'>serialize_char</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.char.html">char</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize a character.</p>
|
|||
|
|
|||
|
<p>If the format does not support characters, it is reasonable to serialize
|
|||
|
it as a single element <code>str</code> or a <code>u32</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">char</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_char</span>(<span class="kw-2">*</span><span class="self">self</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_str' class='method'><span id='serialize_str.v' class='invisible'><code>fn <a href='#tymethod.serialize_str' class='fnname'>serialize_str</a>(self, v: &<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize a <code>&str</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">str</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_str</span>(<span class="self">self</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_bytes' class='method'><span id='serialize_bytes.v' class='invisible'><code>fn <a href='#tymethod.serialize_bytes' class='fnname'>serialize_bytes</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&[</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>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize a chunk of raw byte data.</p>
|
|||
|
|
|||
|
<p>Enables serializers to serialize byte slices more compactly or more
|
|||
|
efficiently than other types of slices. If no efficient implementation
|
|||
|
is available, a reasonable implementation would be to forward to
|
|||
|
<code>serialize_seq</code>. If forwarded, the implementation looks usually just
|
|||
|
like this:</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize_bytes</span>(<span class="self">self</span>, <span class="ident">v</span>: <span class="kw-2">&</span>[<span class="ident">u8</span>]) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="self">Self</span>::<span class="prelude-val">Ok</span>, <span class="self">Self</span>::<span class="ident">Error</span><span class="op">></span> {
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">seq</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">serialize_seq</span>(<span class="prelude-val">Some</span>(<span class="ident">v</span>.<span class="ident">len</span>()))<span class="question-mark">?</span>;
|
|||
|
<span class="kw">for</span> <span class="ident">b</span> <span class="kw">in</span> <span class="ident">v</span> {
|
|||
|
<span class="ident">seq</span>.<span class="ident">serialize_element</span>(<span class="ident">b</span>)<span class="question-mark">?</span>;
|
|||
|
}
|
|||
|
<span class="ident">seq</span>.<span class="ident">end</span>()
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_none' class='method'><span id='serialize_none.v' class='invisible'><code>fn <a href='#tymethod.serialize_none' class='fnname'>serialize_none</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize a <a href="https://doc.rust-lang.org/std/option/enum.Option.html#variant.None"><code>None</code></a> value.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="prelude-ty">Option</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">T</span>: <span class="ident">Serialize</span>
|
|||
|
{
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
|
|||
|
<span class="prelude-val">Some</span>(<span class="kw-2">ref</span> <span class="ident">value</span>) <span class="op">=></span> <span class="ident">serializer</span>.<span class="ident">serialize_some</span>(<span class="ident">value</span>),
|
|||
|
<span class="prelude-val">None</span> <span class="op">=></span> <span class="ident">serializer</span>.<span class="ident">serialize_none</span>(),
|
|||
|
}
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_some' class='method'><span id='serialize_some.v' class='invisible'><code>fn <a href='#tymethod.serialize_some' class='fnname'>serialize_some</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(self, value: &T) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>> <span class="where fmt-newline">where<br> T: <a class="trait" href="../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>, </span></code></span></h3><div class='docblock'><p>Serialize a <a href="https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some"><code>Some(T)</code></a> value.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="prelude-ty">Option</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">T</span>: <span class="ident">Serialize</span>
|
|||
|
{
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
|
|||
|
<span class="prelude-val">Some</span>(<span class="kw-2">ref</span> <span class="ident">value</span>) <span class="op">=></span> <span class="ident">serializer</span>.<span class="ident">serialize_some</span>(<span class="ident">value</span>),
|
|||
|
<span class="prelude-val">None</span> <span class="op">=></span> <span class="ident">serializer</span>.<span class="ident">serialize_none</span>(),
|
|||
|
}
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_unit' class='method'><span id='serialize_unit.v' class='invisible'><code>fn <a href='#tymethod.serialize_unit' class='fnname'>serialize_unit</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize a <code>()</code> value.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> () {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_unit</span>()
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_unit_struct' class='method'><span id='serialize_unit_struct.v' class='invisible'><code>fn <a href='#tymethod.serialize_unit_struct' class='fnname'>serialize_unit_struct</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize a unit struct like <code>struct Unit</code> or <code>PhantomData<T></code>.</p>
|
|||
|
|
|||
|
<p>A reasonable implementation would be to forward to <code>serialize_unit</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
|
|||
|
|
|||
|
<span class="kw">struct</span> <span class="ident">Nothing</span>;
|
|||
|
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">Nothing</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_unit_struct</span>(<span class="string">"Nothing"</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_unit_variant' class='method'><span id='serialize_unit_variant.v' class='invisible'><code>fn <a href='#tymethod.serialize_unit_variant' class='fnname'>serialize_unit_variant</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Serialize a unit variant like <code>E::A</code> in <code>enum E { A, B }</code>.</p>
|
|||
|
|
|||
|
<p>The <code>name</code> is the name of the enum, the <code>variant_index</code> is the index of
|
|||
|
this variant within the enum, and the <code>variant</code> is the name of the
|
|||
|
variant.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
|
|||
|
|
|||
|
<span class="kw">enum</span> <span class="ident">E</span> {
|
|||
|
<span class="ident">A</span>,
|
|||
|
<span class="ident">B</span>,
|
|||
|
}
|
|||
|
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">E</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
|
|||
|
<span class="ident">E</span>::<span class="ident">A</span> <span class="op">=></span> <span class="ident">serializer</span>.<span class="ident">serialize_unit_variant</span>(<span class="string">"E"</span>, <span class="number">0</span>, <span class="string">"A"</span>),
|
|||
|
<span class="ident">E</span>::<span class="ident">B</span> <span class="op">=></span> <span class="ident">serializer</span>.<span class="ident">serialize_unit_variant</span>(<span class="string">"E"</span>, <span class="number">1</span>, <span class="string">"B"</span>),
|
|||
|
}
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_newtype_struct' class='method'><span id='serialize_newtype_struct.v' class='invisible'><code>fn <a href='#tymethod.serialize_newtype_struct' class='fnname'>serialize_newtype_struct</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> value: &T<br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>> <span class="where fmt-newline">where<br> T: <a class="trait" href="../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>, </span></code></span></h3><div class='docblock'><p>Serialize a newtype struct like <code>struct Millimeters(u8)</code>.</p>
|
|||
|
|
|||
|
<p>Serializers are encouraged to treat newtype structs as insignificant
|
|||
|
wrappers around the data they contain. A reasonable implementation would
|
|||
|
be to forward to <code>value.serialize(self)</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
|
|||
|
|
|||
|
<span class="kw">struct</span> <span class="ident">Millimeters</span>(<span class="ident">u8</span>);
|
|||
|
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">Millimeters</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">serialize_newtype_struct</span>(<span class="string">"Millimeters"</span>, <span class="kw-2">&</span><span class="self">self</span>.<span class="number">0</span>)
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_newtype_variant' class='method'><span id='serialize_newtype_variant.v' class='invisible'><code>fn <a href='#tymethod.serialize_newtype_variant' class='fnname'>serialize_newtype_variant</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> value: &T<br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>> <span class="where fmt-newline">where<br> T: <a class="trait" href="../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>, </span></code></span></h3><div class='docblock'><p>Serialize a newtype variant like <code>E::N</code> in <code>enum E { N(u8) }</code>.</p>
|
|||
|
|
|||
|
<p>The <code>name</code> is the name of the enum, the <code>variant_index</code> is the index of
|
|||
|
this variant within the enum, and the <code>variant</code> is the name of the
|
|||
|
variant. The <code>value</code> is the data contained within this newtype variant.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
|
|||
|
|
|||
|
<span class="kw">enum</span> <span class="ident">E</span> {
|
|||
|
<span class="ident">M</span>(<span class="ident">String</span>),
|
|||
|
<span class="ident">N</span>(<span class="ident">u8</span>),
|
|||
|
}
|
|||
|
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">E</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
|
|||
|
<span class="ident">E</span>::<span class="ident">M</span>(<span class="kw-2">ref</span> <span class="ident">s</span>) <span class="op">=></span> <span class="ident">serializer</span>.<span class="ident">serialize_newtype_variant</span>(<span class="string">"E"</span>, <span class="number">0</span>, <span class="string">"M"</span>, <span class="ident">s</span>),
|
|||
|
<span class="ident">E</span>::<span class="ident">N</span>(<span class="ident">n</span>) <span class="op">=></span> <span class="ident">serializer</span>.<span class="ident">serialize_newtype_variant</span>(<span class="string">"E"</span>, <span class="number">1</span>, <span class="string">"N"</span>, <span class="kw-2">&</span><span class="ident">n</span>),
|
|||
|
}
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_seq' class='method'><span id='serialize_seq.v' class='invisible'><code>fn <a href='#tymethod.serialize_seq' class='fnname'>serialize_seq</a>(<br> self, <br> len: <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Begin to serialize a variably sized sequence. This call must be
|
|||
|
followed by zero or more calls to <code>serialize_element</code>, then a call to
|
|||
|
<code>end</code>.</p>
|
|||
|
|
|||
|
<p>The argument is the number of elements in the sequence, which may or may
|
|||
|
not be computable before the sequence is iterated. Some serializers only
|
|||
|
support sequences whose length is known up front.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>, <span class="ident">SerializeSeq</span>};
|
|||
|
|
|||
|
<span class="kw">impl</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">Vec</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">T</span>: <span class="ident">Serialize</span>
|
|||
|
{
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">seq</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_seq</span>(<span class="prelude-val">Some</span>(<span class="self">self</span>.<span class="ident">len</span>()))<span class="question-mark">?</span>;
|
|||
|
<span class="kw">for</span> <span class="ident">element</span> <span class="kw">in</span> <span class="self">self</span> {
|
|||
|
<span class="ident">seq</span>.<span class="ident">serialize_element</span>(<span class="ident">element</span>)<span class="question-mark">?</span>;
|
|||
|
}
|
|||
|
<span class="ident">seq</span>.<span class="ident">end</span>()
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_tuple' class='method'><span id='serialize_tuple.v' class='invisible'><code>fn <a href='#tymethod.serialize_tuple' class='fnname'>serialize_tuple</a>(<br> self, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Begin to serialize a statically sized sequence whose length will be
|
|||
|
known at deserialization time without looking at the serialized data.
|
|||
|
This call must be followed by zero or more calls to <code>serialize_element</code>,
|
|||
|
then a call to <code>end</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>, <span class="ident">SerializeTuple</span>};
|
|||
|
|
|||
|
<span class="kw">impl</span><span class="op"><</span><span class="ident">A</span>, <span class="ident">B</span>, <span class="ident">C</span><span class="op">></span> <span class="ident">Serialize</span> <span class="kw">for</span> (<span class="ident">A</span>, <span class="ident">B</span>, <span class="ident">C</span>)
|
|||
|
<span class="kw">where</span> <span class="ident">A</span>: <span class="ident">Serialize</span>,
|
|||
|
<span class="ident">B</span>: <span class="ident">Serialize</span>,
|
|||
|
<span class="ident">C</span>: <span class="ident">Serialize</span>
|
|||
|
{
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">tup</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_tuple</span>(<span class="number">3</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">tup</span>.<span class="ident">serialize_element</span>(<span class="kw-2">&</span><span class="self">self</span>.<span class="number">0</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">tup</span>.<span class="ident">serialize_element</span>(<span class="kw-2">&</span><span class="self">self</span>.<span class="number">1</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">tup</span>.<span class="ident">serialize_element</span>(<span class="kw-2">&</span><span class="self">self</span>.<span class="number">2</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">tup</span>.<span class="ident">end</span>()
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>, <span class="ident">SerializeTuple</span>};
|
|||
|
|
|||
|
<span class="kw">const</span> <span class="ident">VRAM_SIZE</span>: <span class="ident">usize</span> <span class="op">=</span> <span class="number">386</span>;
|
|||
|
<span class="kw">struct</span> <span class="ident">Vram</span>([<span class="ident">u16</span>; <span class="ident">VRAM_SIZE</span>]);
|
|||
|
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">Vram</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">seq</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_tuple</span>(<span class="ident">VRAM_SIZE</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="kw">for</span> <span class="ident">element</span> <span class="kw">in</span> <span class="kw-2">&</span><span class="self">self</span>.<span class="number">0</span>[..] {
|
|||
|
<span class="ident">seq</span>.<span class="ident">serialize_element</span>(<span class="ident">element</span>)<span class="question-mark">?</span>;
|
|||
|
}
|
|||
|
<span class="ident">seq</span>.<span class="ident">end</span>()
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_tuple_struct' class='method'><span id='serialize_tuple_struct.v' class='invisible'><code>fn <a href='#tymethod.serialize_tuple_struct' class='fnname'>serialize_tuple_struct</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Begin to serialize a tuple struct like <code>struct Rgb(u8, u8, u8)</code>. This
|
|||
|
call must be followed by zero or more calls to <code>serialize_field</code>, then a
|
|||
|
call to <code>end</code>.</p>
|
|||
|
|
|||
|
<p>The <code>name</code> is the name of the tuple struct and the <code>len</code> is the number
|
|||
|
of data fields that will be serialized.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>, <span class="ident">SerializeTupleStruct</span>};
|
|||
|
|
|||
|
<span class="kw">struct</span> <span class="ident">Rgb</span>(<span class="ident">u8</span>, <span class="ident">u8</span>, <span class="ident">u8</span>);
|
|||
|
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">Rgb</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">ts</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_tuple_struct</span>(<span class="string">"Rgb"</span>, <span class="number">3</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">ts</span>.<span class="ident">serialize_field</span>(<span class="kw-2">&</span><span class="self">self</span>.<span class="number">0</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">ts</span>.<span class="ident">serialize_field</span>(<span class="kw-2">&</span><span class="self">self</span>.<span class="number">1</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">ts</span>.<span class="ident">serialize_field</span>(<span class="kw-2">&</span><span class="self">self</span>.<span class="number">2</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">ts</span>.<span class="ident">end</span>()
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_tuple_variant' class='method'><span id='serialize_tuple_variant.v' class='invisible'><code>fn <a href='#tymethod.serialize_tuple_variant' class='fnname'>serialize_tuple_variant</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Begin to serialize a tuple variant like <code>E::T</code> in <code>enum E { T(u8, u8) }</code>. This call must be followed by zero or more calls to
|
|||
|
<code>serialize_field</code>, then a call to <code>end</code>.</p>
|
|||
|
|
|||
|
<p>The <code>name</code> is the name of the enum, the <code>variant_index</code> is the index of
|
|||
|
this variant within the enum, the <code>variant</code> is the name of the variant,
|
|||
|
and the <code>len</code> is the number of data fields that will be serialized.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>, <span class="ident">SerializeTupleVariant</span>};
|
|||
|
|
|||
|
<span class="kw">enum</span> <span class="ident">E</span> {
|
|||
|
<span class="ident">T</span>(<span class="ident">u8</span>, <span class="ident">u8</span>),
|
|||
|
<span class="ident">U</span>(<span class="ident">String</span>, <span class="ident">u32</span>, <span class="ident">u32</span>),
|
|||
|
}
|
|||
|
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">E</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
|
|||
|
<span class="ident">E</span>::<span class="ident">T</span>(<span class="kw-2">ref</span> <span class="ident">a</span>, <span class="kw-2">ref</span> <span class="ident">b</span>) <span class="op">=></span> {
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">tv</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_tuple_variant</span>(<span class="string">"E"</span>, <span class="number">0</span>, <span class="string">"T"</span>, <span class="number">2</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">tv</span>.<span class="ident">serialize_field</span>(<span class="ident">a</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">tv</span>.<span class="ident">serialize_field</span>(<span class="ident">b</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">tv</span>.<span class="ident">end</span>()
|
|||
|
}
|
|||
|
<span class="ident">E</span>::<span class="ident">U</span>(<span class="kw-2">ref</span> <span class="ident">a</span>, <span class="kw-2">ref</span> <span class="ident">b</span>, <span class="kw-2">ref</span> <span class="ident">c</span>) <span class="op">=></span> {
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">tv</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_tuple_variant</span>(<span class="string">"E"</span>, <span class="number">1</span>, <span class="string">"U"</span>, <span class="number">3</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">tv</span>.<span class="ident">serialize_field</span>(<span class="ident">a</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">tv</span>.<span class="ident">serialize_field</span>(<span class="ident">b</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">tv</span>.<span class="ident">serialize_field</span>(<span class="ident">c</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">tv</span>.<span class="ident">end</span>()
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_map' class='method'><span id='serialize_map.v' class='invisible'><code>fn <a href='#tymethod.serialize_map' class='fnname'>serialize_map</a>(<br> self, <br> len: <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Begin to serialize a map. This call must be followed by zero or more
|
|||
|
calls to <code>serialize_key</code> and <code>serialize_value</code>, then a call to <code>end</code>.</p>
|
|||
|
|
|||
|
<p>The argument is the number of elements in the map, which may or may not
|
|||
|
be computable before the map is iterated. Some serializers only support
|
|||
|
maps whose length is known up front.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>, <span class="ident">SerializeMap</span>};
|
|||
|
|
|||
|
<span class="kw">impl</span><span class="op"><</span><span class="ident">K</span>, <span class="ident">V</span><span class="op">></span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">HashMap</span><span class="op"><</span><span class="ident">K</span>, <span class="ident">V</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">K</span>: <span class="ident">Serialize</span>,
|
|||
|
<span class="ident">V</span>: <span class="ident">Serialize</span>
|
|||
|
{
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">map</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_map</span>(<span class="prelude-val">Some</span>(<span class="self">self</span>.<span class="ident">len</span>()))<span class="question-mark">?</span>;
|
|||
|
<span class="kw">for</span> (<span class="ident">k</span>, <span class="ident">v</span>) <span class="kw">in</span> <span class="self">self</span> {
|
|||
|
<span class="ident">map</span>.<span class="ident">serialize_entry</span>(<span class="ident">k</span>, <span class="ident">v</span>)<span class="question-mark">?</span>;
|
|||
|
}
|
|||
|
<span class="ident">map</span>.<span class="ident">end</span>()
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_struct' class='method'><span id='serialize_struct.v' class='invisible'><code>fn <a href='#tymethod.serialize_struct' class='fnname'>serialize_struct</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Begin to serialize a struct like <code>struct Rgb { r: u8, g: u8, b: u8 }</code>.
|
|||
|
This call must be followed by zero or more calls to <code>serialize_field</code>,
|
|||
|
then a call to <code>end</code>.</p>
|
|||
|
|
|||
|
<p>The <code>name</code> is the name of the struct and the <code>len</code> is the number of
|
|||
|
data fields that will be serialized.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>, <span class="ident">SerializeStruct</span>};
|
|||
|
|
|||
|
<span class="kw">struct</span> <span class="ident">Rgb</span> {
|
|||
|
<span class="ident">r</span>: <span class="ident">u8</span>,
|
|||
|
<span class="ident">g</span>: <span class="ident">u8</span>,
|
|||
|
<span class="ident">b</span>: <span class="ident">u8</span>,
|
|||
|
}
|
|||
|
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">Rgb</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rgb</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_struct</span>(<span class="string">"Rgb"</span>, <span class="number">3</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">rgb</span>.<span class="ident">serialize_field</span>(<span class="string">"r"</span>, <span class="kw-2">&</span><span class="self">self</span>.<span class="ident">r</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">rgb</span>.<span class="ident">serialize_field</span>(<span class="string">"g"</span>, <span class="kw-2">&</span><span class="self">self</span>.<span class="ident">g</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">rgb</span>.<span class="ident">serialize_field</span>(<span class="string">"b"</span>, <span class="kw-2">&</span><span class="self">self</span>.<span class="ident">b</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">rgb</span>.<span class="ident">end</span>()
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='tymethod.serialize_struct_variant' class='method'><span id='serialize_struct_variant.v' class='invisible'><code>fn <a href='#tymethod.serialize_struct_variant' class='fnname'>serialize_struct_variant</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>></code></span></h3><div class='docblock'><p>Begin to serialize a struct variant like <code>E::S</code> in <code>enum E { S { r: u8, g: u8, b: u8 } }</code>. This call must be followed by zero or more calls to
|
|||
|
<code>serialize_field</code>, then a call to <code>end</code>.</p>
|
|||
|
|
|||
|
<p>The <code>name</code> is the name of the enum, the <code>variant_index</code> is the index of
|
|||
|
this variant within the enum, the <code>variant</code> is the name of the variant,
|
|||
|
and the <code>len</code> is the number of data fields that will be serialized.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::<span class="ident">ser</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>, <span class="ident">SerializeStructVariant</span>};
|
|||
|
|
|||
|
<span class="kw">enum</span> <span class="ident">E</span> {
|
|||
|
<span class="ident">S</span> { <span class="ident">r</span>: <span class="ident">u8</span>, <span class="ident">g</span>: <span class="ident">u8</span>, <span class="ident">b</span>: <span class="ident">u8</span> }
|
|||
|
}
|
|||
|
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">E</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
|
|||
|
<span class="ident">E</span>::<span class="ident">S</span> { <span class="kw-2">ref</span> <span class="ident">r</span>, <span class="kw-2">ref</span> <span class="ident">g</span>, <span class="kw-2">ref</span> <span class="ident">b</span> } <span class="op">=></span> {
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">sv</span> <span class="op">=</span> <span class="ident">serializer</span>.<span class="ident">serialize_struct_variant</span>(<span class="string">"E"</span>, <span class="number">0</span>, <span class="string">"S"</span>, <span class="number">3</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">sv</span>.<span class="ident">serialize_field</span>(<span class="string">"r"</span>, <span class="ident">r</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">sv</span>.<span class="ident">serialize_field</span>(<span class="string">"g"</span>, <span class="ident">g</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">sv</span>.<span class="ident">serialize_field</span>(<span class="string">"b"</span>, <span class="ident">b</span>)<span class="question-mark">?</span>;
|
|||
|
<span class="ident">sv</span>.<span class="ident">end</span>()
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div></div>
|
|||
|
<h2 id='provided-methods'>Provided Methods</h2>
|
|||
|
<div class='methods'>
|
|||
|
<h3 id='method.collect_seq' class='method'><span id='collect_seq.v' class='invisible'><code>fn <a href='#method.collect_seq' class='fnname'>collect_seq</a><I>(self, iter: I) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>> <span class="where fmt-newline">where<br> I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html" title="trait core::iter::traits::IntoIterator">IntoIterator</a>,<br> <I as <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html" title="trait core::iter::traits::IntoIterator">IntoIterator</a>>::<a class="type" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html#associatedtype.Item" title="type core::iter::traits::IntoIterator::Item">Item</a>: <a class="trait" href="../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>, </span></code></span></h3><div class='docblock'><p>Collect an iterator as a sequence.</p>
|
|||
|
|
|||
|
<p>The default implementation serializes each item yielded by the iterator
|
|||
|
using <a href="#tymethod.serialize_seq"><code>serialize_seq</code></a>. Implementors should not need to override this
|
|||
|
method.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
|
|||
|
|
|||
|
<span class="kw">struct</span> <span class="ident">SecretlyOneHigher</span> {
|
|||
|
<span class="ident">data</span>: <span class="ident">Vec</span><span class="op"><</span><span class="ident">i32</span><span class="op">></span>,
|
|||
|
}
|
|||
|
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">SecretlyOneHigher</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">collect_seq</span>(<span class="self">self</span>.<span class="ident">data</span>.<span class="ident">iter</span>().<span class="ident">map</span>(<span class="op">|</span><span class="ident">x</span><span class="op">|</span> <span class="ident">x</span> <span class="op">+</span> <span class="number">1</span>))
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='method.collect_map' class='method'><span id='collect_map.v' class='invisible'><code>fn <a href='#method.collect_map' class='fnname'>collect_map</a><K, V, I>(self, iter: I) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>> <span class="where fmt-newline">where<br> K: <a class="trait" href="../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,<br> V: <a class="trait" href="../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,<br> I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/trait.IntoIterator.html" title="trait core::iter::traits::IntoIterator">IntoIterator</a><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>>, </span></code></span></h3><div class='docblock'><p>Collect an iterator as a map.</p>
|
|||
|
|
|||
|
<p>The default implementation serializes each pair yielded by the iterator
|
|||
|
using <a href="#tymethod.serialize_map"><code>serialize_map</code></a>. Implementors should not need to override this
|
|||
|
method.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BTreeSet</span>;
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
|
|||
|
|
|||
|
<span class="kw">struct</span> <span class="ident">MapToUnit</span> {
|
|||
|
<span class="ident">keys</span>: <span class="ident">BTreeSet</span><span class="op"><</span><span class="ident">i32</span><span class="op">></span>,
|
|||
|
}
|
|||
|
|
|||
|
<span class="comment">// Serializes as a map in which the values are all unit.</span>
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">MapToUnit</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">collect_map</span>(<span class="self">self</span>.<span class="ident">keys</span>.<span class="ident">iter</span>().<span class="ident">map</span>(<span class="op">|</span><span class="ident">k</span><span class="op">|</span> (<span class="ident">k</span>, ())))
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h3 id='method.collect_str' class='method'><span id='collect_str.v' class='invisible'><code>fn <a href='#method.collect_str' class='fnname'>collect_str</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(self, value: &T) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><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>> <span class="where fmt-newline">where<br> T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a>, </span></code></span></h3><div class='docblock'><p>Serialize a string produced by an implementation of <code>Display</code>.</p>
|
|||
|
|
|||
|
<p>The default implementation builds a heap-allocated <a href="https://doc.rust-lang.org/std/string/struct.String.html"><code>String</code></a> and
|
|||
|
delegates to <a href="#tymethod.serialize_str"><code>serialize_str</code></a>. Serializers are encouraged to provide a
|
|||
|
more efficient implementation if possible.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Serializer</span>};
|
|||
|
|
|||
|
<span class="kw">impl</span> <span class="ident">Serialize</span> <span class="kw">for</span> <span class="ident">DateTime</span> {
|
|||
|
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S</span>::<span class="prelude-val">Ok</span>, <span class="ident">S</span>::<span class="ident">Error</span><span class="op">></span>
|
|||
|
<span class="kw">where</span> <span class="ident">S</span>: <span class="ident">Serializer</span>
|
|||
|
{
|
|||
|
<span class="ident">serializer</span>.<span class="ident">collect_str</span>(<span class="kw-2">&</span><span class="macro">format_args</span><span class="macro">!</span>(<span class="string">"{:?}{:?}"</span>,
|
|||
|
<span class="self">self</span>.<span class="ident">naive_local</span>(),
|
|||
|
<span class="self">self</span>.<span class="ident">offset</span>()))
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div></div>
|
|||
|
<h2 id='implementors'>Implementors</h2>
|
|||
|
<ul class='item-list' id='implementors-list'>
|
|||
|
</ul><script type="text/javascript" async
|
|||
|
src="../implementors/serde/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>?</dt>
|
|||
|
<dd>Show this help dialog</dd>
|
|||
|
<dt>S</dt>
|
|||
|
<dd>Focus the search field</dd>
|
|||
|
<dt>⇤</dt>
|
|||
|
<dd>Move up in search results</dd>
|
|||
|
<dt>⇥</dt>
|
|||
|
<dd>Move down in search results</dd>
|
|||
|
<dt>⏎</dt>
|
|||
|
<dd>Go to active search result</dd>
|
|||
|
<dt>+</dt>
|
|||
|
<dd>Collapse/expand all sections</dd>
|
|||
|
</dl>
|
|||
|
</div>
|
|||
|
|
|||
|
<div class="infos">
|
|||
|
<h2>Search Tricks</h2>
|
|||
|
|
|||
|
<p>
|
|||
|
Prefix searches with a type followed by a colon (e.g.
|
|||
|
<code>fn:</code>) to restrict the search to a given type.
|
|||
|
</p>
|
|||
|
|
|||
|
<p>
|
|||
|
Accepted types are: <code>fn</code>, <code>mod</code>,
|
|||
|
<code>struct</code>, <code>enum</code>,
|
|||
|
<code>trait</code>, <code>type</code>, <code>macro</code>,
|
|||
|
and <code>const</code>.
|
|||
|
</p>
|
|||
|
|
|||
|
<p>
|
|||
|
Search functions by type signature (e.g.
|
|||
|
<code>vec -> usize</code> or <code>* -> vec</code>)
|
|||
|
</p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</aside>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<script>
|
|||
|
window.rootPath = "../";
|
|||
|
window.currentCrate = "serde";
|
|||
|
</script>
|
|||
|
<script src="../main.js"></script>
|
|||
|
<script defer src="../search-index.js"></script>
|
|||
|
</body>
|
|||
|
</html>
|