diff --git a/soroban-env-common/src/convert.rs b/soroban-env-common/src/convert.rs index b49d9d15f..b9ac86283 100644 --- a/soroban-env-common/src/convert.rs +++ b/soroban-env-common/src/convert.rs @@ -1,6 +1,6 @@ use crate::{ num::{i256_from_pieces, i256_into_pieces, u256_from_pieces, u256_into_pieces}, - ConversionError, DurationSmall, DurationVal, Env, I128Small, I128Val, I256Small, I256Val, + DurationSmall, DurationVal, Env, I128Small, I128Val, I256Small, I256Val, I64Small, TimepointSmall, TimepointVal, U128Small, U128Val, U256Small, U256Val, U64Small, U64Val, Val, I256, U256, }; @@ -9,6 +9,7 @@ use stellar_xdr::int128_helpers; #[cfg(feature = "std")] use crate::{ + ConversionError, num, object::ScValObjRef, val::ValConvert, Error, Object, ScValObject, SymbolSmall, Tag, }; #[cfg(feature = "std")] @@ -59,7 +60,7 @@ where // i64 conversions impl TryFromVal for i64 { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, val: &Val) -> Result { let val = *val; @@ -67,20 +68,20 @@ impl TryFromVal for i64 { Ok(so.into()) } else { let obj = val.try_into()?; - Ok(env.obj_to_i64(obj).map_err(|_| ConversionError)?) + Ok(env.obj_to_i64(obj).map_err(Into::into)?) } } } impl TryFromVal for Val { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &i64) -> Result { let v = *v; if let Ok(so) = I64Small::try_from(v) { Ok(so.into()) } else { - Ok(env.obj_from_i64(v).map_err(|_| ConversionError)?.to_val()) + Ok(env.obj_from_i64(v).map_err(Into::into)?.to_val()) } } } @@ -88,7 +89,7 @@ impl TryFromVal for Val { // u64 conversions impl TryFromVal for u64 { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, val: &Val) -> Result { let val = *val; @@ -96,13 +97,13 @@ impl TryFromVal for u64 { Ok(so.into()) } else { let obj = val.try_into()?; - Ok(env.obj_to_u64(obj).map_err(|_| ConversionError)?) + Ok(env.obj_to_u64(obj).map_err(Into::into)?) } } } impl TryFromVal for Val { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &u64) -> Result { Ok(U64Val::try_from_val(env, v)?.to_val()) @@ -110,7 +111,7 @@ impl TryFromVal for Val { } impl TryFromVal for u64 { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, val: &U64Val) -> Result { let val = *val; @@ -118,20 +119,20 @@ impl TryFromVal for u64 { Ok(so.into()) } else { let obj = val.try_into()?; - Ok(env.obj_to_u64(obj).map_err(|_| ConversionError)?) + Ok(env.obj_to_u64(obj).map_err(Into::into)?) } } } impl TryFromVal for U64Val { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &u64) -> Result { let v = *v; if let Ok(so) = U64Small::try_from(v) { Ok(so.into()) } else { - Ok(env.obj_from_u64(v).map_err(|_| ConversionError)?.into()) + Ok(env.obj_from_u64(v).map_err(Into::into)?.into()) } } } @@ -139,7 +140,7 @@ impl TryFromVal for U64Val { // {Timepoint, Duration} <-> u64 conversions impl TryFromVal for u64 { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, val: &TimepointVal) -> Result { let val = *val; @@ -147,13 +148,13 @@ impl TryFromVal for u64 { Ok(so.into()) } else { let obj = val.try_into()?; - Ok(env.timepoint_obj_to_u64(obj).map_err(|_| ConversionError)?) + Ok(env.timepoint_obj_to_u64(obj).map_err(Into::into)?) } } } impl TryFromVal for TimepointVal { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &u64) -> Result { let v = *v; @@ -162,14 +163,14 @@ impl TryFromVal for TimepointVal { } else { Ok(env .timepoint_obj_from_u64(v) - .map_err(|_| ConversionError)? + .map_err(Into::into)? .into()) } } } impl TryFromVal for u64 { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, val: &DurationVal) -> Result { let val = *val; @@ -177,13 +178,13 @@ impl TryFromVal for u64 { Ok(so.into()) } else { let obj = val.try_into()?; - Ok(env.duration_obj_to_u64(obj).map_err(|_| ConversionError)?) + Ok(env.duration_obj_to_u64(obj).map_err(Into::into)?) } } } impl TryFromVal for DurationVal { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &u64) -> Result { let v = *v; @@ -192,7 +193,7 @@ impl TryFromVal for DurationVal { } else { Ok(env .duration_obj_from_u64(v) - .map_err(|_| ConversionError)? + .map_err(Into::into)? .into()) } } @@ -201,7 +202,7 @@ impl TryFromVal for DurationVal { // i128 conversions impl TryFromVal for i128 { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &Val) -> Result { let v = *v; @@ -209,15 +210,15 @@ impl TryFromVal for i128 { Ok(so.into()) } else { let obj = v.try_into()?; - let hi = env.obj_to_i128_hi64(obj).map_err(|_| ConversionError)?; - let lo = env.obj_to_i128_lo64(obj).map_err(|_| ConversionError)?; + let hi = env.obj_to_i128_hi64(obj).map_err(Into::into)?; + let lo = env.obj_to_i128_lo64(obj).map_err(Into::into)?; Ok(int128_helpers::i128_from_pieces(hi, lo)) } } } impl TryFromVal for Val { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &i128) -> Result { Ok(I128Val::try_from_val(env, v)?.to_val()) @@ -225,7 +226,7 @@ impl TryFromVal for Val { } impl TryFromVal for I128Val { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &i128) -> Result { let v = *v; @@ -234,7 +235,7 @@ impl TryFromVal for I128Val { } else { Ok(env .obj_from_i128_pieces(int128_helpers::i128_hi(v), int128_helpers::i128_lo(v)) - .map_err(|_| ConversionError)? + .map_err(Into::into)? .into()) } } @@ -243,7 +244,7 @@ impl TryFromVal for I128Val { // u128 conversions impl TryFromVal for u128 { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &Val) -> Result { let v = *v; @@ -251,15 +252,15 @@ impl TryFromVal for u128 { Ok(so.into()) } else { let obj = v.try_into()?; - let hi = env.obj_to_u128_hi64(obj).map_err(|_| ConversionError)?; - let lo = env.obj_to_u128_lo64(obj).map_err(|_| ConversionError)?; + let hi = env.obj_to_u128_hi64(obj).map_err(Into::into)?; + let lo = env.obj_to_u128_lo64(obj).map_err(Into::into)?; Ok(int128_helpers::u128_from_pieces(hi, lo)) } } } impl TryFromVal for Val { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &u128) -> Result { Ok(U128Val::try_from_val(env, v)?.to_val()) @@ -267,7 +268,7 @@ impl TryFromVal for Val { } impl TryFromVal for U128Val { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &u128) -> Result { let v = *v; @@ -276,7 +277,7 @@ impl TryFromVal for U128Val { } else { Ok(env .obj_from_u128_pieces(int128_helpers::u128_hi(v), int128_helpers::u128_lo(v)) - .map_err(|_| ConversionError)? + .map_err(Into::into)? .into()) } } @@ -284,7 +285,7 @@ impl TryFromVal for U128Val { // i256 conversions impl TryFromVal for I256 { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &Val) -> Result { let v = *v; @@ -292,17 +293,17 @@ impl TryFromVal for I256 { Ok(so.into()) } else { let obj = v.try_into()?; - let hi_hi = env.obj_to_i256_hi_hi(obj).map_err(|_| ConversionError)?; - let hi_lo = env.obj_to_i256_hi_lo(obj).map_err(|_| ConversionError)?; - let lo_hi = env.obj_to_i256_lo_hi(obj).map_err(|_| ConversionError)?; - let lo_lo = env.obj_to_i256_lo_lo(obj).map_err(|_| ConversionError)?; + let hi_hi = env.obj_to_i256_hi_hi(obj).map_err(Into::into)?; + let hi_lo = env.obj_to_i256_hi_lo(obj).map_err(Into::into)?; + let lo_hi = env.obj_to_i256_lo_hi(obj).map_err(Into::into)?; + let lo_lo = env.obj_to_i256_lo_lo(obj).map_err(Into::into)?; Ok(i256_from_pieces(hi_hi, hi_lo, lo_hi, lo_lo)) } } } impl TryFromVal for Val { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &I256) -> Result { Ok(I256Val::try_from_val(env, v)?.to_val()) @@ -310,7 +311,7 @@ impl TryFromVal for Val { } impl TryFromVal for I256Val { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &I256) -> Result { let v = *v; @@ -320,7 +321,7 @@ impl TryFromVal for I256Val { let (hi_hi, hi_lo, lo_hi, lo_lo) = i256_into_pieces(v); Ok(env .obj_from_i256_pieces(hi_hi, hi_lo, lo_hi, lo_lo) - .map_err(|_| ConversionError)? + .map_err(Into::into)? .into()) } } @@ -328,7 +329,7 @@ impl TryFromVal for I256Val { // u256 conversions impl TryFromVal for U256 { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &Val) -> Result { let v = *v; @@ -336,17 +337,17 @@ impl TryFromVal for U256 { Ok(so.into()) } else { let obj = v.try_into()?; - let hi_hi = env.obj_to_u256_hi_hi(obj).map_err(|_| ConversionError)?; - let hi_lo = env.obj_to_u256_hi_lo(obj).map_err(|_| ConversionError)?; - let lo_hi = env.obj_to_u256_lo_hi(obj).map_err(|_| ConversionError)?; - let lo_lo = env.obj_to_u256_lo_lo(obj).map_err(|_| ConversionError)?; + let hi_hi = env.obj_to_u256_hi_hi(obj).map_err(Into::into)?; + let hi_lo = env.obj_to_u256_hi_lo(obj).map_err(Into::into)?; + let lo_hi = env.obj_to_u256_lo_hi(obj).map_err(Into::into)?; + let lo_lo = env.obj_to_u256_lo_lo(obj).map_err(Into::into)?; Ok(u256_from_pieces(hi_hi, hi_lo, lo_hi, lo_lo)) } } } impl TryFromVal for Val { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &U256) -> Result { Ok(U256Val::try_from_val(env, v)?.to_val()) @@ -354,7 +355,7 @@ impl TryFromVal for Val { } impl TryFromVal for U256Val { - type Error = ConversionError; + type Error = crate::Error; fn try_from_val(env: &E, v: &U256) -> Result { let v = *v; @@ -364,7 +365,7 @@ impl TryFromVal for U256Val { let (hi_hi, hi_lo, lo_hi, lo_lo) = u256_into_pieces(v); Ok(env .obj_from_u256_pieces(hi_hi, hi_lo, lo_hi, lo_lo) - .map_err(|_| ConversionError)? + .map_err(Into::into)? .into()) } }