]> git.proxmox.com Git - rustc.git/blame - src/vendor/serde-0.9.15/src/ser/impossible.rs
New upstream version 1.19.0+dfsg3
[rustc.git] / src / vendor / serde-0.9.15 / src / ser / impossible.rs
CommitLineData
7cac9316
XL
1//! This module contains `Impossible` serializer and its implementations.
2
3use core::marker::PhantomData;
4
5use ser::{self, Serialize, SerializeSeq, SerializeTuple, SerializeTupleStruct,
6 SerializeTupleVariant, SerializeMap, SerializeStruct, SerializeStructVariant};
7
8/// Helper type for implementing a `Serializer` that does not support
9/// serializing one of the compound types.
10///
11/// This type cannot be instantiated, but implements every one of the traits
12/// corresponding to the `Serializer` compound types: `SerializeSeq`,
13/// `SerializeTuple`, `SerializeTupleStruct`, `SerializeTupleVariant`,
14/// `SerializeMap`, `SerializeStruct`, and `SerializeStructVariant`.
15///
16/// ```rust,ignore
17/// impl Serializer for MySerializer {
18/// type Ok = ();
19/// type Error = Error;
20///
21/// type SerializeSeq = Impossible<(), Error>;
22/// /* other associated types */
23///
24/// /// This data format does not support serializing sequences.
25/// fn serialize_seq(self,
26/// len: Option<usize>)
27/// -> Result<Self::SerializeSeq, Error> {
28/// // Given Impossible cannot be instantiated, the only
29/// // thing we can do here is to return an error.
30/// Err(...)
31/// }
32///
33/// /* other Serializer methods */
34/// }
35/// ```
36pub struct Impossible<Ok, E> {
37 void: Void,
38 _marker: PhantomData<(Ok, E)>,
39}
40
41enum Void {}
42
43impl<Ok, E> SerializeSeq for Impossible<Ok, E>
44 where E: ser::Error
45{
46 type Ok = Ok;
47 type Error = E;
48
49 fn serialize_element<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
50 match self.void {}
51 }
52
53 fn end(self) -> Result<Ok, E> {
54 match self.void {}
55 }
56}
57
58impl<Ok, E> SerializeTuple for Impossible<Ok, E>
59 where E: ser::Error
60{
61 type Ok = Ok;
62 type Error = E;
63
64 fn serialize_element<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
65 match self.void {}
66 }
67
68 fn end(self) -> Result<Ok, E> {
69 match self.void {}
70 }
71}
72
73impl<Ok, E> SerializeTupleStruct for Impossible<Ok, E>
74 where E: ser::Error
75{
76 type Ok = Ok;
77 type Error = E;
78
79 fn serialize_field<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
80 match self.void {}
81 }
82
83 fn end(self) -> Result<Ok, E> {
84 match self.void {}
85 }
86}
87
88impl<Ok, E> SerializeTupleVariant for Impossible<Ok, E>
89 where E: ser::Error
90{
91 type Ok = Ok;
92 type Error = E;
93
94 fn serialize_field<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
95 match self.void {}
96 }
97
98 fn end(self) -> Result<Ok, E> {
99 match self.void {}
100 }
101}
102
103impl<Ok, E> SerializeMap for Impossible<Ok, E>
104 where E: ser::Error
105{
106 type Ok = Ok;
107 type Error = E;
108
109 fn serialize_key<T: ?Sized + Serialize>(&mut self, _key: &T) -> Result<(), E> {
110 match self.void {}
111 }
112
113 fn serialize_value<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
114 match self.void {}
115 }
116
117 fn end(self) -> Result<Ok, E> {
118 match self.void {}
119 }
120}
121
122impl<Ok, E> SerializeStruct for Impossible<Ok, E>
123 where E: ser::Error
124{
125 type Ok = Ok;
126 type Error = E;
127
128 fn serialize_field<T: ?Sized + Serialize>(&mut self,
129 _key: &'static str,
130 _value: &T)
131 -> Result<(), E> {
132 match self.void {}
133 }
134
135 fn end(self) -> Result<Ok, E> {
136 match self.void {}
137 }
138}
139
140impl<Ok, E> SerializeStructVariant for Impossible<Ok, E>
141 where E: ser::Error
142{
143 type Ok = Ok;
144 type Error = E;
145
146 fn serialize_field<T: ?Sized + Serialize>(&mut self,
147 _key: &'static str,
148 _value: &T)
149 -> Result<(), E> {
150 match self.void {}
151 }
152
153 fn end(self) -> Result<Ok, E> {
154 match self.void {}
155 }
156}