#How/Where to implement the support for Option in contracttype ?

18 messages · Page 1 of 1 (latest)

worldly silo
#

Hello

Right now I’m trying to implement a solution to this issue: https://github.com/stellar/rs-soroban-sdk/issues/877

The error it should solve is

error[E0277]: the trait bound `soroban_sdk::xdr::ScVal: From<&core::option::Option<i128>>` is not satisfied
  --> src/lib.rs:13:1
   |
13 | #[contracttype]
   | ^^^^^^^^^^^^^^^ the trait `From<&core::option::Option<i128>>` is not implemented for `soroban_sdk::xdr::ScVal`
   |
   = note: required for `&core::option::Option<i128>` to implement `Into<soroban_sdk::xdr::ScVal>`
   = note: required for `soroban_sdk::xdr::ScVal` to implement `TryFrom<&core::option::Option<i128>>`
   = note: required for `&core::option::Option<i128>` to implement `TryInto<soroban_sdk::xdr::ScVal>`
   = note: this error originates in the attribute macro `contracttype` (in Nightly builds, run with -Z macro-backtrace for more info)

From what I understand the problem needs something like this to solve it

impl<T: Into<ScVal>> Into<ScVal> for Option<T> {
   fn into(self) -> ScVal {
       // code…
   }
}

So to do it, the implementation should be in the repository that has ScVal(rs-stellar-xdr) ?

BTW the same issue is in rs-soroban-env https://github.com/stellar/rs-soroban-env/issues/444
That already has an option file with these two impl, that is getting me a bit confused

The questions would be, in what repo should be implemented the solution for this? (I think is rs-stellar-xdr) and what I think is the solution to the problem really is or am I lacking in my knowledge of Rust?

worldly silo
#

How/Where to implement the support for Option in contracttype ?

mortal raptor
#

Are you using nostdlib at the top of your contract? the core::option::Option looks suspicious. I think the soroban_sdk exports it's own Option type for use in contracts (if I remember right).

worldly silo
# mortal raptor Are you using nostdlib at the top of your contract? the `core::option::Option` l...

Yep it has it, for what I know the sdk doesn't have his own Option type, that was the first solution I was thinking for the issue but overall the contract allows to use core::option::Option in the exception of contracttype (that means any function works fine with that option) so the approach of implementing a custom Option to the sdk was dropped(At least that is what I think as someone that just wants to contribute to the sdk)

mortal raptor
#

oh. huh. question for @visual leaf , maybe.

visual leaf
#

There's an unfortunate long running issue that Option<> isn't supported in contracttype types.

#

I'm not able to answer the question of what is missing without taking a stab at implementing it. There are likely multiple points, changes to rs-stellar-xdr to ensure missing conversions are in place for ScVal, and missing conversions in rs-soroban-env/sdk.

#

We made an attempt at implementing it a while back and ran into an issue that you need a blanket implementation on Option<T>. This is why you're seeing errors saying you need implementations for Option<i128>. There needs to be implementations for any T. The problem is, Option<T> already has a blanket implementation in Rust's libcore (@mortal raptor which isn't the same thing as libstd), and Rust prevents multiple blanket implementations that can collide.

worldly silo
#

Jmm so for what I understand the issue is more tricky than I thought it was 😶
Thanks for the clarifications

visual leaf
#

Unfortunately Rust's type system while amazing in some regards, has some jagged corners.

#

But we have made changes to the way we use Rust traits, and I wonder if it would be easier now. It would be a great problem to solve if you find a way to debug the above and make it work.

visual leaf
#

Good news! Fix coming in next release, largely thanks to a refactor @lean sapphire did earlier in the year has made it possible to support. 👏🏻

granite pewter
#

I'm having issues with Option<String> on the latest release.

tiny field
#

I can't reproduce this, are you sure that's a build issue and not some rustanalyzer staleness?

visual leaf
granite pewter