From 37e76e0f8552260079ee4aad8fd569a48eeaf02f Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Oct 2022 14:44:02 -0700 Subject: [PATCH] Assume serde_if_integer128 is true --- src/de.rs | 145 +++++++++++++++++++++------------------------- src/number.rs | 26 +++------ src/ser.rs | 117 ++++++++++++++++--------------------- src/value/de.rs | 23 +++----- src/value/from.rs | 9 +-- src/value/ser.rs | 15 ++--- 6 files changed, 138 insertions(+), 197 deletions(-) diff --git a/src/de.rs b/src/de.rs index ffd0d48c2..378b71062 100644 --- a/src/de.rs +++ b/src/de.rs @@ -14,7 +14,7 @@ use core::marker::PhantomData; use core::result; use core::str::FromStr; use serde::de::{self, Expected, Unexpected}; -use serde::{forward_to_deserialize_any, serde_if_integer128}; +use serde::forward_to_deserialize_any; #[cfg(feature = "arbitrary_precision")] use crate::number::NumberDeserializer; @@ -335,31 +335,25 @@ impl<'de, R: Read<'de>> Deserializer { } } - serde_if_integer128! { - fn scan_integer128(&mut self, buf: &mut String) -> Result<()> { - match tri!(self.next_char_or_null()) { - b'0' => { - buf.push('0'); - // There can be only one leading '0'. - match tri!(self.peek_or_null()) { - b'0'..=b'9' => { - Err(self.peek_error(ErrorCode::InvalidNumber)) - } - _ => Ok(()), - } + fn scan_integer128(&mut self, buf: &mut String) -> Result<()> { + match tri!(self.next_char_or_null()) { + b'0' => { + buf.push('0'); + // There can be only one leading '0'. + match tri!(self.peek_or_null()) { + b'0'..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)), + _ => Ok(()), } - c @ b'1'..=b'9' => { + } + c @ b'1'..=b'9' => { + buf.push(c as char); + while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) { + self.eat_char(); buf.push(c as char); - while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) { - self.eat_char(); - buf.push(c as char); - } - Ok(()) - } - _ => { - Err(self.error(ErrorCode::InvalidNumber)) } + Ok(()) } + _ => Err(self.error(ErrorCode::InvalidNumber)), } } @@ -1437,67 +1431,65 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer { val } - serde_if_integer128! { - fn deserialize_i128(self, visitor: V) -> Result - where - V: de::Visitor<'de>, - { - let mut buf = String::new(); - - match tri!(self.parse_whitespace()) { - Some(b'-') => { - self.eat_char(); - buf.push('-'); - } - Some(_) => {} - None => { - return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); - } - }; + fn deserialize_i128(self, visitor: V) -> Result + where + V: de::Visitor<'de>, + { + let mut buf = String::new(); - tri!(self.scan_integer128(&mut buf)); + match tri!(self.parse_whitespace()) { + Some(b'-') => { + self.eat_char(); + buf.push('-'); + } + Some(_) => {} + None => { + return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); + } + }; - let value = match buf.parse() { - Ok(int) => visitor.visit_i128(int), - Err(_) => { - return Err(self.error(ErrorCode::NumberOutOfRange)); - } - }; + tri!(self.scan_integer128(&mut buf)); - match value { - Ok(value) => Ok(value), - Err(err) => Err(self.fix_position(err)), + let value = match buf.parse() { + Ok(int) => visitor.visit_i128(int), + Err(_) => { + return Err(self.error(ErrorCode::NumberOutOfRange)); } + }; + + match value { + Ok(value) => Ok(value), + Err(err) => Err(self.fix_position(err)), } + } - fn deserialize_u128(self, visitor: V) -> Result - where - V: de::Visitor<'de>, - { - match tri!(self.parse_whitespace()) { - Some(b'-') => { - return Err(self.peek_error(ErrorCode::NumberOutOfRange)); - } - Some(_) => {} - None => { - return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); - } + fn deserialize_u128(self, visitor: V) -> Result + where + V: de::Visitor<'de>, + { + match tri!(self.parse_whitespace()) { + Some(b'-') => { + return Err(self.peek_error(ErrorCode::NumberOutOfRange)); } + Some(_) => {} + None => { + return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); + } + } - let mut buf = String::new(); - tri!(self.scan_integer128(&mut buf)); - - let value = match buf.parse() { - Ok(int) => visitor.visit_u128(int), - Err(_) => { - return Err(self.error(ErrorCode::NumberOutOfRange)); - } - }; + let mut buf = String::new(); + tri!(self.scan_integer128(&mut buf)); - match value { - Ok(value) => Ok(value), - Err(err) => Err(self.fix_position(err)), + let value = match buf.parse() { + Ok(int) => visitor.visit_u128(int), + Err(_) => { + return Err(self.error(ErrorCode::NumberOutOfRange)); } + }; + + match value { + Ok(value) => Ok(value), + Err(err) => Err(self.fix_position(err)), } } @@ -2164,15 +2156,12 @@ where deserialize_integer_key!(deserialize_i16 => visit_i16); deserialize_integer_key!(deserialize_i32 => visit_i32); deserialize_integer_key!(deserialize_i64 => visit_i64); + deserialize_integer_key!(deserialize_i128 => visit_i128); deserialize_integer_key!(deserialize_u8 => visit_u8); deserialize_integer_key!(deserialize_u16 => visit_u16); deserialize_integer_key!(deserialize_u32 => visit_u32); deserialize_integer_key!(deserialize_u64 => visit_u64); - - serde_if_integer128! { - deserialize_integer_key!(deserialize_i128 => visit_i128); - deserialize_integer_key!(deserialize_u128 => visit_u128); - } + deserialize_integer_key!(deserialize_u128 => visit_u128); #[inline] fn deserialize_option(self, visitor: V) -> Result diff --git a/src/number.rs b/src/number.rs index e57b6d518..21a76411c 100644 --- a/src/number.rs +++ b/src/number.rs @@ -12,10 +12,7 @@ use core::hash::{Hash, Hasher}; use serde::de::{self, Unexpected, Visitor}; #[cfg(feature = "arbitrary_precision")] use serde::de::{IntoDeserializer, MapAccess}; -use serde::{ - forward_to_deserialize_any, serde_if_integer128, Deserialize, Deserializer, Serialize, - Serializer, -}; +use serde::{forward_to_deserialize_any, Deserialize, Deserializer, Serialize, Serializer}; #[cfg(feature = "arbitrary_precision")] pub(crate) const TOKEN: &str = "$serde_json::private::Number"; @@ -540,18 +537,15 @@ impl<'de> Deserializer<'de> for Number { deserialize_number!(deserialize_i16 => visit_i16); deserialize_number!(deserialize_i32 => visit_i32); deserialize_number!(deserialize_i64 => visit_i64); + deserialize_number!(deserialize_i128 => visit_i128); deserialize_number!(deserialize_u8 => visit_u8); deserialize_number!(deserialize_u16 => visit_u16); deserialize_number!(deserialize_u32 => visit_u32); deserialize_number!(deserialize_u64 => visit_u64); + deserialize_number!(deserialize_u128 => visit_u128); deserialize_number!(deserialize_f32 => visit_f32); deserialize_number!(deserialize_f64 => visit_f64); - serde_if_integer128! { - deserialize_number!(deserialize_i128 => visit_i128); - deserialize_number!(deserialize_u128 => visit_u128); - } - forward_to_deserialize_any! { bool char str string bytes byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct map struct enum identifier @@ -568,18 +562,15 @@ impl<'de, 'a> Deserializer<'de> for &'a Number { deserialize_number!(deserialize_i16 => visit_i16); deserialize_number!(deserialize_i32 => visit_i32); deserialize_number!(deserialize_i64 => visit_i64); + deserialize_number!(deserialize_i128 => visit_i128); deserialize_number!(deserialize_u8 => visit_u8); deserialize_number!(deserialize_u16 => visit_u16); deserialize_number!(deserialize_u32 => visit_u32); deserialize_number!(deserialize_u64 => visit_u64); + deserialize_number!(deserialize_u128 => visit_u128); deserialize_number!(deserialize_f32 => visit_f32); deserialize_number!(deserialize_f64 => visit_f64); - serde_if_integer128! { - deserialize_number!(deserialize_i128 => visit_i128); - deserialize_number!(deserialize_u128 => visit_u128); - } - forward_to_deserialize_any! { bool char str string bytes byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct map struct enum identifier @@ -731,10 +722,9 @@ impl_from_unsigned!(u8, u16, u32, u64, usize); impl_from_signed!(i8, i16, i32, i64, isize); #[cfg(feature = "arbitrary_precision")] -serde_if_integer128! { - impl_from_unsigned!(u128); - impl_from_signed!(i128); -} +impl_from_unsigned!(u128); +#[cfg(feature = "arbitrary_precision")] +impl_from_signed!(i128); impl Number { #[cfg(not(feature = "arbitrary_precision"))] diff --git a/src/ser.rs b/src/ser.rs index 17fe4daa3..b965f0ed1 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -7,7 +7,6 @@ use alloc::vec::Vec; use core::fmt::{self, Display}; use core::num::FpCategory; use serde::ser::{self, Impossible, Serialize}; -use serde::serde_if_integer128; /// A structure for serializing Rust values into JSON. #[cfg_attr(docsrs, doc(cfg(feature = "std")))] @@ -118,12 +117,10 @@ where Ok(()) } - serde_if_integer128! { - fn serialize_i128(self, value: i128) -> Result<()> { - self.formatter - .write_i128(&mut self.writer, value) - .map_err(Error::io) - } + fn serialize_i128(self, value: i128) -> Result<()> { + self.formatter + .write_i128(&mut self.writer, value) + .map_err(Error::io) } #[inline] @@ -162,12 +159,10 @@ where Ok(()) } - serde_if_integer128! { - fn serialize_u128(self, value: u128) -> Result<()> { - self.formatter - .write_u128(&mut self.writer, value) - .map_err(Error::io) - } + fn serialize_u128(self, value: u128) -> Result<()> { + self.formatter + .write_u128(&mut self.writer, value) + .map_err(Error::io) } #[inline] @@ -964,25 +959,23 @@ where Ok(()) } - serde_if_integer128! { - fn serialize_i128(self, value: i128) -> Result<()> { - tri!(self - .ser - .formatter - .begin_string(&mut self.ser.writer) - .map_err(Error::io)); - tri!(self - .ser - .formatter - .write_i128(&mut self.ser.writer, value) - .map_err(Error::io)); - tri!(self - .ser - .formatter - .end_string(&mut self.ser.writer) - .map_err(Error::io)); - Ok(()) - } + fn serialize_i128(self, value: i128) -> Result<()> { + tri!(self + .ser + .formatter + .begin_string(&mut self.ser.writer) + .map_err(Error::io)); + tri!(self + .ser + .formatter + .write_i128(&mut self.ser.writer, value) + .map_err(Error::io)); + tri!(self + .ser + .formatter + .end_string(&mut self.ser.writer) + .map_err(Error::io)); + Ok(()) } fn serialize_u8(self, value: u8) -> Result<()> { @@ -1061,25 +1054,23 @@ where Ok(()) } - serde_if_integer128! { - fn serialize_u128(self, value: u128) -> Result<()> { - tri!(self - .ser - .formatter - .begin_string(&mut self.ser.writer) - .map_err(Error::io)); - tri!(self - .ser - .formatter - .write_u128(&mut self.ser.writer, value) - .map_err(Error::io)); - tri!(self - .ser - .formatter - .end_string(&mut self.ser.writer) - .map_err(Error::io)); - Ok(()) - } + fn serialize_u128(self, value: u128) -> Result<()> { + tri!(self + .ser + .formatter + .begin_string(&mut self.ser.writer) + .map_err(Error::io)); + tri!(self + .ser + .formatter + .write_u128(&mut self.ser.writer, value) + .map_err(Error::io)); + tri!(self + .ser + .formatter + .end_string(&mut self.ser.writer) + .map_err(Error::io)); + Ok(()) } fn serialize_f32(self, _value: f32) -> Result<()> { @@ -1218,10 +1209,8 @@ impl<'a, W: io::Write, F: Formatter> ser::Serializer for NumberStrEmitter<'a, W, Err(invalid_number()) } - serde_if_integer128! { - fn serialize_i128(self, _v: i128) -> Result<()> { - Err(invalid_number()) - } + fn serialize_i128(self, _v: i128) -> Result<()> { + Err(invalid_number()) } fn serialize_u8(self, _v: u8) -> Result<()> { @@ -1240,10 +1229,8 @@ impl<'a, W: io::Write, F: Formatter> ser::Serializer for NumberStrEmitter<'a, W, Err(invalid_number()) } - serde_if_integer128! { - fn serialize_u128(self, _v: u128) -> Result<()> { - Err(invalid_number()) - } + fn serialize_u128(self, _v: u128) -> Result<()> { + Err(invalid_number()) } fn serialize_f32(self, _v: f32) -> Result<()> { @@ -1399,10 +1386,8 @@ impl<'a, W: io::Write, F: Formatter> ser::Serializer for RawValueStrEmitter<'a, Err(ser::Error::custom("expected RawValue")) } - serde_if_integer128! { - fn serialize_i128(self, _v: i128) -> Result<()> { - Err(ser::Error::custom("expected RawValue")) - } + fn serialize_i128(self, _v: i128) -> Result<()> { + Err(ser::Error::custom("expected RawValue")) } fn serialize_u8(self, _v: u8) -> Result<()> { @@ -1421,10 +1406,8 @@ impl<'a, W: io::Write, F: Formatter> ser::Serializer for RawValueStrEmitter<'a, Err(ser::Error::custom("expected RawValue")) } - serde_if_integer128! { - fn serialize_u128(self, _v: u128) -> Result<()> { - Err(ser::Error::custom("expected RawValue")) - } + fn serialize_u128(self, _v: u128) -> Result<()> { + Err(ser::Error::custom("expected RawValue")) } fn serialize_f32(self, _v: f32) -> Result<()> { diff --git a/src/value/de.rs b/src/value/de.rs index cc1d38565..09d69c980 100644 --- a/src/value/de.rs +++ b/src/value/de.rs @@ -14,7 +14,7 @@ use serde::de::{ self, Deserialize, DeserializeSeed, EnumAccess, Expected, IntoDeserializer, MapAccess, SeqAccess, Unexpected, VariantAccess, Visitor, }; -use serde::{forward_to_deserialize_any, serde_if_integer128}; +use serde::forward_to_deserialize_any; #[cfg(feature = "arbitrary_precision")] use crate::number::NumberFromString; @@ -228,18 +228,15 @@ impl<'de> serde::Deserializer<'de> for Value { deserialize_number!(deserialize_i16); deserialize_number!(deserialize_i32); deserialize_number!(deserialize_i64); + deserialize_number!(deserialize_i128); deserialize_number!(deserialize_u8); deserialize_number!(deserialize_u16); deserialize_number!(deserialize_u32); deserialize_number!(deserialize_u64); + deserialize_number!(deserialize_u128); deserialize_number!(deserialize_f32); deserialize_number!(deserialize_f64); - serde_if_integer128! { - deserialize_number!(deserialize_i128); - deserialize_number!(deserialize_u128); - } - #[inline] fn deserialize_option(self, visitor: V) -> Result where @@ -724,18 +721,15 @@ impl<'de> serde::Deserializer<'de> for &'de Value { deserialize_value_ref_number!(deserialize_i16); deserialize_value_ref_number!(deserialize_i32); deserialize_value_ref_number!(deserialize_i64); + deserialize_number!(deserialize_i128); deserialize_value_ref_number!(deserialize_u8); deserialize_value_ref_number!(deserialize_u16); deserialize_value_ref_number!(deserialize_u32); deserialize_value_ref_number!(deserialize_u64); + deserialize_number!(deserialize_u128); deserialize_value_ref_number!(deserialize_f32); deserialize_value_ref_number!(deserialize_f64); - serde_if_integer128! { - deserialize_number!(deserialize_i128); - deserialize_number!(deserialize_u128); - } - fn deserialize_option(self, visitor: V) -> Result where V: Visitor<'de>, @@ -1156,15 +1150,12 @@ impl<'de> serde::Deserializer<'de> for MapKeyDeserializer<'de> { deserialize_integer_key!(deserialize_i16 => visit_i16); deserialize_integer_key!(deserialize_i32 => visit_i32); deserialize_integer_key!(deserialize_i64 => visit_i64); + deserialize_integer_key!(deserialize_i128 => visit_i128); deserialize_integer_key!(deserialize_u8 => visit_u8); deserialize_integer_key!(deserialize_u16 => visit_u16); deserialize_integer_key!(deserialize_u32 => visit_u32); deserialize_integer_key!(deserialize_u64 => visit_u64); - - serde_if_integer128! { - deserialize_integer_key!(deserialize_i128 => visit_i128); - deserialize_integer_key!(deserialize_u128 => visit_u128); - } + deserialize_integer_key!(deserialize_u128 => visit_u128); #[inline] fn deserialize_option(self, visitor: V) -> Result diff --git a/src/value/from.rs b/src/value/from.rs index 858a6e48a..c5a6a3960 100644 --- a/src/value/from.rs +++ b/src/value/from.rs @@ -6,9 +6,6 @@ use alloc::string::{String, ToString}; use alloc::vec::Vec; use core::iter::FromIterator; -#[cfg(feature = "arbitrary_precision")] -use serde::serde_if_integer128; - macro_rules! from_integer { ($($ty:ident)*) => { $( @@ -27,10 +24,8 @@ from_integer! { } #[cfg(feature = "arbitrary_precision")] -serde_if_integer128! { - from_integer! { - i128 u128 - } +from_integer! { + i128 u128 } impl From for Value { diff --git a/src/value/ser.rs b/src/value/ser.rs index 37e495f65..892a63d5f 100644 --- a/src/value/ser.rs +++ b/src/value/ser.rs @@ -9,9 +9,6 @@ use core::fmt::Display; use core::result; use serde::ser::{Impossible, Serialize}; -#[cfg(feature = "arbitrary_precision")] -use serde::serde_if_integer128; - impl Serialize for Value { #[inline] fn serialize(&self, serializer: S) -> result::Result @@ -96,10 +93,8 @@ impl serde::Serializer for Serializer { } #[cfg(feature = "arbitrary_precision")] - serde_if_integer128! { - fn serialize_i128(self, value: i128) -> Result { - Ok(Value::Number(value.into())) - } + fn serialize_i128(self, value: i128) -> Result { + Ok(Value::Number(value.into())) } #[inline] @@ -123,10 +118,8 @@ impl serde::Serializer for Serializer { } #[cfg(feature = "arbitrary_precision")] - serde_if_integer128! { - fn serialize_u128(self, value: u128) -> Result { - Ok(Value::Number(value.into())) - } + fn serialize_u128(self, value: u128) -> Result { + Ok(Value::Number(value.into())) } #[inline]