Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard u128 and i128 behind optional integer128 feature #351

Merged
merged 4 commits into from Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Expand Up @@ -38,6 +38,8 @@ jobs:
command: update
args: -p indexmap --precise 1.6.2
- run: cargo test
- run: cargo test --features integer128
- run: cargo test --features indexmap
- run: cargo test --all-features

clippy:
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Expand Up @@ -18,6 +18,10 @@ repository = "https://github.com/ron-rs/ron"
documentation = "https://docs.rs/ron/"
exclude = ["bors.toml", ".travis.yml"]

[features]
default = []
integer128 = []

[dependencies]
base64 = "0.13"
bitflags = "=1.2.1"
Expand Down
2 changes: 2 additions & 0 deletions src/de/id.rs
Expand Up @@ -71,6 +71,7 @@ impl<'a, 'b: 'a, 'c> de::Deserializer<'b> for &'c mut IdDeserializer<'a, 'b> {
unimplemented!("IdDeserializer may only be used for identifiers")
}

#[cfg(feature = "integer128")]
fn deserialize_i128<V>(self, _: V) -> Result<V::Value>
where
V: Visitor<'b>,
Expand Down Expand Up @@ -106,6 +107,7 @@ impl<'a, 'b: 'a, 'c> de::Deserializer<'b> for &'c mut IdDeserializer<'a, 'b> {
unimplemented!("IdDeserializer may only be used for identifiers")
}

#[cfg(feature = "integer128")]
fn deserialize_u128<V>(self, _: V) -> Result<V::Value>
where
V: Visitor<'b>,
Expand Down
4 changes: 4 additions & 0 deletions src/de/mod.rs
Expand Up @@ -182,7 +182,9 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
AnyNum::U32(x) => visitor.visit_u32(x),
AnyNum::I64(x) => visitor.visit_i64(x),
AnyNum::U64(x) => visitor.visit_u64(x),
#[cfg(feature = "integer128")]
AnyNum::I128(x) => visitor.visit_i128(x),
#[cfg(feature = "integer128")]
AnyNum::U128(x) => visitor.visit_u128(x),
}
}
Expand Down Expand Up @@ -228,6 +230,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
visitor.visit_i64(self.bytes.signed_integer()?)
}

#[cfg(feature = "integer128")]
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
Expand Down Expand Up @@ -263,6 +266,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
visitor.visit_u64(self.bytes.unsigned_integer()?)
}

#[cfg(feature = "integer128")]
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
Expand Down
2 changes: 2 additions & 0 deletions src/de/tag.rs
Expand Up @@ -71,6 +71,7 @@ impl<'a, 'b: 'a, 'c> de::Deserializer<'b> for &'c mut TagDeserializer<'a, 'b> {
self.d.deserialize_i64(visitor)
}

#[cfg(feature = "integer128")]
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'b>,
Expand Down Expand Up @@ -106,6 +107,7 @@ impl<'a, 'b: 'a, 'c> de::Deserializer<'b> for &'c mut TagDeserializer<'a, 'b> {
self.d.deserialize_u64(visitor)
}

#[cfg(feature = "integer128")]
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'b>,
Expand Down
2 changes: 2 additions & 0 deletions src/de/value.rs
Expand Up @@ -56,6 +56,7 @@ impl<'de> Visitor<'de> for ValueVisitor {
Ok(Value::Number(Number::new(v)))
}

#[cfg(feature = "integer128")]
fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E>
where
E: Error,
Expand All @@ -70,6 +71,7 @@ impl<'de> Visitor<'de> for ValueVisitor {
Ok(Value::Number(Number::new(v)))
}

#[cfg(feature = "integer128")]
fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
where
E: Error,
Expand Down
65 changes: 48 additions & 17 deletions src/parse.rs
Expand Up @@ -97,7 +97,9 @@ pub enum AnyNum {
U32(u32),
I64(i64),
U64(u64),
#[cfg(feature = "integer128")]
I128(i128),
#[cfg(feature = "integer128")]
U128(u128),
}

Expand All @@ -110,6 +112,15 @@ pub struct Bytes<'a> {
line: usize,
}

