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

Add infallible From conversions for types to ScVal #1338

Merged
merged 5 commits into from
Sep 26, 2024

Conversation

leighmcculloch
Copy link
Member

What

Add infallible From conversions for types to ScVal, replacing the existing fallible TryFrom conversions.

Why

When I look through tests I see lots of try_into().unwrap() when converting types to their ScVal values. This is unnecessary, we know that the conversions will succeed because the host won't give the SDK types that aren't valid and couldn't be converted. It's just a result of the way the Rust types are setup we can't guarantee the type is always convertible, but in practice they are.

Note that this is not really a breaking change, because in the Rust core lib there is a blanket impl for all From conversions to also provide a TryFrom conversion. The only behaviour change is that if there were cases that would fail conversion previously a panic will occur instead of an error when using a TryFrom to do the conversion. In all cases there is no expected failure because the Env guarantees valid host types to be created and we're converting host types to ScVal types.

@leighmcculloch leighmcculloch marked this pull request as ready for review September 20, 2024 00:45
soroban-sdk/src/address.rs Outdated Show resolved Hide resolved
@leighmcculloch leighmcculloch added this pull request to the merge queue Sep 26, 2024
Merged via the queue into main with commit 8fc9f53 Sep 26, 2024
16 checks passed
@leighmcculloch leighmcculloch deleted the better-conversions-experience-in-tests branch September 26, 2024 21:58
leighmcculloch added a commit that referenced this pull request Oct 21, 2024
### What
Add infallible From conversions for types to ScVal, replacing the
existing fallible TryFrom conversions.

### Why
When I look through tests I see lots of `try_into().unwrap()` when
converting types to their ScVal values. This is unnecessary, we know
that the conversions will succeed because the host won't give the SDK
types that aren't valid and couldn't be converted. It's just a result of
the way the Rust types are setup we can't guarantee the type is always
convertible, but in practice they are.

Note that this is _not_ really a breaking change, because in the Rust
core lib there is a blanket impl for all From conversions to also
provide a TryFrom conversion. The only behaviour change is that if there
were cases that would fail conversion previously a panic will occur
instead of an error when using a TryFrom to do the conversion. In all
cases there is no expected failure because the Env guarantees valid host
types to be created and we're converting host types to ScVal types.

(cherry picked from commit 8fc9f53)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants