stellar / rs-soroban-sdk

Rust SDK for Soroban contracts.
Apache License 2.0
121 stars 67 forks source link

SorobanArbitrary to be implemented for (T, V) tuple #1053

Closed gangov closed 11 months ago

gangov commented 1 year ago

What problem does your feature solve?

error: Broken pipe (os error 32)
Error: warning: build failed, waiting for other jobs to finish...
error: could not compile `curve` (lib test) due to 9 previous errors
Error: Aug 08 15:23:48.238 ERROR cargo_tarpaulin: Failed to compile tests!
error[E0277]: the trait bound `(u64, u128): SorobanArbitrary` is not satisfied
Error:    --> packages/curve/src/lib.rs:41:1
    |
41  | #[contracttype]
    | ^^^^^^^^^^^^^^^ the trait `SorobanArbitrary` is not implemented for `(u64, u128)` 

the tuple in question is in a soroban_sdk::Vector within the storage of our contract.

What would you like to see?

SorobanArbitrary to have tuple support

What alternatives are there?

none

leighmcculloch commented 1 year ago

Thanks for reporting the error. Could you share what the type is that the underneath the contracttype attribute? It's unclear what the type is.

gangov commented 1 year ago

hey @leighmcculloch sorry for the late reply. It looks like the following:

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct VerySpecialStruct {
    pub foo: Vec<(u64, u128)>,
}

the above doesn't work.

As a workaround, we changed that to

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct VerySpecialStruct {
    pub foo: Vec<OurType>,
} 

where OurType is

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct OurType {
    x: u64,
    y: u128,
}
brson commented 11 months ago

Ah good catch. I forgot about tuples here. I'll look at this asap.

brson commented 11 months ago

I've submitted the fix in https://github.com/stellar/rs-soroban-sdk/pull/1125, which does fix the original reported use case of structs containing vecs of tuples.

I discovered though that structs/enums still cannot contain tuple fields because of https://github.com/stellar/rs-soroban-sdk/issues/1124