Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill gaps in ref conversions to Val #1479

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading