Skip to content

Commit

Permalink
Fill gaps in ref conversions to Val
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Oct 24, 2024
1 parent 8911531 commit f91034b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
60 changes: 60 additions & 0 deletions soroban-env-common/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ impl<E: Env> TryFromVal<E, i64> for Val {
}
}

impl<E: Env> TryFromVal<E, &i64> for Val {
type Error = crate::Error;

fn try_from_val(env: &E, v: &&i64) -> Result<Self, Self::Error> {
Self::try_from_val(env, *v)
}
}

// u64 conversions

impl<E: Env> TryFromVal<E, Val> for u64 {
Expand All @@ -110,6 +118,14 @@ impl<E: Env> TryFromVal<E, u64> for Val {
}
}

impl<E: Env> TryFromVal<E, &u64> for Val {
type Error = crate::Error;

fn try_from_val(env: &E, v: &&u64) -> Result<Self, Self::Error> {
Self::try_from_val(env, *v)
}
}

impl<E: Env> TryFromVal<E, U64Val> for u64 {
type Error = crate::Error;

Expand Down Expand Up @@ -219,6 +235,14 @@ impl<E: Env> TryFromVal<E, i128> for Val {
}
}

impl<E: Env> TryFromVal<E, &i128> for Val {
type Error = crate::Error;

fn try_from_val(env: &E, v: &&i128) -> Result<Self, Self::Error> {
Self::try_from_val(env, *v)
}
}

impl<E: Env> TryFromVal<E, i128> for I128Val {
type Error = crate::Error;

Expand Down Expand Up @@ -261,6 +285,14 @@ impl<E: Env> TryFromVal<E, u128> for Val {
}
}

impl<E: Env> TryFromVal<E, &u128> for Val {
type Error = crate::Error;

fn try_from_val(env: &E, v: &&u128) -> Result<Self, Self::Error> {
Self::try_from_val(env, *v)
}
}

impl<E: Env> TryFromVal<E, u128> for U128Val {
type Error = crate::Error;

Expand Down Expand Up @@ -304,6 +336,14 @@ impl<E: Env> TryFromVal<E, I256> for Val {
}
}

impl<E: Env> TryFromVal<E, &I256> for Val {
type Error = crate::Error;

fn try_from_val(env: &E, v: &&I256) -> Result<Self, Self::Error> {
Self::try_from_val(env, *v)
}
}

impl<E: Env> TryFromVal<E, I256> for I256Val {
type Error = crate::Error;

Expand Down Expand Up @@ -348,6 +388,14 @@ impl<E: Env> TryFromVal<E, U256> for Val {
}
}

impl<E: Env> TryFromVal<E, &U256> for Val {
type Error = crate::Error;

fn try_from_val(env: &E, v: &&U256) -> Result<Self, Self::Error> {
Self::try_from_val(env, *v)
}
}

impl<E: Env> TryFromVal<E, U256> for U256Val {
type Error = crate::Error;

Expand Down Expand Up @@ -550,3 +598,15 @@ where
})
}
}

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, &ScVal> for Val
where
Object: for<'a> TryFromVal<E, ScValObjRef<'a>>,
for<'a> <Object as TryFromVal<E, ScValObjRef<'a>>>::Error: Into<crate::Error>,
{
type Error = crate::Error;
fn try_from_val(env: &E, val: &&ScVal) -> Result<Val, Self::Error> {
Self::try_from_val(env, *val)
}
}
11 changes: 11 additions & 0 deletions soroban-env-common/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ where
}
}
}

impl<E: Env, T> TryFromVal<E, &Option<T>> for Val
where
T: TryIntoVal<E, Val>,
{
type Error = T::Error;

fn try_from_val(env: &E, v: &&Option<T>) -> Result<Self, Self::Error> {
Self::try_from_val(env, *v)
}
}
10 changes: 10 additions & 0 deletions soroban-env-common/src/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ macro_rules! impl_for_tuple {
}
}

impl<E: Env, $($typ),*> TryFromVal<E, &($($typ,)*)> for Val
where
$($typ: TryIntoVal<E, Val>),*
{
type Error = crate::Error;
fn try_from_val(env: &E, v: &&($($typ,)*)) -> Result<Self, Self::Error> {
Self::try_from_val(env, *v)
}
}


// Conversions to and from Array of Val.

Expand Down
7 changes: 7 additions & 0 deletions soroban-env-common/src/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ impl<E: Env> TryFromVal<E, Val> for Val {
}
}

impl<E: Env> TryFromVal<E, &Val> for Val {
type Error = ConversionError;
fn try_from_val(_env: &E, val: &&Val) -> Result<Self, Self::Error> {
Ok(**val)
}
}

// Declare a few extra small-value wrapper types that don't live anywhere else.
declare_tag_based_wrapper!(Void);

Expand Down
6 changes: 6 additions & 0 deletions soroban-env-common/src/wrapper_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ macro_rules! impl_tryfroms_and_tryfromvals_delegating_to_valconvert {
Ok((*val).into())
}
}
impl<E: $crate::Env> $crate::TryFromVal<E, &$T> for $crate::Val {
type Error = $crate::ConversionError;
fn try_from_val(_env: &E, val: &&$T) -> Result<Self, Self::Error> {
Ok((**val).into())
}
}
};
}

Expand Down

0 comments on commit f91034b

Please sign in to comment.