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 31c9d6f commit 18bfe8f
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/value/ser.rs
Expand Up @@ -5,6 +5,7 @@ use crate::value::{to_value, Value};
use alloc::borrow::ToOwned;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::convert::TryFrom;
use core::fmt::Display;
use core::result;
use serde::ser::{Impossible, Serialize};
Expand Down Expand Up @@ -92,9 +93,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 +131,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 18bfe8f

Please sign in to comment.