#[cfg(feature = "integer128")]
pub(crate) type LargeUInt = u128;
#[cfg(not(feature = "integer128"))]
pub(crate) type LargeUInt = u64;
#[cfg(feature = "integer128")]
pub(crate) type LargeSInt = i128;
#[cfg(not(feature = "integer128"))]
pub(crate) type LargeSInt = i64;

impl<'a> Bytes<'a> {
pub fn new(bytes: &'a [u8]) -> Result<Self> {
let mut b = Bytes {
Expand Down Expand Up @@ -251,22 +262,25 @@ impl<'a> Bytes<'a> {

any_float(f)
} else {
let max_u8 = u128::from(std::u8::MAX);
let max_u16 = u128::from(std::u16::MAX);
let max_u32 = u128::from(std::u32::MAX);
let max_u64 = u128::from(std::u64::MAX);

let min_i8 = i128::from(std::i8::MIN);
let max_i8 = i128::from(std::i8::MAX);
let min_i16 = i128::from(std::i16::MIN);
let max_i16 = i128::from(std::i16::MAX);
let min_i32 = i128::from(std::i32::MIN);
let max_i32 = i128::from(std::i32::MAX);
let min_i64 = i128::from(std::i64::MIN);
let max_i64 = i128::from(std::i64::MAX);
let max_u8 = LargeUInt::from(std::u8::MAX);
let max_u16 = LargeUInt::from(std::u16::MAX);
let max_u32 = LargeUInt::from(std::u32::MAX);
#[cfg_attr(not(feature = "integer128"), allow(clippy::useless_conversion))]
let max_u64 = LargeUInt::from(std::u64::MAX);

let min_i8 = LargeSInt::from(std::i8::MIN);
let max_i8 = LargeSInt::from(std::i8::MAX);
let min_i16 = LargeSInt::from(std::i16::MIN);
let max_i16 = LargeSInt::from(std::i16::MAX);
let min_i32 = LargeSInt::from(std::i32::MIN);
let max_i32 = LargeSInt::from(std::i32::MAX);
#[cfg_attr(not(feature = "integer128"), allow(clippy::useless_conversion))]
let min_i64 = LargeSInt::from(std::i64::MIN);
#[cfg_attr(not(feature = "integer128"), allow(clippy::useless_conversion))]
let max_i64 = LargeSInt::from(std::i64::MAX);

if is_signed {
match self.signed_integer::<i128>() {
match self.signed_integer::<LargeSInt>() {
Ok(x) => {
if x >= min_i8 && x <= max_i8 {
Ok(AnyNum::I8(x as i8))
Expand All @@ -277,7 +291,14 @@ impl<'a> Bytes<'a> {
} else if x >= min_i64 && x <= max_i64 {
Ok(AnyNum::I64(x as i64))
} else {
Ok(AnyNum::I128(x))
#[cfg(feature = "integer128")]
{
Ok(AnyNum::I128(x))
}
#[cfg(not(feature = "integer128"))]
{
Ok(AnyNum::I64(x))
}
}
}
Err(_) => {
Expand All @@ -287,7 +308,7 @@ impl<'a> Bytes<'a> {
}
}
} else {
match self.unsigned_integer::<u128>() {
match self.unsigned_integer::<LargeUInt>() {
Ok(x) => {
if x <= max_u8 {
Ok(AnyNum::U8(x as u8))
Expand All @@ -298,7 +319,14 @@ impl<'a> Bytes<'a> {
} else if x <= max_u64 {
Ok(AnyNum::U64(x as u64))
} else {
Ok(AnyNum::U128(x))
#[cfg(feature = "integer128")]
{
Ok(AnyNum::U128(x))
}
#[cfg(not(feature = "integer128"))]
{
Ok(AnyNum::U64(x))
}
}
}
Err(_) => {
Expand Down Expand Up @@ -913,7 +941,10 @@ macro_rules! impl_num {
};
}

#[cfg(feature = "integer128")]
impl_num!(u8 u16 u32 u64 u128 i8 i16 i32 i64 i128);
#[cfg(not(feature = "integer128"))]
impl_num!(u8 u16 u32 u64 i8 i16 i32 i64);

#[derive(Clone, Debug)]
pub enum ParsedStr<'a> {
Expand Down
41 changes: 27 additions & 14 deletions src/ser/mod.rs
Expand Up @@ -5,7 +5,7 @@ use crate::{
error::{Error, Result},
extensions::Extensions,
options::Options,
parse::{is_ident_first_char, is_ident_other_char},
parse::{is_ident_first_char, is_ident_other_char, LargeSInt, LargeUInt},
};

#[cfg(test)]
Expand Down Expand Up @@ -375,6 +375,20 @@ impl<W: io::Write> Serializer<W> {
Ok(())
}

fn serialize_sint(&mut self, value: impl Into<LargeSInt>) -> Result<()> {
// TODO optimize
write!(self.output, "{}", value.into())?;

Ok(())
}

fn serialize_uint(&mut self, value: impl Into<LargeUInt>) -> Result<()> {
// TODO optimize
write!(self.output, "{}", value.into())?;

Ok(())
}

fn write_identifier(&mut self, name: &str) -> io::Result<()> {
let mut bytes = name.as_bytes().iter().cloned();
if !bytes.next().map_or(false, is_ident_first_char) || !bytes.all(is_ident_other_char) {
Expand Down Expand Up @@ -409,46 +423,45 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
}

fn serialize_i8(self, v: i8) -> Result<()> {
self.serialize_i128(i128::from(v))
self.serialize_sint(v)
}

fn serialize_i16(self, v: i16) -> Result<()> {
self.serialize_i128(i128::from(v))
self.serialize_sint(v)
}

fn serialize_i32(self, v: i32) -> Result<()> {
self.serialize_i128(i128::from(v))
self.serialize_sint(v)
}

fn serialize_i64(self, v: i64) -> Result<()> {
self.serialize_i128(i128::from(v))
self.serialize_sint(v)
}

#[cfg(feature = "integer128")]
fn serialize_i128(self, v: i128) -> Result<()> {
// TODO optimize
write!(self.output, "{}", v)?;
Ok(())
self.serialize_sint(v)
}

fn serialize_u8(self, v: u8) -> Result<()> {
self.serialize_u128(u128::from(v))
self.serialize_uint(v)
}

fn serialize_u16(self, v: u16) -> Result<()> {
self.serialize_u128(u128::from(v))
self.serialize_uint(v)
}

fn serialize_u32(self, v: u32) -> Result<()> {
self.serialize_u128(u128::from(v))
self.serialize_uint(v)
}

fn serialize_u64(self, v: u64) -> Result<()> {
self.serialize_u128(u128::from(v))
self.serialize_uint(v)
}

#[cfg(feature = "integer128")]
fn serialize_u128(self, v: u128) -> Result<()> {
write!(self.output, "{}", v)?;
Ok(())
self.serialize_uint(v)
}

fn serialize_f32(self, v: f32) -> Result<()> {
Expand Down
4 changes: 4 additions & 0 deletions tests/min_max.rs
Expand Up @@ -32,6 +32,7 @@ fn test_i64_max() {
);
}

#[cfg(feature = "integer128")]
#[test]
fn test_i128_min() {
assert_eq!(
Expand All @@ -40,6 +41,7 @@ fn test_i128_min() {
);
}

#[cfg(feature = "integer128")]
#[test]
fn test_i128_max() {
assert_eq!(
Expand All @@ -48,6 +50,7 @@ fn test_i128_max() {
);
}

#[cfg(feature = "integer128")]
#[test]
fn test_u128_min() {
assert_eq!(
Expand All @@ -56,6 +59,7 @@ fn test_u128_min() {
);
}

#[cfg(feature = "integer128")]
#[test]
fn test_u128_max() {
assert_eq!(
Expand Down
4 changes: 4 additions & 0 deletions tests/struct_integers.rs
Expand Up @@ -6,11 +6,13 @@ struct S {
b: i16,
c: i32,
d: i64,
#[cfg(feature = "integer128")]
e: i128,
f: u8,
g: u16,
h: u32,
i: u64,
#[cfg(feature = "integer128")]
j: u128,
}

Expand All @@ -21,11 +23,13 @@ fn roundtrip() {
b: std::i16::MIN,
c: std::i32::MIN,
d: std::i64::MIN,
#[cfg(feature = "integer128")]
e: std::i128::MIN,
f: std::u8::MAX,
g: std::u16::MAX,
h: std::u32::MAX,
i: std::u64::MAX,
#[cfg(feature = "integer128")]
j: std::u128::MAX,
};
let serialized = ron::ser::to_string(&s).unwrap();
Expand Down