Skip to content
Myr

impossible.rs

This file

Path

serde_core/src/ser/impossible.rs

216 lines · 5.2 KiB · Rust

Last changed by

Merge pull request #2970 from dtolnay/coredoctestDavid Tolnaybefore adoption69aa109

History of this file

Take it

Merge pull request #2608 from osiewicz/extract_serde_coreDavid Tolnaybefore adoption723fcac

lines 1–19why, one step back
1//! This module contains `Impossible` serializer and its implementations.
23use crate::lib::*;45use crate::ser::{6 self, Serialize, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant,7 SerializeTuple, SerializeTupleStruct, SerializeTupleVariant,8};910/// Helper type for implementing a `Serializer` that does not support11/// serializing one of the compound types.12///13/// This type cannot be instantiated, but implements every one of the traits14/// corresponding to the [`Serializer`] compound types: [`SerializeSeq`],15/// [`SerializeTuple`], [`SerializeTupleStruct`], [`SerializeTupleVariant`],16/// [`SerializeMap`], [`SerializeStruct`], and [`SerializeStructVariant`].17///18/// ```edition202119/// # use serde::ser::{Serializer, Impossible};

Merge pull request #2970 from dtolnay/coredoctestDavid Tolnaybefore adoption69aa109

line 20why, one step back
20/// # use serde_core::__private::doc::Error;

Merge pull request #2608 from osiewicz/extract_serde_coreDavid Tolnaybefore adoption723fcac

lines 21–43why, one step back
21/// #
22/// # struct MySerializer;23/// #24/// impl Serializer for MySerializer {25/// type Ok = ();26/// type Error = Error;27///28/// type SerializeSeq = Impossible<(), Error>;29/// /* other associated types */30///31/// /// This data format does not support serializing sequences.32/// fn serialize_seq(self,33/// len: Option<usize>)34/// -> Result<Self::SerializeSeq, Error> {35/// // Given Impossible cannot be instantiated, the only36/// // thing we can do here is to return an error.37/// # stringify! {38/// Err(...)39/// # };40/// # unimplemented!()41/// }42///43/// /* other Serializer methods */

Merge pull request #2970 from dtolnay/coredoctestDavid Tolnaybefore adoption69aa109

line 44why, one step back
44/// # serde_core::__serialize_unimplemented! {

Merge pull request #2608 from osiewicz/extract_serde_coreDavid Tolnaybefore adoption723fcac

lines 45–216why, one step back
45/// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str bytes none some
46/// # unit unit_struct unit_variant newtype_struct newtype_variant47/// # tuple tuple_struct tuple_variant map struct struct_variant48/// # }49/// }50/// ```51///52/// [`Serializer`]: crate::Serializer53/// [`SerializeSeq`]: crate::ser::SerializeSeq54/// [`SerializeTuple`]: crate::ser::SerializeTuple55/// [`SerializeTupleStruct`]: crate::ser::SerializeTupleStruct56/// [`SerializeTupleVariant`]: crate::ser::SerializeTupleVariant57/// [`SerializeMap`]: crate::ser::SerializeMap58/// [`SerializeStruct`]: crate::ser::SerializeStruct59/// [`SerializeStructVariant`]: crate::ser::SerializeStructVariant60pub struct Impossible<Ok, Error> {61 void: Void,62 ok: PhantomData<Ok>,63 error: PhantomData<Error>,64}6566enum Void {}6768impl<Ok, Error> SerializeSeq for Impossible<Ok, Error>69where70 Error: ser::Error,71{72 type Ok = Ok;73 type Error = Error;7475 fn serialize_element<T>(&mut self, value: &T) -> Result<(), Error>76 where77 T: ?Sized + Serialize,78 {79 let _ = value;80 match self.void {}81 }8283 fn end(self) -> Result<Ok, Error> {84 match self.void {}85 }86}8788impl<Ok, Error> SerializeTuple for Impossible<Ok, Error>89where90 Error: ser::Error,91{92 type Ok = Ok;93 type Error = Error;9495 fn serialize_element<T>(&mut self, value: &T) -> Result<(), Error>96 where97 T: ?Sized + Serialize,98 {99 let _ = value;100 match self.void {}101 }102103 fn end(self) -> Result<Ok, Error> {104 match self.void {}105 }106}107108impl<Ok, Error> SerializeTupleStruct for Impossible<Ok, Error>109where110 Error: ser::Error,111{112 type Ok = Ok;113 type Error = Error;114115 fn serialize_field<T>(&mut self, value: &T) -> Result<(), Error>116 where117 T: ?Sized + Serialize,118 {119 let _ = value;120 match self.void {}121 }122123 fn end(self) -> Result<Ok, Error> {124 match self.void {}125 }126}127128impl<Ok, Error> SerializeTupleVariant for Impossible<Ok, Error>129where130 Error: ser::Error,131{132 type Ok = Ok;133 type Error = Error;134135 fn serialize_field<T>(&mut self, value: &T) -> Result<(), Error>136 where137 T: ?Sized + Serialize,138 {139 let _ = value;140 match self.void {}141 }142143 fn end(self) -> Result<Ok, Error> {144 match self.void {}145 }146}147148impl<Ok, Error> SerializeMap for Impossible<Ok, Error>149where150 Error: ser::Error,151{152 type Ok = Ok;153 type Error = Error;154155 fn serialize_key<T>(&mut self, key: &T) -> Result<(), Error>156 where157 T: ?Sized + Serialize,158 {159 let _ = key;160 match self.void {}161 }162163 fn serialize_value<T>(&mut self, value: &T) -> Result<(), Error>164 where165 T: ?Sized + Serialize,166 {167 let _ = value;168 match self.void {}169 }170171 fn end(self) -> Result<Ok, Error> {172 match self.void {}173 }174}175176impl<Ok, Error> SerializeStruct for Impossible<Ok, Error>177where178 Error: ser::Error,179{180 type Ok = Ok;181 type Error = Error;182183 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Error>184 where185 T: ?Sized + Serialize,186 {187 let _ = key;188 let _ = value;189 match self.void {}190 }191192 fn end(self) -> Result<Ok, Error> {193 match self.void {}194 }195}196197impl<Ok, Error> SerializeStructVariant for Impossible<Ok, Error>198where199 Error: ser::Error,200{201 type Ok = Ok;202 type Error = Error;203204 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Error>205 where206 T: ?Sized + Serialize,207 {208 let _ = key;209 let _ = value;210 match self.void {}211 }212213 fn end(self) -> Result<Ok, Error> {214 match self.void {}215 }216}