Skip to content

Commit

Permalink
Merge pull request #982 from serde-rs/integer128tovalue
Browse files Browse the repository at this point in the history
Support 128-bit integers in to_value
  • Loading branch information
dtolnay authored Feb 8, 2023
2 parents a9c984f + e3d13cd commit 4fd4850
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/value/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::value::{to_value, Value};
use alloc::borrow::ToOwned;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
#[cfg(not(feature = "arbitrary_precision"))]
use core::convert::TryFrom;
use core::fmt::Display;
use core::result;
use serde::ser::{Impossible, Serialize};
Expand Down Expand Up @@ -92,9 +94,22 @@ impl serde::Serializer for Serializer {
Ok(Value::Number(value.into()))
}

#[cfg(feature = "arbitrary_precision")]
fn serialize_i128(self, value: i128) -> Result<Value> {
Ok(Value::Number(value.into()))
#[cfg(feature = "arbitrary_precision")]
{
Ok(Value::Number(value.into()))
}

#[cfg(not(feature = "arbitrary_precision"))]
{
if let Ok(value) = u64::try_from(value) {
Ok(Value::Number(value.into()))
} else if let Ok(value) = i64::try_from(value) {
Ok(Value::Number(value.into()))
} else {
Err(Error::syntax(ErrorCode::NumberOutOfRange, 0, 0))
}
}
}

#[inline]
Expand All @@ -117,9 +132,20 @@ impl serde::Serializer for Serializer {
Ok(Value::Number(value.into()))
}

#[cfg(feature = "arbitrary_precision")]
fn serialize_u128(self, value: u128) -> Result<Value> {
Ok(Value::Number(value.into()))
#[cfg(feature = "arbitrary_precision")]
{
Ok(Value::Number(value.into()))
}

#[cfg(not(feature = "arbitrary_precision"))]
{
if let Ok(value) = u64::try_from(value) {
Ok(Value::Number(value.into()))
} else {
Err(Error::syntax(ErrorCode::NumberOutOfRange, 0, 0))
}
}
}

#[inline]
Expand Down
21 changes: 21 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2180,6 +2180,27 @@ fn test_integer128() {
]);
}

#[test]
fn test_integer128_to_value() {
let signed = &[i128::from(i64::min_value()), i128::from(u64::max_value())];
let unsigned = &[0, u128::from(u64::max_value())];

for integer128 in signed {
let expected = integer128.to_string();
assert_eq!(to_value(integer128).unwrap().to_string(), expected);
}

for integer128 in unsigned {
let expected = integer128.to_string();
assert_eq!(to_value(integer128).unwrap().to_string(), expected);
}

if !cfg!(feature = "arbitrary_precision") {
let err = to_value(u128::from(u64::max_value()) + 1).unwrap_err();
assert_eq!(err.to_string(), "number out of range");
}
}

#[cfg(feature = "raw_value")]
#[test]
fn test_borrowed_raw_value() {
Expand Down

0 comments on commit 4fd4850

Please sign in to comment.