Add integer128 to Serde traits

This commit is contained in:
David Tolnay
2018-05-19 15:14:02 -07:00
parent 5510f758f8
commit fdb51cc7dc
3 changed files with 133 additions and 19 deletions
+54 -6
View File
@@ -52,8 +52,8 @@
//! //!
//! - **Primitive types**: //! - **Primitive types**:
//! - bool //! - bool
//! - i8, i16, i32, i64, isize //! - i8, i16, i32, i64, i128, isize
//! - u8, u16, u32, u64, usize //! - u8, u16, u32, u64, u128, usize
//! - f32, f64 //! - f32, f64
//! - char //! - char
//! - **Compound types**: //! - **Compound types**:
@@ -757,7 +757,7 @@ where
/// Serde. /// Serde.
/// ///
/// The role of this trait is to define the deserialization half of the Serde /// The role of this trait is to define the deserialization half of the Serde
/// data model, which is a way to categorize every Rust data type into one of 27 /// data model, which is a way to categorize every Rust data type into one of 29
/// possible types. Each method of the `Serializer` trait corresponds to one of /// possible types. Each method of the `Serializer` trait corresponds to one of
/// the types of the data model. /// the types of the data model.
/// ///
@@ -767,10 +767,10 @@ where
/// ///
/// The types that make up the Serde data model are: /// The types that make up the Serde data model are:
/// ///
/// - **12 primitive types** /// - **14 primitive types**
/// - bool /// - bool
/// - i8, i16, i32, i64 /// - i8, i16, i32, i64, i128
/// - u8, u16, u32, u64 /// - u8, u16, u32, u64, u128
/// - f32, f64 /// - f32, f64
/// - char /// - char
/// - **string** /// - **string**
@@ -884,6 +884,17 @@ pub trait Deserializer<'de>: Sized {
where where
V: Visitor<'de>; V: Visitor<'de>;
serde_if_integer128! {
/// Hint that the `Deserialize` type is expecting an `i128` value.
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>
{
let _ = visitor;
Err(Error::custom("i128 is not supported"))
}
}
/// Hint that the `Deserialize` type is expecting a `u8` value. /// Hint that the `Deserialize` type is expecting a `u8` value.
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where where
@@ -904,6 +915,17 @@ pub trait Deserializer<'de>: Sized {
where where
V: Visitor<'de>; V: Visitor<'de>;
serde_if_integer128! {
/// Hint that the `Deserialize` type is expecting an `u128` value.
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>
{
let _ = visitor;
Err(Error::custom("u128 is not supported"))
}
}
/// Hint that the `Deserialize` type is expecting a `f32` value. /// Hint that the `Deserialize` type is expecting a `f32` value.
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where where
@@ -1250,6 +1272,19 @@ pub trait Visitor<'de>: Sized {
Err(Error::invalid_type(Unexpected::Signed(v), &self)) Err(Error::invalid_type(Unexpected::Signed(v), &self))
} }
serde_if_integer128! {
/// The input contains a `i128`.
///
/// The default implementation fails with a type error.
fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E>
where
E: Error,
{
let _ = v;
Err(Error::invalid_type(Unexpected::Other("i128"), &self))
}
}
/// The input contains a `u8`. /// The input contains a `u8`.
/// ///
/// The default implementation forwards to [`visit_u64`]. /// The default implementation forwards to [`visit_u64`].
@@ -1296,6 +1331,19 @@ pub trait Visitor<'de>: Sized {
Err(Error::invalid_type(Unexpected::Unsigned(v), &self)) Err(Error::invalid_type(Unexpected::Unsigned(v), &self))
} }
serde_if_integer128! {
/// The input contains a `u128`.
///
/// The default implementation fails with a type error.
fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
where
E: Error,
{
let _ = v;
Err(Error::invalid_type(Unexpected::Other("u128"), &self))
}
}
/// The input contains an `f32`. /// The input contains an `f32`.
/// ///
/// The default implementation forwards to [`visit_f64`]. /// The default implementation forwards to [`visit_f64`].
+17 -7
View File
@@ -46,8 +46,8 @@
/// } /// }
/// # /// #
/// # forward_to_deserialize_any! { /// # forward_to_deserialize_any! {
/// # i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes /// # i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
/// # byte_buf option unit unit_struct newtype_struct seq tuple /// # bytes byte_buf option unit unit_struct newtype_struct seq tuple
/// # tuple_struct map struct enum identifier ignored_any /// # tuple_struct map struct enum identifier ignored_any
/// # } /// # }
/// # } /// # }
@@ -80,8 +80,8 @@
/// } /// }
/// ///
/// forward_to_deserialize_any! { /// forward_to_deserialize_any! {
/// bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes /// bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
/// byte_buf option unit unit_struct newtype_struct seq tuple /// bytes byte_buf option unit unit_struct newtype_struct seq tuple
/// tuple_struct map struct enum identifier ignored_any /// tuple_struct map struct enum identifier ignored_any
/// } /// }
/// } /// }
@@ -116,9 +116,9 @@
/// # /// #
/// forward_to_deserialize_any! { /// forward_to_deserialize_any! {
/// <W: Visitor<'q>> /// <W: Visitor<'q>>
/// bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes /// bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
/// byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct /// bytes byte_buf option unit unit_struct newtype_struct seq tuple
/// map struct enum identifier ignored_any /// tuple_struct map struct enum identifier ignored_any
/// } /// }
/// # } /// # }
/// # /// #
@@ -174,6 +174,11 @@ macro_rules! forward_to_deserialize_any_helper {
(i64<$l:tt, $v:ident>) => { (i64<$l:tt, $v:ident>) => {
forward_to_deserialize_any_method!{deserialize_i64<$l, $v>()} forward_to_deserialize_any_method!{deserialize_i64<$l, $v>()}
}; };
(i128<$l:tt, $v:ident>) => {
serde_if_integer128! {
forward_to_deserialize_any_method!{deserialize_i128<$l, $v>()}
}
};
(u8<$l:tt, $v:ident>) => { (u8<$l:tt, $v:ident>) => {
forward_to_deserialize_any_method!{deserialize_u8<$l, $v>()} forward_to_deserialize_any_method!{deserialize_u8<$l, $v>()}
}; };
@@ -186,6 +191,11 @@ macro_rules! forward_to_deserialize_any_helper {
(u64<$l:tt, $v:ident>) => { (u64<$l:tt, $v:ident>) => {
forward_to_deserialize_any_method!{deserialize_u64<$l, $v>()} forward_to_deserialize_any_method!{deserialize_u64<$l, $v>()}
}; };
(u128<$l:tt, $v:ident>) => {
serde_if_integer128! {
forward_to_deserialize_any_method!{deserialize_u128<$l, $v>()}
}
};
(f32<$l:tt, $v:ident>) => { (f32<$l:tt, $v:ident>) => {
forward_to_deserialize_any_method!{deserialize_f32<$l, $v>()} forward_to_deserialize_any_method!{deserialize_f32<$l, $v>()}
}; };
+62 -6
View File
@@ -48,8 +48,8 @@
//! //!
//! - **Primitive types**: //! - **Primitive types**:
//! - bool //! - bool
//! - i8, i16, i32, i64, isize //! - i8, i16, i32, i64, i128, isize
//! - u8, u16, u32, u64, usize //! - u8, u16, u32, u64, u128, usize
//! - f32, f64 //! - f32, f64
//! - char //! - char
//! - str //! - str
@@ -245,7 +245,7 @@ pub trait Serialize {
/// A **data format** that can serialize any data structure supported by Serde. /// A **data format** that can serialize any data structure supported by Serde.
/// ///
/// The role of this trait is to define the serialization half of the Serde data /// The role of this trait is to define the serialization half of the Serde data
/// model, which is a way to categorize every Rust data structure into one of 27 /// model, which is a way to categorize every Rust data structure into one of 29
/// possible types. Each method of the `Serializer` trait corresponds to one of /// possible types. Each method of the `Serializer` trait corresponds to one of
/// the types of the data model. /// the types of the data model.
/// ///
@@ -254,10 +254,10 @@ pub trait Serialize {
/// ///
/// The types that make up the Serde data model are: /// The types that make up the Serde data model are:
/// ///
/// - **12 primitive types** /// - **14 primitive types**
/// - bool /// - bool
/// - i8, i16, i32, i64 /// - i8, i16, i32, i64, i128
/// - u8, u16, u32, u64 /// - u8, u16, u32, u64, u128
/// - f32, f64 /// - f32, f64
/// - char /// - char
/// - **string** /// - **string**
@@ -492,6 +492,34 @@ pub trait Serializer: Sized {
/// ``` /// ```
fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error>; fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error>;
serde_if_integer128! {
/// Serialize an `i128` value.
///
/// ```rust
/// # #[macro_use]
/// # extern crate serde;
/// #
/// # use serde::Serializer;
/// #
/// # __private_serialize!();
/// #
/// impl Serialize for i128 {
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
/// where
/// S: Serializer,
/// {
/// serializer.serialize_i128(*self)
/// }
/// }
/// #
/// # fn main() {}
/// ```
fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
let _ = v;
Err(Error::custom("i128 is not supported"))
}
}
/// Serialize a `u8` value. /// Serialize a `u8` value.
/// ///
/// If the format does not differentiate between `u8` and `u64`, a /// If the format does not differentiate between `u8` and `u64`, a
@@ -596,6 +624,34 @@ pub trait Serializer: Sized {
/// ``` /// ```
fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error>; fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error>;
serde_if_integer128! {
/// Serialize a `u128` value.
///
/// ```rust
/// # #[macro_use]
/// # extern crate serde;
/// #
/// # use serde::Serializer;
/// #
/// # __private_serialize!();
/// #
/// impl Serialize for u128 {
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
/// where
/// S: Serializer,
/// {
/// serializer.serialize_u128(*self)
/// }
/// }
/// #
/// # fn main() {}
/// ```
fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
let _ = v;
Err(Error::custom("u128 is not supported"))
}
}
/// Serialize an `f32` value. /// Serialize an `f32` value.
/// ///
/// If the format does not differentiate between `f32` and `f64`, a /// If the format does not differentiate between `f32` and `f64`, a