Merge branch 'master' into feat/VM-454-rkyv-json-value

This commit is contained in:
Ivan Boldyrev 2024-02-13 14:11:53 +01:00
commit 343b58f65f
7 changed files with 57 additions and 27 deletions

View File

@ -85,7 +85,8 @@ impl ExecutionCidState {
&mut self, &mut self,
canon_value: &ValueAggregate, canon_value: &ValueAggregate,
) -> Result<CID<CanonCidAggregate>, UncatchableError> { ) -> Result<CID<CanonCidAggregate>, UncatchableError> {
let value_cid = self.value_tracker.track_value(canon_value.get_result().clone())?; let value = canon_value.get_result().clone();
let value_cid = self.value_tracker.track_value(value)?;
let tetraplet = self.tetraplet_tracker.track_value(canon_value.get_tetraplet())?; let tetraplet = self.tetraplet_tracker.track_value(canon_value.get_tetraplet())?;
let canon_value_aggregate = CanonCidAggregate::new(value_cid, tetraplet, canon_value.get_provenance()); let canon_value_aggregate = CanonCidAggregate::new(value_cid, tetraplet, canon_value.get_provenance());
@ -97,8 +98,8 @@ impl ExecutionCidState {
pub(crate) fn get_value_by_cid(&self, cid: &CID<JValue>) -> Result<JValue, UncatchableError> { pub(crate) fn get_value_by_cid(&self, cid: &CID<JValue>) -> Result<JValue, UncatchableError> {
self.value_tracker self.value_tracker
.get(cid) .get(cid)
.map(|v| (*v).clone())
.ok_or_else(|| UncatchableError::ValueForCidNotFound("value", cid.get_inner())) .ok_or_else(|| UncatchableError::ValueForCidNotFound("value", cid.get_inner()))
.map(|rc_value| (*rc_value).clone())
} }
pub(crate) fn get_tetraplet_by_cid( pub(crate) fn get_tetraplet_by_cid(

View File

@ -331,9 +331,9 @@ fn fold_merge() {
}; };
let service_result_agg = data.cid_info.service_result_store.get(cid).unwrap(); let service_result_agg = data.cid_info.service_result_store.get(cid).unwrap();
let value = data.cid_info.value_store.get(&service_result_agg.value_cid).unwrap(); let value = &*data.cid_info.value_store.get(&service_result_agg.value_cid).unwrap();
if let JValue::String(ref var_name) = *value { if let JValue::String(ref var_name) = value {
let current_count: usize = calls_count.get(var_name).copied().unwrap_or_default(); let current_count: usize = calls_count.get(var_name).copied().unwrap_or_default();
calls_count.insert(var_name.to_owned(), current_count + 1); calls_count.insert(var_name.to_owned(), current_count + 1);
} }

View File

@ -20,7 +20,7 @@
*/ */
use super::{JValue, Object}; use super::{JValue, Object};
use crate::{JsonString, Map}; use crate::{JsonString, Map, Number};
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
@ -168,6 +168,22 @@ impl<'a> From<Cow<'a, str>> for JValue {
} }
} }
impl From<Number> for JValue {
/// Convert `Number` to `JValue::Number`.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::{JValue, Number};
///
/// let n = Number::from(7);
/// let x: JValue = n.into();
/// ```
fn from(f: Number) -> Self {
JValue::Number(f)
}
}
impl From<Map<JsonString, JValue>> for JValue { impl From<Map<JsonString, JValue>> for JValue {
/// Convert map (with string keys) to `JValue::Object`. /// Convert map (with string keys) to `JValue::Object`.
/// ///

View File

@ -301,7 +301,10 @@ impl JValue {
/// return the integer value. /// return the integer value.
#[inline] #[inline]
pub fn is_i64(&self) -> bool { pub fn is_i64(&self) -> bool {
matches!(self, JValue::Number(_)) match self {
JValue::Number(n) => n.is_i64(),
_ => false,
}
} }
/// Returns true if the `JValue` is an integer between zero and `u64::MAX`. /// Returns true if the `JValue` is an integer between zero and `u64::MAX`.
@ -310,7 +313,10 @@ impl JValue {
/// return the integer value. /// return the integer value.
#[inline] #[inline]
pub fn is_u64(&self) -> bool { pub fn is_u64(&self) -> bool {
false match self {
JValue::Number(n) => n.is_u64(),
_ => false,
}
} }
/// Returns true if the `JValue` is a number that can be represented by f64. /// Returns true if the `JValue` is a number that can be represented by f64.
@ -322,7 +328,10 @@ impl JValue {
/// `is_u64` return false but this is not a guarantee in the future. /// `is_u64` return false but this is not a guarantee in the future.
#[inline] #[inline]
pub fn is_f64(&self) -> bool { pub fn is_f64(&self) -> bool {
false match self {
JValue::Number(n) => n.is_f64(),
_ => false,
}
} }
/// If the `JValue` is an integer, represent it as i64 if possible. Returns /// If the `JValue` is an integer, represent it as i64 if possible. Returns
@ -339,14 +348,20 @@ impl JValue {
/// None otherwise. /// None otherwise.
#[inline] #[inline]
pub fn as_u64(&self) -> Option<u64> { pub fn as_u64(&self) -> Option<u64> {
None match self {
JValue::Number(n) => n.as_u64(),
_ => None,
}
} }
/// If the `JValue` is a number, represent it as f64 if possible. Returns /// If the `JValue` is a number, represent it as f64 if possible. Returns
/// None otherwise. /// None otherwise.
#[inline] #[inline]
pub fn as_f64(&self) -> Option<f64> { pub fn as_f64(&self) -> Option<f64> {
None match self {
JValue::Number(n) => n.as_f64(),
_ => None,
}
} }
/// Returns true if the `JValue` is a Boolean. Returns false otherwise. /// Returns true if the `JValue` is a Boolean. Returns false otherwise.

View File

@ -33,7 +33,7 @@ fn eq_u64(value: &JValue, other: u64) -> bool {
fn eq_f32(value: &JValue, other: f32) -> bool { fn eq_f32(value: &JValue, other: f32) -> bool {
match value { match value {
// NB: is not same as the original version // NB: is not same as the original version
JValue::Number(n) => n.as_f64() == Some(other as f64), JValue::Number(n) => n.as_f64().map_or(false, |i| i == other as f64),
_ => false, _ => false,
} }
} }

View File

@ -14,8 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
use air_interpreter_value::{JValue, Map}; use air_interpreter_value::{JValue, Map, Number};
// use serde_json::Number;
#[test] #[test]
fn test_deserialize_null() { fn test_deserialize_null() {
@ -52,12 +51,12 @@ fn test_deserialize_i64_2() {
assert_eq!(val, JValue::Number((-42).into())); assert_eq!(val, JValue::Number((-42).into()));
} }
// #[test] #[test]
// fn test_deserialize_f64() { fn test_deserialize_f64() {
// let inp = "-3140000000000000.0"; let inp = "-3140000000000000.0";
// let val: JValue = serde_json::from_str(inp).unwrap(); let val: JValue = serde_json::from_str(inp).unwrap();
// assert_eq!(val, JValue::Number(Number::from_f64(-3.14e15).unwrap())); assert_eq!(val, JValue::Number(Number::from_f64(-3.14e15).unwrap()));
// } }
#[test] #[test]
fn test_deserialize_string_simple() { fn test_deserialize_string_simple() {

View File

@ -14,8 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
use air_interpreter_value::JValue; use air_interpreter_value::{JValue, Number};
// use serde_json::Number;
#[test] #[test]
fn test_serialize_null() { fn test_serialize_null() {
@ -52,12 +51,12 @@ fn test_serialize_i64_2() {
assert_eq!(res, "-42"); assert_eq!(res, "-42");
} }
// #[test] #[test]
// fn test_serialize_f64() { fn test_serialize_f64() {
// let val = JValue::Number(Number::from_f64(-3.14e15).unwrap()); let val = JValue::Number(Number::from_f64(-3.14e15).unwrap());
// let res = serde_json::to_string(&val).unwrap(); let res = serde_json::to_string(&val).unwrap();
// assert_eq!(res, "-3140000000000000.0"); assert_eq!(res, "-3140000000000000.0");
// } }
#[test] #[test]
fn test_serialize_string_simple() { fn test_serialize_string_simple() {