1 use lib::*;
2 use ser::{Error, Impossible, Serialize, Serializer};
3 
4 impl Error for fmt::Error {
custom<T: Display>(_msg: T) -> Self5     fn custom<T: Display>(_msg: T) -> Self {
6         fmt::Error
7     }
8 }
9 
10 macro_rules! fmt_primitives {
11     ($($f:ident: $t:ty,)*) => {
12         $(
13             fn $f(self, v: $t) -> fmt::Result {
14                 Display::fmt(&v, self)
15             }
16         )*
17     };
18 }
19 
20 /// ```edition2018
21 /// use serde::Serialize;
22 /// use std::fmt::{self, Display};
23 ///
24 /// #[derive(Serialize)]
25 /// #[serde(rename_all = "kebab-case")]
26 /// pub enum MessageType {
27 ///     StartRequest,
28 ///     EndRequest,
29 /// }
30 ///
31 /// impl Display for MessageType {
32 ///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33 ///         self.serialize(f)
34 ///     }
35 /// }
36 /// ```
37 impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> {
38     type Ok = ();
39     type Error = fmt::Error;
40     type SerializeSeq = Impossible<(), fmt::Error>;
41     type SerializeTuple = Impossible<(), fmt::Error>;
42     type SerializeTupleStruct = Impossible<(), fmt::Error>;
43     type SerializeTupleVariant = Impossible<(), fmt::Error>;
44     type SerializeMap = Impossible<(), fmt::Error>;
45     type SerializeStruct = Impossible<(), fmt::Error>;
46     type SerializeStructVariant = Impossible<(), fmt::Error>;
47 
48     fmt_primitives! {
49         serialize_bool: bool,
50         serialize_i8: i8,
51         serialize_i16: i16,
52         serialize_i32: i32,
53         serialize_i64: i64,
54         serialize_u8: u8,
55         serialize_u16: u16,
56         serialize_u32: u32,
57         serialize_u64: u64,
58         serialize_f32: f32,
59         serialize_f64: f64,
60         serialize_char: char,
61         serialize_str: &str,
62         serialize_unit_struct: &'static str,
63     }
64 
65     serde_if_integer128! {
66         fmt_primitives! {
67             serialize_i128: i128,
68             serialize_u128: u128,
69         }
70     }
71 
serialize_unit_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, ) -> fmt::Result72     fn serialize_unit_variant(
73         self,
74         _name: &'static str,
75         _variant_index: u32,
76         variant: &'static str,
77     ) -> fmt::Result {
78         Display::fmt(variant, self)
79     }
80 
serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, value: &T) -> fmt::Result where T: Serialize,81     fn serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, value: &T) -> fmt::Result
82     where
83         T: Serialize,
84     {
85         Serialize::serialize(value, self)
86     }
87 
serialize_bytes(self, _v: &[u8]) -> fmt::Result88     fn serialize_bytes(self, _v: &[u8]) -> fmt::Result {
89         Err(fmt::Error)
90     }
91 
serialize_none(self) -> fmt::Result92     fn serialize_none(self) -> fmt::Result {
93         Err(fmt::Error)
94     }
95 
serialize_some<T: ?Sized>(self, _value: &T) -> fmt::Result where T: Serialize,96     fn serialize_some<T: ?Sized>(self, _value: &T) -> fmt::Result
97     where
98         T: Serialize,
99     {
100         Err(fmt::Error)
101     }
102 
serialize_unit(self) -> fmt::Result103     fn serialize_unit(self) -> fmt::Result {
104         Err(fmt::Error)
105     }
106 
serialize_newtype_variant<T: ?Sized>( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _value: &T, ) -> fmt::Result where T: Serialize,107     fn serialize_newtype_variant<T: ?Sized>(
108         self,
109         _name: &'static str,
110         _variant_index: u32,
111         _variant: &'static str,
112         _value: &T,
113     ) -> fmt::Result
114     where
115         T: Serialize,
116     {
117         Err(fmt::Error)
118     }
119 
serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, fmt::Error>120     fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, fmt::Error> {
121         Err(fmt::Error)
122     }
123 
serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, fmt::Error>124     fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, fmt::Error> {
125         Err(fmt::Error)
126     }
127 
serialize_tuple_struct( self, _name: &'static str, _len: usize, ) -> Result<Self::SerializeTupleStruct, fmt::Error>128     fn serialize_tuple_struct(
129         self,
130         _name: &'static str,
131         _len: usize,
132     ) -> Result<Self::SerializeTupleStruct, fmt::Error> {
133         Err(fmt::Error)
134     }
135 
serialize_tuple_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result<Self::SerializeTupleVariant, fmt::Error>136     fn serialize_tuple_variant(
137         self,
138         _name: &'static str,
139         _variant_index: u32,
140         _variant: &'static str,
141         _len: usize,
142     ) -> Result<Self::SerializeTupleVariant, fmt::Error> {
143         Err(fmt::Error)
144     }
145 
serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, fmt::Error>146     fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, fmt::Error> {
147         Err(fmt::Error)
148     }
149 
serialize_struct( self, _name: &'static str, _len: usize, ) -> Result<Self::SerializeStruct, fmt::Error>150     fn serialize_struct(
151         self,
152         _name: &'static str,
153         _len: usize,
154     ) -> Result<Self::SerializeStruct, fmt::Error> {
155         Err(fmt::Error)
156     }
157 
serialize_struct_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result<Self::SerializeStructVariant, fmt::Error>158     fn serialize_struct_variant(
159         self,
160         _name: &'static str,
161         _variant_index: u32,
162         _variant: &'static str,
163         _len: usize,
164     ) -> Result<Self::SerializeStructVariant, fmt::Error> {
165         Err(fmt::Error)
166     }
167 
collect_str<T: ?Sized>(self, value: &T) -> fmt::Result where T: Display,168     fn collect_str<T: ?Sized>(self, value: &T) -> fmt::Result
169     where
170         T: Display,
171     {
172         Display::fmt(value, self)
173     }
174 }
175