mirror of
https://github.com/fluencelabs/aquavm
synced 2025-04-24 23:02:16 +00:00
Number type
This commit is contained in:
parent
0a51913687
commit
41c1766e7a
@ -34,6 +34,7 @@ macro_rules! tri {
|
||||
};
|
||||
}
|
||||
|
||||
mod number;
|
||||
mod value;
|
||||
|
||||
pub use value::JValue;
|
||||
|
298
crates/air-lib/interpreter-value/src/number.rs
Normal file
298
crates/air-lib/interpreter-value/src/number.rs
Normal file
@ -0,0 +1,298 @@
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
use std::{
|
||||
fmt::{self, Debug, Display},
|
||||
hash::{Hash, Hasher},
|
||||
};
|
||||
|
||||
/// Represents a JSON number, whether integer or floating point.
|
||||
#[derive(Clone, PartialEq, Eq, Hash, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||
#[archive(check_bytes)]
|
||||
pub struct Number {
|
||||
n: N,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||
#[archive(check_bytes)]
|
||||
enum N {
|
||||
PosInt(u64),
|
||||
/// Always less than zero.
|
||||
NegInt(i64),
|
||||
/// Always finite.
|
||||
// TODO bytecheck that deserialized value is finite
|
||||
Float(f64),
|
||||
}
|
||||
|
||||
impl PartialEq for N {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
(N::PosInt(a), N::PosInt(b)) => a == b,
|
||||
(N::NegInt(a), N::NegInt(b)) => a == b,
|
||||
(N::Float(a), N::Float(b)) => a == b,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Implementing Eq is fine since any float values are always finite.
|
||||
impl Eq for N {}
|
||||
|
||||
impl Hash for N {
|
||||
fn hash<H: Hasher>(&self, h: &mut H) {
|
||||
match *self {
|
||||
N::PosInt(i) => i.hash(h),
|
||||
N::NegInt(i) => i.hash(h),
|
||||
N::Float(f) => {
|
||||
if f == 0.0f64 {
|
||||
// There are 2 zero representations, +0 and -0, which
|
||||
// compare equal but have different bits. We use the +0 hash
|
||||
// for both so that hash(+0) == hash(-0).
|
||||
0.0f64.to_bits().hash(h);
|
||||
} else {
|
||||
f.to_bits().hash(h);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Number {
|
||||
/// Returns true if the `Number` is an integer between `i64::MIN` and
|
||||
/// `i64::MAX`.
|
||||
///
|
||||
/// For any Number on which `is_i64` returns true, `as_i64` is guaranteed to
|
||||
/// return the integer value.
|
||||
///
|
||||
/// ```
|
||||
/// # use serde_json::json;
|
||||
/// #
|
||||
/// let big = i64::max_value() as u64 + 10;
|
||||
/// let v = json!({ "a": 64, "b": big, "c": 256.0 });
|
||||
///
|
||||
/// assert!(v["a"].is_i64());
|
||||
///
|
||||
/// // Greater than i64::MAX.
|
||||
/// assert!(!v["b"].is_i64());
|
||||
///
|
||||
/// // Numbers with a decimal point are not considered integers.
|
||||
/// assert!(!v["c"].is_i64());
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn is_i64(&self) -> bool {
|
||||
match self.n {
|
||||
N::PosInt(v) => v <= i64::max_value() as u64,
|
||||
N::NegInt(_) => true,
|
||||
N::Float(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Number` is an integer between zero and `u64::MAX`.
|
||||
///
|
||||
/// For any Number on which `is_u64` returns true, `as_u64` is guaranteed to
|
||||
/// return the integer value.
|
||||
///
|
||||
/// ```
|
||||
/// # use serde_json::json;
|
||||
/// #
|
||||
/// let v = json!({ "a": 64, "b": -64, "c": 256.0 });
|
||||
///
|
||||
/// assert!(v["a"].is_u64());
|
||||
///
|
||||
/// // Negative integer.
|
||||
/// assert!(!v["b"].is_u64());
|
||||
///
|
||||
/// // Numbers with a decimal point are not considered integers.
|
||||
/// assert!(!v["c"].is_u64());
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn is_u64(&self) -> bool {
|
||||
match self.n {
|
||||
N::PosInt(_) => true,
|
||||
N::NegInt(_) | N::Float(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Number` can be represented by f64.
|
||||
///
|
||||
/// For any Number on which `is_f64` returns true, `as_f64` is guaranteed to
|
||||
/// return the floating point value.
|
||||
///
|
||||
/// Currently this function returns true if and only if both `is_i64` and
|
||||
/// `is_u64` return false but this is not a guarantee in the future.
|
||||
///
|
||||
/// ```
|
||||
/// # use serde_json::json;
|
||||
/// #
|
||||
/// let v = json!({ "a": 256.0, "b": 64, "c": -64 });
|
||||
///
|
||||
/// assert!(v["a"].is_f64());
|
||||
///
|
||||
/// // Integers.
|
||||
/// assert!(!v["b"].is_f64());
|
||||
/// assert!(!v["c"].is_f64());
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn is_f64(&self) -> bool {
|
||||
match self.n {
|
||||
N::Float(_) => true,
|
||||
N::PosInt(_) | N::NegInt(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// If the `Number` is an integer, represent it as i64 if possible. Returns
|
||||
/// None otherwise.
|
||||
///
|
||||
/// ```
|
||||
/// # use serde_json::json;
|
||||
/// #
|
||||
/// let big = i64::max_value() as u64 + 10;
|
||||
/// let v = json!({ "a": 64, "b": big, "c": 256.0 });
|
||||
///
|
||||
/// assert_eq!(v["a"].as_i64(), Some(64));
|
||||
/// assert_eq!(v["b"].as_i64(), None);
|
||||
/// assert_eq!(v["c"].as_i64(), None);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn as_i64(&self) -> Option<i64> {
|
||||
match self.n {
|
||||
N::PosInt(n) => {
|
||||
if n <= i64::max_value() as u64 {
|
||||
Some(n as i64)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
N::NegInt(n) => Some(n),
|
||||
N::Float(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// If the `Number` is an integer, represent it as u64 if possible. Returns
|
||||
/// None otherwise.
|
||||
///
|
||||
/// ```
|
||||
/// # use serde_json::json;
|
||||
/// #
|
||||
/// let v = json!({ "a": 64, "b": -64, "c": 256.0 });
|
||||
///
|
||||
/// assert_eq!(v["a"].as_u64(), Some(64));
|
||||
/// assert_eq!(v["b"].as_u64(), None);
|
||||
/// assert_eq!(v["c"].as_u64(), None);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn as_u64(&self) -> Option<u64> {
|
||||
match self.n {
|
||||
N::PosInt(n) => Some(n),
|
||||
N::NegInt(_) | N::Float(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents the number as f64 if possible. Returns None otherwise.
|
||||
///
|
||||
/// ```
|
||||
/// # use serde_json::json;
|
||||
/// #
|
||||
/// let v = json!({ "a": 256.0, "b": 64, "c": -64 });
|
||||
///
|
||||
/// assert_eq!(v["a"].as_f64(), Some(256.0));
|
||||
/// assert_eq!(v["b"].as_f64(), Some(64.0));
|
||||
/// assert_eq!(v["c"].as_f64(), Some(-64.0));
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn as_f64(&self) -> Option<f64> {
|
||||
match self.n {
|
||||
N::PosInt(n) => Some(n as f64),
|
||||
N::NegInt(n) => Some(n as f64),
|
||||
N::Float(n) => Some(n),
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a finite `f64` to a `Number`. Infinite or NaN values are not JSON
|
||||
/// numbers.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::f64;
|
||||
/// #
|
||||
/// # use serde_json::Number;
|
||||
/// #
|
||||
/// assert!(Number::from_f64(256.0).is_some());
|
||||
///
|
||||
/// assert!(Number::from_f64(f64::NAN).is_none());
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn from_f64(f: f64) -> Option<Number> {
|
||||
if f.is_finite() {
|
||||
let n = {
|
||||
{
|
||||
N::Float(f)
|
||||
}
|
||||
};
|
||||
Some(Number { n })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Number {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(formatter, "{}", serde_json::Number::from(self.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Number> for serde_json::Number {
|
||||
fn from(value: Number) -> Self {
|
||||
match value.n {
|
||||
N::PosInt(n) => n.into(),
|
||||
N::NegInt(n) => n.into(),
|
||||
// input is always finite, so unwrap is never triggered
|
||||
N::Float(f) => serde_json::Number::from_f64(f).unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Number> for Number {
|
||||
fn from(value: serde_json::Number) -> Self {
|
||||
if let Some(f) = value.as_f64() {
|
||||
return Number::from_f64(f).unwrap();
|
||||
}
|
||||
if let Some(n) = value.as_u64() {
|
||||
return Number { n: N::PosInt(n) };
|
||||
}
|
||||
if let Some(n) = value.as_i64() {
|
||||
return Number { n: N::NegInt(n) };
|
||||
}
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Number {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(formatter, "Number({})", self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for Number {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match self.n {
|
||||
N::PosInt(u) => serializer.serialize_u64(u),
|
||||
N::NegInt(i) => serializer.serialize_i64(i),
|
||||
N::Float(f) => serializer.serialize_f64(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Number {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: D) -> Result<Number, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let n: serde_json::Number = serde_json::Number::deserialize(deserializer)?;
|
||||
Ok(n.into())
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user