From 6182eceed1a1b72fc265ba853389cacbd04f726f Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 9 May 2020 17:19:19 -0700 Subject: [PATCH] Move the Formatter Serializer to a module Let's keep impls.rs for Serialize impls, which there are enough of already. --- serde/src/ser/fmt.rs | 150 +++++++++++++++++++++++++++++++++++++++++ serde/src/ser/impls.rs | 132 +----------------------------------- serde/src/ser/mod.rs | 1 + 3 files changed, 152 insertions(+), 131 deletions(-) create mode 100644 serde/src/ser/fmt.rs diff --git a/serde/src/ser/fmt.rs b/serde/src/ser/fmt.rs new file mode 100644 index 00000000..65c16562 --- /dev/null +++ b/serde/src/ser/fmt.rs @@ -0,0 +1,150 @@ +use lib::*; +use ser::{Error, Impossible, Serialize, Serializer}; + +impl Error for fmt::Error { + fn custom(_msg: T) -> Self { + fmt::Error + } +} + +macro_rules! fmt_primitives { + ($($f:ident: $t:ty,)*) => { + $( + fn $f(self, v: $t) -> fmt::Result { + write!(self, "{}", v) + } + )* + }; +} + +impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> { + type Ok = (); + type Error = fmt::Error; + type SerializeSeq = Impossible<(), fmt::Error>; + type SerializeTuple = Impossible<(), fmt::Error>; + type SerializeTupleStruct = Impossible<(), fmt::Error>; + type SerializeTupleVariant = Impossible<(), fmt::Error>; + type SerializeMap = Impossible<(), fmt::Error>; + type SerializeStruct = Impossible<(), fmt::Error>; + type SerializeStructVariant = Impossible<(), fmt::Error>; + + fmt_primitives! { + serialize_bool: bool, + serialize_i8: i8, + serialize_i16: i16, + serialize_i32: i32, + serialize_i64: i64, + serialize_u8: u8, + serialize_u16: u16, + serialize_u32: u32, + serialize_u64: u64, + serialize_f32: f32, + serialize_f64: f64, + serialize_char: char, + serialize_str: &str, + serialize_unit_struct: &'static str, + } + + fn serialize_unit(self) -> fmt::Result { + Ok(()) + } + + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + ) -> fmt::Result { + write!(self, "{}", variant) + } + + fn serialize_bytes(self, _v: &[u8]) -> fmt::Result { + Err(fmt::Error) + } + + fn serialize_none(self) -> fmt::Result { + Err(fmt::Error) + } + + fn serialize_some(self, _value: &T) -> fmt::Result + where + T: Serialize, + { + Err(fmt::Error) + } + + fn serialize_newtype_struct(self, _name: &'static str, _value: &T) -> fmt::Result + where + T: Serialize, + { + Err(fmt::Error) + } + + fn serialize_newtype_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _value: &T, + ) -> fmt::Result + where + T: Serialize, + { + Err(fmt::Error) + } + + fn serialize_seq(self, _len: Option) -> Result { + Err(fmt::Error) + } + + fn serialize_tuple(self, _len: usize) -> Result { + Err(fmt::Error) + } + + fn serialize_tuple_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result { + Err(fmt::Error) + } + + fn serialize_tuple_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> Result { + Err(fmt::Error) + } + + fn serialize_map(self, _len: Option) -> Result { + Err(fmt::Error) + } + + fn serialize_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result { + Err(fmt::Error) + } + + fn serialize_struct_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> Result { + Err(fmt::Error) + } + + fn collect_str(self, value: &T) -> fmt::Result + where + T: Display, + { + Display::fmt(value, self) + } +} diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index f78d372f..803c739e 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -1,6 +1,6 @@ use lib::*; -use ser::{Error, Impossible, Serialize, SerializeTuple, Serializer}; +use ser::{Error, Serialize, SerializeTuple, Serializer}; //////////////////////////////////////////////////////////////////////////////// @@ -881,133 +881,3 @@ atomic_impl! { atomic_impl! { AtomicI64 AtomicU64 } - -//////////////////////////////////////////////////////////////////////////////// - -impl Error for fmt::Error { - fn custom(_msg: T) -> Self { - fmt::Error - } -} - -macro_rules! fmt_primitives { - ($($f:ident: $t:ty, )*) => {$( - fn $f(self, v: $t) -> fmt::Result { - write!(self, "{}", v) - } - )*} -} - -impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> { - type Ok = (); - type Error = fmt::Error; - type SerializeSeq = Impossible<(), fmt::Error>; - type SerializeTuple = Impossible<(), fmt::Error>; - type SerializeTupleStruct = Impossible<(), fmt::Error>; - type SerializeTupleVariant = Impossible<(), fmt::Error>; - type SerializeMap = Impossible<(), fmt::Error>; - type SerializeStruct = Impossible<(), fmt::Error>; - type SerializeStructVariant = Impossible<(), fmt::Error>; - - fmt_primitives!( - serialize_str: &str, - serialize_bool: bool, - serialize_i8: i8, - serialize_i16: i16, - serialize_i32: i32, - serialize_i64: i64, - serialize_u8: u8, - serialize_u16: u16, - serialize_u32: u32, - serialize_u64: u64, - serialize_f32: f32, - serialize_f64: f64, - serialize_char: char, - serialize_unit_struct: &'static str, - ); - fn serialize_unit(self) -> fmt::Result { - Ok(()) - } - fn serialize_unit_variant(self, _: &'static str, _: u32, v: &'static str) -> fmt::Result { - write!(self, "{}", v) - } - - fn serialize_bytes(self, _v: &[u8]) -> fmt::Result { - Err(fmt::Error) - } - fn serialize_none(self) -> fmt::Result { - Err(fmt::Error) - } - fn serialize_some(self, _value: &T) -> fmt::Result - where - T: Serialize, - { - Err(fmt::Error) - } - fn serialize_newtype_struct(self, _name: &'static str, _value: &T) -> fmt::Result - where - T: Serialize, - { - Err(fmt::Error) - } - fn serialize_newtype_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _value: &T, - ) -> fmt::Result - where - T: Serialize, - { - Err(fmt::Error) - } - fn serialize_seq(self, _len: Option) -> Result { - Err(fmt::Error) - } - fn serialize_tuple(self, _len: usize) -> Result { - Err(fmt::Error) - } - fn serialize_tuple_struct( - self, - _name: &'static str, - _len: usize, - ) -> Result { - Err(fmt::Error) - } - fn serialize_tuple_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize, - ) -> Result { - Err(fmt::Error) - } - fn serialize_map(self, _len: Option) -> Result { - Err(fmt::Error) - } - fn serialize_struct( - self, - _name: &'static str, - _len: usize, - ) -> Result { - Err(fmt::Error) - } - fn serialize_struct_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize, - ) -> Result { - Err(fmt::Error) - } - - fn collect_str(self, value: &T) -> fmt::Result - where - T: Display, - { - Display::fmt(value, self) - } -} diff --git a/serde/src/ser/mod.rs b/serde/src/ser/mod.rs index 6d71649f..8b57c026 100644 --- a/serde/src/ser/mod.rs +++ b/serde/src/ser/mod.rs @@ -109,6 +109,7 @@ use lib::*; +mod fmt; mod impls; mod impossible;