diff --git a/ergotree-interpreter/src/eval.rs b/ergotree-interpreter/src/eval.rs index ba646f462..ddce59719 100644 --- a/ergotree-interpreter/src/eval.rs +++ b/ergotree-interpreter/src/eval.rs @@ -342,7 +342,9 @@ fn smethod_eval_fn(method: &SMethod) -> Result { sglobal::TYPE_CODE => match method.method_id() { sglobal::GROUP_GENERATOR_METHOD_ID => self::sglobal::GROUP_GENERATOR_EVAL_FN, sglobal::XOR_METHOD_ID => self::sglobal::XOR_EVAL_FN, - sglobal::FROM_BIGENDIAN_BYTES_METHOD_ID => self::sglobal::SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN, + sglobal::FROM_BIGENDIAN_BYTES_METHOD_ID => { + self::sglobal::SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN + } method_id => { return Err(EvalError::NotFound(format!( "Eval fn: method {:?} with method id {:?} not found in SGlobal", diff --git a/ergotree-interpreter/src/eval/sglobal.rs b/ergotree-interpreter/src/eval/sglobal.rs index b7677ebf0..b9d0bd7ea 100644 --- a/ergotree-interpreter/src/eval/sglobal.rs +++ b/ergotree-interpreter/src/eval/sglobal.rs @@ -4,10 +4,10 @@ use crate::eval::EvalError; use ergotree_ir::mir::value::{CollKind, NativeColl, Value}; +use super::EvalFn; use ergo_chain_types::ec_point::generator; use ergotree_ir::bigint256::BigInt256; use ergotree_ir::types::stype::SType; -use super::EvalFn; fn helper_xor(x: &[i8], y: &[i8]) -> Arc<[i8]> { x.iter().zip(y.iter()).map(|(x1, x2)| *x1 ^ *x2).collect() @@ -70,10 +70,12 @@ pub(crate) static SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN: EvalFn = |mc, _env, _ctx let bytes = match bytes_val { Value::Coll(CollKind::NativeColl(NativeColl::CollByte(bytes))) => bytes, - _ => return Err(EvalError::UnexpectedValue(format!( - "fromBigEndianBytes: expected first argument to be byte array, got {:?}", - bytes_val - ))), + _ => { + return Err(EvalError::UnexpectedValue(format!( + "fromBigEndianBytes: expected first argument to be byte array, got {:?}", + bytes_val + ))) + } }; match *type_val { @@ -102,9 +104,9 @@ pub(crate) static SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN: EvalFn = |mc, _env, _ctx )); } let bytes_vec: Vec = bytes.iter().map(|&x| x as u8).collect(); - let int_bytes: [u8; 4] = bytes_vec.try_into().map_err(|_| EvalError::UnexpectedValue( - "Invalid byte array length for Int".to_string(), - ))?; + let int_bytes: [u8; 4] = bytes_vec.try_into().map_err(|_| { + EvalError::UnexpectedValue("Invalid byte array length for Int".to_string()) + })?; Ok(Value::Int(i32::from_be_bytes(int_bytes))) } SType::SLong => { @@ -114,9 +116,9 @@ pub(crate) static SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN: EvalFn = |mc, _env, _ctx )); } let bytes_vec: Vec = bytes.iter().map(|&x| x as u8).collect(); - let long_bytes: [u8; 8] = bytes_vec.try_into().map_err(|_| EvalError::UnexpectedValue( - "Invalid byte array length for Long".to_string(), - ))?; + let long_bytes: [u8; 8] = bytes_vec.try_into().map_err(|_| { + EvalError::UnexpectedValue("Invalid byte array length for Long".to_string()) + })?; Ok(Value::Long(i64::from_be_bytes(long_bytes))) } SType::SBigInt => { @@ -127,8 +129,8 @@ pub(crate) static SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN: EvalFn = |mc, _env, _ctx } let bytes_vec: Vec = bytes.iter().map(|&x| x as u8).collect(); let big_int = num_bigint::BigInt::from_bytes_be(num_bigint::Sign::Plus, &bytes_vec); - Ok(Value::BigInt(BigInt256::try_from(big_int).map_err(|e| - EvalError::UnexpectedValue(format!("Failed to convert to BigInt256: {:?}", e)) + Ok(Value::BigInt(BigInt256::try_from(big_int).map_err( + |e| EvalError::UnexpectedValue(format!("Failed to convert to BigInt256: {:?}", e)), )?)) } SType::SUnit => { @@ -140,7 +142,8 @@ pub(crate) static SGLOBAL_FROM_BIGENDIAN_BYTES_EVAL_FN: EvalFn = |mc, _env, _ctx Ok(Value::Unit) } _ => Err(EvalError::UnexpectedValue(format!( - "Unsupported type provided in fromBigEndianBytes: {:?}", type_val + "Unsupported type provided in fromBigEndianBytes: {:?}", + type_val ))), } }; @@ -201,11 +204,13 @@ mod tests { let bytes = vec![0_i8, 0, 0, 1]; let expr: Expr = MethodCall::new( Expr::Global, - sglobal::FROM_BIGENDIAN_BYTES_METHOD.clone().with_concrete_types(&type_args), + sglobal::FROM_BIGENDIAN_BYTES_METHOD + .clone() + .with_concrete_types(&type_args), vec![bytes.into()], ) - .unwrap() - .into(); + .unwrap() + .into(); let ctx = force_any_val::(); assert_eq!(eval_out::(&expr, &ctx), 1); } @@ -217,11 +222,13 @@ mod tests { let bytes = vec![0_i8, 1]; let expr: Expr = MethodCall::new( Expr::Global, - sglobal::FROM_BIGENDIAN_BYTES_METHOD.clone().with_concrete_types(&type_args), + sglobal::FROM_BIGENDIAN_BYTES_METHOD + .clone() + .with_concrete_types(&type_args), vec![bytes.into()], ) - .unwrap() - .into(); + .unwrap() + .into(); let ctx = force_any_val::(); assert_eq!(eval_out::(&expr, &ctx), 1); } @@ -233,11 +240,13 @@ mod tests { let bytes = vec![0_i8, 0, 0, 0, 0, 0, 0, 1]; let expr: Expr = MethodCall::new( Expr::Global, - sglobal::FROM_BIGENDIAN_BYTES_METHOD.clone().with_concrete_types(&type_args), + sglobal::FROM_BIGENDIAN_BYTES_METHOD + .clone() + .with_concrete_types(&type_args), vec![bytes.into()], ) - .unwrap() - .into(); + .unwrap() + .into(); let ctx = force_any_val::(); assert_eq!(eval_out::(&expr, &ctx), 1); } @@ -249,11 +258,13 @@ mod tests { let bytes = vec![0_i8, 0, 0, 1]; let expr: Expr = MethodCall::new( Expr::Global, - sglobal::FROM_BIGENDIAN_BYTES_METHOD.clone().with_concrete_types(&type_args), + sglobal::FROM_BIGENDIAN_BYTES_METHOD + .clone() + .with_concrete_types(&type_args), vec![bytes.into()], ) - .unwrap() - .into(); + .unwrap() + .into(); let ctx = force_any_val::(); assert_ne!(eval_out::(&expr, &ctx), 2); } @@ -265,14 +276,18 @@ mod tests { let bytes = vec![0_i8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]; let expr: Expr = MethodCall::new( Expr::Global, - sglobal::FROM_BIGENDIAN_BYTES_METHOD.clone().with_concrete_types(&type_args), + sglobal::FROM_BIGENDIAN_BYTES_METHOD + .clone() + .with_concrete_types(&type_args), vec![bytes.into()], ) - .unwrap() - .into(); + .unwrap() + .into(); let ctx = force_any_val::(); let expected_bigint = num_bigint::BigInt::from(1); - assert_eq!(eval_out::(&expr, &ctx), BigInt256::try_from(expected_bigint).unwrap()); + assert_eq!( + eval_out::(&expr, &ctx), + BigInt256::try_from(expected_bigint).unwrap() + ); } - } diff --git a/ergotree-ir/src/types/sglobal.rs b/ergotree-ir/src/types/sglobal.rs index 9dd59cc9e..dc6ffcb3f 100644 --- a/ergotree-ir/src/types/sglobal.rs +++ b/ergotree-ir/src/types/sglobal.rs @@ -6,10 +6,10 @@ use super::smethod::SMethodDesc; use super::stype::SType; use crate::types::smethod::SMethod; use crate::types::stype_companion::STypeCompanion; +use crate::types::stype_param::{STypeParam, STypeVar}; use alloc::vec; use alloc::vec::Vec; use lazy_static::lazy_static; -use crate::types::stype_param::{STypeParam, STypeVar}; /// SGlobal type code pub const TYPE_CODE: TypeCode = TypeCode::SGLOBAL; @@ -65,7 +65,7 @@ lazy_static! { } -lazy_static!{ +lazy_static! { static ref FROM_BIGENDIAN_BYTES_METHOD_DESC: SMethodDesc = SMethodDesc { method_id: FROM_BIGENDIAN_BYTES_METHOD_ID, name: "fromBigEndianBytes",