diff --git a/soroban-env-common/src/convert.rs b/soroban-env-common/src/convert.rs index 2b7cb2819..fe284fb42 100644 --- a/soroban-env-common/src/convert.rs +++ b/soroban-env-common/src/convert.rs @@ -86,6 +86,14 @@ impl TryFromVal for Val { } } +impl TryFromVal for Val { + type Error = crate::Error; + + fn try_from_val(env: &E, v: &&i64) -> Result { + Self::try_from_val(env, *v) + } +} + // u64 conversions impl TryFromVal for u64 { @@ -110,6 +118,14 @@ impl TryFromVal for Val { } } +impl TryFromVal for Val { + type Error = crate::Error; + + fn try_from_val(env: &E, v: &&u64) -> Result { + Self::try_from_val(env, *v) + } +} + impl TryFromVal for u64 { type Error = crate::Error; @@ -219,6 +235,14 @@ impl TryFromVal for Val { } } +impl TryFromVal for Val { + type Error = crate::Error; + + fn try_from_val(env: &E, v: &&i128) -> Result { + Self::try_from_val(env, *v) + } +} + impl TryFromVal for I128Val { type Error = crate::Error; @@ -261,6 +285,14 @@ impl TryFromVal for Val { } } +impl TryFromVal for Val { + type Error = crate::Error; + + fn try_from_val(env: &E, v: &&u128) -> Result { + Self::try_from_val(env, *v) + } +} + impl TryFromVal for U128Val { type Error = crate::Error; @@ -304,6 +336,14 @@ impl TryFromVal for Val { } } +impl TryFromVal for Val { + type Error = crate::Error; + + fn try_from_val(env: &E, v: &&I256) -> Result { + Self::try_from_val(env, *v) + } +} + impl TryFromVal for I256Val { type Error = crate::Error; @@ -348,6 +388,14 @@ impl TryFromVal for Val { } } +impl TryFromVal for Val { + type Error = crate::Error; + + fn try_from_val(env: &E, v: &&U256) -> Result { + Self::try_from_val(env, *v) + } +} + impl TryFromVal for U256Val { type Error = crate::Error; @@ -550,3 +598,15 @@ where }) } } + +#[cfg(feature = "std")] +impl TryFromVal for Val +where + Object: for<'a> TryFromVal>, + for<'a> >>::Error: Into, +{ + type Error = crate::Error; + fn try_from_val(env: &E, val: &&ScVal) -> Result { + Self::try_from_val(env, *val) + } +} diff --git a/soroban-env-common/src/option.rs b/soroban-env-common/src/option.rs index b221c8595..7e03f5fe1 100644 --- a/soroban-env-common/src/option.rs +++ b/soroban-env-common/src/option.rs @@ -28,3 +28,14 @@ where } } } + +impl TryFromVal> for Val +where + T: TryIntoVal, +{ + type Error = T::Error; + + fn try_from_val(env: &E, v: &&Option) -> Result { + Self::try_from_val(env, *v) + } +} diff --git a/soroban-env-common/src/tuple.rs b/soroban-env-common/src/tuple.rs index 79f5a674a..91328a231 100644 --- a/soroban-env-common/src/tuple.rs +++ b/soroban-env-common/src/tuple.rs @@ -33,6 +33,16 @@ macro_rules! impl_for_tuple { } } + impl TryFromVal for Val + where + $($typ: TryIntoVal),* + { + type Error = crate::Error; + fn try_from_val(env: &E, v: &&($($typ,)*)) -> Result { + Self::try_from_val(env, *v) + } + } + // Conversions to and from Array of Val. diff --git a/soroban-env-common/src/val.rs b/soroban-env-common/src/val.rs index 689e8954e..4224b4c0b 100644 --- a/soroban-env-common/src/val.rs +++ b/soroban-env-common/src/val.rs @@ -275,6 +275,13 @@ impl TryFromVal for Val { } } +impl TryFromVal for Val { + type Error = ConversionError; + fn try_from_val(_env: &E, val: &&Val) -> Result { + Ok(**val) + } +} + // Declare a few extra small-value wrapper types that don't live anywhere else. declare_tag_based_wrapper!(Void); diff --git a/soroban-env-common/src/wrapper_macros.rs b/soroban-env-common/src/wrapper_macros.rs index 704d5fcfb..e76f2f547 100644 --- a/soroban-env-common/src/wrapper_macros.rs +++ b/soroban-env-common/src/wrapper_macros.rs @@ -74,6 +74,12 @@ macro_rules! impl_tryfroms_and_tryfromvals_delegating_to_valconvert { Ok((*val).into()) } } + impl $crate::TryFromVal for $crate::Val { + type Error = $crate::ConversionError; + fn try_from_val(_env: &E, val: &&$T) -> Result { + Ok((**val).into()) + } + } }; }