]> git.proxmox.com Git - cargo.git/blob - vendor/serde-1.0.15/src/ser/impossible.rs
New upstream version 0.23.0
[cargo.git] / vendor / serde-1.0.15 / src / ser / impossible.rs
1 // Copyright 2017 Serde Developers
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 //! This module contains `Impossible` serializer and its implementations.
10
11 use lib::*;
12
13 use ser::{self, Serialize, SerializeSeq, SerializeTuple, SerializeTupleStruct,
14 SerializeTupleVariant, SerializeMap, SerializeStruct, SerializeStructVariant};
15
16 /// Helper type for implementing a `Serializer` that does not support
17 /// serializing one of the compound types.
18 ///
19 /// This type cannot be instantiated, but implements every one of the traits
20 /// corresponding to the [`Serializer`] compound types: [`SerializeSeq`],
21 /// [`SerializeTuple`], [`SerializeTupleStruct`], [`SerializeTupleVariant`],
22 /// [`SerializeMap`], [`SerializeStruct`], and [`SerializeStructVariant`].
23 ///
24 /// ```rust
25 /// # #[macro_use]
26 /// # extern crate serde;
27 /// #
28 /// # use serde::ser::{Serializer, Impossible};
29 /// # use serde::private::ser::Error;
30 /// #
31 /// # struct MySerializer;
32 /// #
33 /// impl Serializer for MySerializer {
34 /// type Ok = ();
35 /// type Error = Error;
36 ///
37 /// type SerializeSeq = Impossible<(), Error>;
38 /// /* other associated types */
39 ///
40 /// /// This data format does not support serializing sequences.
41 /// fn serialize_seq(self,
42 /// len: Option<usize>)
43 /// -> Result<Self::SerializeSeq, Error> {
44 /// // Given Impossible cannot be instantiated, the only
45 /// // thing we can do here is to return an error.
46 /// # stringify! {
47 /// Err(...)
48 /// # };
49 /// # unimplemented!()
50 /// }
51 ///
52 /// /* other Serializer methods */
53 /// # __serialize_unimplemented! {
54 /// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str bytes none some
55 /// # unit unit_struct unit_variant newtype_struct newtype_variant
56 /// # tuple tuple_struct tuple_variant map struct struct_variant
57 /// # }
58 /// }
59 /// #
60 /// # fn main() {}
61 /// ```
62 ///
63 /// [`Serializer`]: trait.Serializer.html
64 /// [`SerializeSeq`]: trait.SerializeSeq.html
65 /// [`SerializeTuple`]: trait.SerializeTuple.html
66 /// [`SerializeTupleStruct`]: trait.SerializeTupleStruct.html
67 /// [`SerializeTupleVariant`]: trait.SerializeTupleVariant.html
68 /// [`SerializeMap`]: trait.SerializeMap.html
69 /// [`SerializeStruct`]: trait.SerializeStruct.html
70 /// [`SerializeStructVariant`]: trait.SerializeStructVariant.html
71 pub struct Impossible<Ok, Error> {
72 void: Void,
73 ok: PhantomData<Ok>,
74 error: PhantomData<Error>,
75 }
76
77 enum Void {}
78
79 impl<Ok, Error> SerializeSeq for Impossible<Ok, Error>
80 where
81 Error: ser::Error,
82 {
83 type Ok = Ok;
84 type Error = Error;
85
86 fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
87 where
88 T: Serialize,
89 {
90 let _ = value;
91 match self.void {}
92 }
93
94 fn end(self) -> Result<Ok, Error> {
95 match self.void {}
96 }
97 }
98
99 impl<Ok, Error> SerializeTuple for Impossible<Ok, Error>
100 where
101 Error: ser::Error,
102 {
103 type Ok = Ok;
104 type Error = Error;
105
106 fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
107 where
108 T: Serialize,
109 {
110 let _ = value;
111 match self.void {}
112 }
113
114 fn end(self) -> Result<Ok, Error> {
115 match self.void {}
116 }
117 }
118
119 impl<Ok, Error> SerializeTupleStruct for Impossible<Ok, Error>
120 where
121 Error: ser::Error,
122 {
123 type Ok = Ok;
124 type Error = Error;
125
126 fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
127 where
128 T: Serialize,
129 {
130 let _ = value;
131 match self.void {}
132 }
133
134 fn end(self) -> Result<Ok, Error> {
135 match self.void {}
136 }
137 }
138
139 impl<Ok, Error> SerializeTupleVariant for Impossible<Ok, Error>
140 where
141 Error: ser::Error,
142 {
143 type Ok = Ok;
144 type Error = Error;
145
146 fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
147 where
148 T: Serialize,
149 {
150 let _ = value;
151 match self.void {}
152 }
153
154 fn end(self) -> Result<Ok, Error> {
155 match self.void {}
156 }
157 }
158
159 impl<Ok, Error> SerializeMap for Impossible<Ok, Error>
160 where
161 Error: ser::Error,
162 {
163 type Ok = Ok;
164 type Error = Error;
165
166 fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Error>
167 where
168 T: Serialize,
169 {
170 let _ = key;
171 match self.void {}
172 }
173
174 fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
175 where
176 T: Serialize,
177 {
178 let _ = value;
179 match self.void {}
180 }
181
182 fn end(self) -> Result<Ok, Error> {
183 match self.void {}
184 }
185 }
186
187 impl<Ok, Error> SerializeStruct for Impossible<Ok, Error>
188 where
189 Error: ser::Error,
190 {
191 type Ok = Ok;
192 type Error = Error;
193
194 fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Error>
195 where
196 T: Serialize,
197 {
198 let _ = key;
199 let _ = value;
200 match self.void {}
201 }
202
203 fn end(self) -> Result<Ok, Error> {
204 match self.void {}
205 }
206 }
207
208 impl<Ok, Error> SerializeStructVariant for Impossible<Ok, Error>
209 where
210 Error: ser::Error,
211 {
212 type Ok = Ok;
213 type Error = Error;
214
215 fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Error>
216 where
217 T: Serialize,
218 {
219 let _ = key;
220 let _ = value;
221 match self.void {}
222 }
223
224 fn end(self) -> Result<Ok, Error> {
225 match self.void {}
226 }
227 }