diff --git a/serde/src/macros.rs b/serde/src/macros.rs index 7505846c..9f958f08 100644 --- a/serde/src/macros.rs +++ b/serde/src/macros.rs @@ -209,3 +209,123 @@ macro_rules! serialize_display_bounded_length { } } } + +#[doc(hidden)] +#[macro_export] +macro_rules! __serialize_unimplemented_method { + ($func:ident $(<$t:ident>)* ($($arg:ty),*) -> $ret:ident) => { + fn $func $(<$t: ?Sized + $crate::Serialize>)* (self $(, _: $arg)*) -> $crate::export::Result { + unimplemented!() + } + }; +} + +#[doc(hidden)] +#[macro_export] +macro_rules! __serialize_unimplemented_helper { + (bool) => { + __serialize_unimplemented_method!(serialize_bool(bool) -> Ok); + }; + (i8) => { + __serialize_unimplemented_method!(serialize_i8(i8) -> Ok); + }; + (i16) => { + __serialize_unimplemented_method!(serialize_i16(i16) -> Ok); + }; + (i32) => { + __serialize_unimplemented_method!(serialize_i32(i32) -> Ok); + }; + (i64) => { + __serialize_unimplemented_method!(serialize_i64(i64) -> Ok); + }; + (u8) => { + __serialize_unimplemented_method!(serialize_u8(u8) -> Ok); + }; + (u16) => { + __serialize_unimplemented_method!(serialize_u16(u16) -> Ok); + }; + (u32) => { + __serialize_unimplemented_method!(serialize_u32(u32) -> Ok); + }; + (u64) => { + __serialize_unimplemented_method!(serialize_u64(u64) -> Ok); + }; + (f32) => { + __serialize_unimplemented_method!(serialize_f32(f32) -> Ok); + }; + (f64) => { + __serialize_unimplemented_method!(serialize_f64(f64) -> Ok); + }; + (char) => { + __serialize_unimplemented_method!(serialize_char(char) -> Ok); + }; + (str) => { + __serialize_unimplemented_method!(serialize_str(&str) -> Ok); + }; + (bytes) => { + __serialize_unimplemented_method!(serialize_bytes(&[u8]) -> Ok); + }; + (none) => { + __serialize_unimplemented_method!(serialize_none() -> Ok); + }; + (some) => { + __serialize_unimplemented_method!(serialize_some(&T) -> Ok); + }; + (unit) => { + __serialize_unimplemented_method!(serialize_unit() -> Ok); + }; + (unit_struct) => { + __serialize_unimplemented_method!(serialize_unit_struct(&str) -> Ok); + }; + (unit_variant) => { + __serialize_unimplemented_method!(serialize_unit_variant(&str, usize, &str) -> Ok); + }; + (newtype_struct) => { + __serialize_unimplemented_method!(serialize_newtype_struct(&str, &T) -> Ok); + }; + (newtype_variant) => { + __serialize_unimplemented_method!(serialize_newtype_variant(&str, usize, &str, &T) -> Ok); + }; + (seq) => { + type SerializeSeq = $crate::ser::Impossible; + __serialize_unimplemented_method!(serialize_seq(Option) -> SerializeSeq); + }; + (seq_fixed_size) => { + __serialize_unimplemented_method!(serialize_seq_fixed_size(usize) -> SerializeSeq); + }; + (tuple) => { + type SerializeTuple = $crate::ser::Impossible; + __serialize_unimplemented_method!(serialize_tuple(usize) -> SerializeTuple); + }; + (tuple_struct) => { + type SerializeTupleStruct = $crate::ser::Impossible; + __serialize_unimplemented_method!(serialize_tuple_struct(&str, usize) -> SerializeTupleStruct); + }; + (tuple_variant) => { + type SerializeTupleVariant = $crate::ser::Impossible; + __serialize_unimplemented_method!(serialize_tuple_variant(&str, usize, &str, usize) -> SerializeTupleVariant); + }; + (map) => { + type SerializeMap = $crate::ser::Impossible; + __serialize_unimplemented_method!(serialize_map(Option) -> SerializeMap); + }; + (struct) => { + type SerializeStruct = $crate::ser::Impossible; + __serialize_unimplemented_method!(serialize_struct(&str, usize) -> SerializeStruct); + }; + (struct_variant) => { + type SerializeStructVariant = $crate::ser::Impossible; + __serialize_unimplemented_method!(serialize_struct_variant(&str, usize, &str, usize) -> SerializeStructVariant); + }; +} + +/// Used only by Serde doc tests. Not public API. +#[doc(hidden)] +#[macro_export] +macro_rules! __serialize_unimplemented { + ($($func:ident)*) => { + $( + __serialize_unimplemented_helper!($func); + )* + }; +} diff --git a/serde/src/ser/impossible.rs b/serde/src/ser/impossible.rs index 6b8e7c28..e4aedba7 100644 --- a/serde/src/ser/impossible.rs +++ b/serde/src/ser/impossible.rs @@ -13,7 +13,15 @@ use ser::{self, Serialize, SerializeSeq, SerializeTuple, SerializeTupleStruct, /// `SerializeTuple`, `SerializeTupleStruct`, `SerializeTupleVariant`, /// `SerializeMap`, `SerializeStruct`, and `SerializeStructVariant`. /// -/// ```rust,ignore +/// ```rust +/// # #[macro_use] +/// # extern crate serde; +/// # +/// # use serde::ser::{Serializer, Impossible}; +/// # use serde::ser::private::Error; +/// # +/// # struct MySerializer; +/// # /// impl Serializer for MySerializer { /// type Ok = (); /// type Error = Error; @@ -27,11 +35,22 @@ use ser::{self, Serialize, SerializeSeq, SerializeTuple, SerializeTupleStruct, /// -> Result { /// // Given Impossible cannot be instantiated, the only /// // thing we can do here is to return an error. +/// # macro_rules! ellipses { () => { /// Err(...) +/// # } } +/// # unimplemented!() /// } /// /// /* other Serializer methods */ +/// # __serialize_unimplemented! { +/// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str bytes none some +/// # unit unit_struct unit_variant newtype_struct newtype_variant +/// # seq_fixed_size tuple tuple_struct tuple_variant map struct +/// # struct_variant +/// # } /// } +/// # +/// # fn main() {} /// ``` pub struct Impossible { void: Void, diff --git a/serde/src/ser/private.rs b/serde/src/ser/private.rs index 6e30cb72..5d1f723f 100644 --- a/serde/src/ser/private.rs +++ b/serde/src/ser/private.rs @@ -5,6 +5,9 @@ use ser::{self, Serialize, Serializer, SerializeMap, SerializeStruct, Impossible #[cfg(any(feature = "std", feature = "collections"))] use ser::content::{SerializeTupleVariantAsMapValue, SerializeStructVariantAsMapValue}; +#[cfg(feature = "std")] +use std::error; + /// Not public API. pub fn serialize_tagged_newtype(serializer: S, type_ident: &'static str, @@ -71,31 +74,15 @@ impl Display for Unsupported { } } -struct Error { - type_ident: &'static str, - variant_ident: &'static str, - ty: Unsupported, -} - -impl Display for Error { - fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, - "cannot serialize tagged newtype variant {}::{} containing {}", - self.type_ident, - self.variant_ident, - self.ty) - } -} - impl TaggedSerializer where S: Serializer { fn bad_type(self, what: Unsupported) -> S::Error { - ser::Error::custom(Error { - type_ident: self.type_ident, - variant_ident: self.variant_ident, - ty: what, - }) + ser::Error::custom(format_args!( + "cannot serialize tagged newtype variant {}::{} containing {}", + self.type_ident, + self.variant_ident, + what)) } } @@ -320,3 +307,27 @@ impl Serializer for TaggedSerializer Err(self.bad_type(Unsupported::String)) } } + +/// Used only by Serde doc tests. Not public API. +#[doc(hidden)] +#[derive(Debug)] +pub struct Error; + +impl ser::Error for Error { + fn custom(_: T) -> Self { + unimplemented!() + } +} + +#[cfg(feature = "std")] +impl error::Error for Error { + fn description(&self) -> &str { + unimplemented!() + } +} + +impl Display for Error { + fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result { + unimplemented!() + } +}