Skip to content

Commit

Permalink
Support 128-bit integers in to_value
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Feb 8, 2023
1 parent a5522f1 commit 04a852b
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/value/ser.rs
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

0 comments on commit 04a852b

Please sign in to comment.