Debian Codegraph @

.cargo/vendor/serde/src/ser/impossible.rs

raw
1 //! This module contains `Impossible` serializer and its implementations.
2
3 use crate::lib::*;
4
5 use crate::ser::{
6 self, Serialize, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant,
7 SerializeTuple, SerializeTupleStruct, SerializeTupleVariant,
8 };
9
10 /// Helper type for implementing a `Serializer` that does not support
11 /// serializing one of the compound types.
12 ///
13 /// This type cannot be instantiated, but implements every one of the traits
14 /// corresponding to the [`Serializer`] compound types: [`SerializeSeq`],
15 /// [`SerializeTuple`], [`SerializeTupleStruct`], [`SerializeTupleVariant`],
16 /// [`SerializeMap`], [`SerializeStruct`], and [`SerializeStructVariant`].
17 ///
18 /// ```edition2021
19 /// # use serde::ser::{Serializer, Impossible};
20 /// # use serde::__private::doc::Error;
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 only
36 /// // thing we can do here is to return an error.
37 /// # stringify! {
38 /// Err(...)
39 /// # };
40 /// # unimplemented!()
41 /// }
42 ///
43 /// /* other Serializer methods */
44 /// # serde::__serialize_unimplemented! {
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_variant
47 /// # tuple tuple_struct tuple_variant map struct struct_variant
48 /// # }
49 /// }
50 /// ```
51 ///
52 /// [`Serializer`]: trait.Serializer.html
53 /// [`SerializeSeq`]: trait.SerializeSeq.html
54 /// [`SerializeTuple`]: trait.SerializeTuple.html
55 /// [`SerializeTupleStruct`]: trait.SerializeTupleStruct.html
56 /// [`SerializeTupleVariant`]: trait.SerializeTupleVariant.html
57 /// [`SerializeMap`]: trait.SerializeMap.html
58 /// [`SerializeStruct`]: trait.SerializeStruct.html
59 /// [`SerializeStructVariant`]: trait.SerializeStructVariant.html
60 pub struct Impossible<Ok, Error> {
61 void: Void,
62 ok: PhantomData<Ok>,
63 error: PhantomData<Error>,
64 }
65
66 enum Void {}
67
68 impl<Ok, Error> SerializeSeq for Impossible<Ok, Error>
69 where
70 Error: ser::Error,
71 {
72 type Ok = Ok;
73 type Error = Error;
74
75 fn serialize_element<T>(&mut self, value: &T) -> Result<(), Error>
76 where
77 T: ?Sized + Serialize,
78 {
79 let _ = value;
80 match self.void {}
81 }
82
83 fn end(self) -> Result<Ok, Error> {
84 match self.void {}
85 }
86 }
87
88 impl<Ok, Error> SerializeTuple for Impossible<Ok, Error>
89 where
90 Error: ser::Error,
91 {
92 type Ok = Ok;
93 type Error = Error;
94
95 fn serialize_element<T>(&mut self, value: &T) -> Result<(), Error>
96 where
97 T: ?Sized + Serialize,
98 {
99 let _ = value;
100 match self.void {}
101 }
102
103 fn end(self) -> Result<Ok, Error> {
104 match self.void {}
105 }
106 }
107
108 impl<Ok, Error> SerializeTupleStruct for Impossible<Ok, Error>
109 where
110 Error: ser::Error,
111 {
112 type Ok = Ok;
113 type Error = Error;
114
115 fn serialize_field<T>(&mut self, value: &T) -> Result<(), Error>
116 where
117 T: ?Sized + Serialize,
118 {
119 let _ = value;
120 match self.void {}
121 }
122
123 fn end(self) -> Result<Ok, Error> {
124 match self.void {}
125 }
126 }
127
128 impl<Ok, Error> SerializeTupleVariant for Impossible<Ok, Error>
129 where
130 Error: ser::Error,
131 {
132 type Ok = Ok;
133 type Error = Error;
134
135 fn serialize_field<T>(&mut self, value: &T) -> Result<(), Error>
136 where
137 T: ?Sized + Serialize,
138 {
139 let _ = value;
140 match self.void {}
141 }
142
143 fn end(self) -> Result<Ok, Error> {
144 match self.void {}
145 }
146 }
147
148 impl<Ok, Error> SerializeMap for Impossible<Ok, Error>
149 where
150 Error: ser::Error,
151 {
152 type Ok = Ok;
153 type Error = Error;
154
155 fn serialize_key<T>(&mut self, key: &T) -> Result<(), Error>
156 where
157 T: ?Sized + Serialize,
158 {
159 let _ = key;
160 match self.void {}
161 }
162
163 fn serialize_value<T>(&mut self, value: &T) -> Result<(), Error>
164 where
165 T: ?Sized + Serialize,
166 {
167 let _ = value;
168 match self.void {}
169 }
170
171 fn end(self) -> Result<Ok, Error> {
172 match self.void {}
173 }
174 }
175
176 impl<Ok, Error> SerializeStruct for Impossible<Ok, Error>
177 where
178 Error: ser::Error,
179 {
180 type Ok = Ok;
181 type Error = Error;
182
183 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Error>
184 where
185 T: ?Sized + Serialize,
186 {
187 let _ = key;
188 let _ = value;
189 match self.void {}
190 }
191
192 fn end(self) -> Result<Ok, Error> {
193 match self.void {}
194 }
195 }
196
197 impl<Ok, Error> SerializeStructVariant for Impossible<Ok, Error>
198 where
199 Error: ser::Error,
200 {
201 type Ok = Ok;
202 type Error = Error;
203
204 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Error>
205 where
206 T: ?Sized + Serialize,
207 {
208 let _ = key;
209 let _ = value;
210 match self.void {}
211 }
212
213 fn end(self) -> Result<Ok, Error> {
214 match self.void {}
215 }
216 }