gear-tech / sails

Framework for ultimate experience of writing programs powered by Gear protocol
Apache License 2.0
17 stars 4 forks source link

Generated Rust code from idl with set external type requires PartialEq #556

Open gshep opened 1 week ago

gshep commented 1 week ago

Problem

There is a new feature not to generate new entities from IDL file - #538 .

However new types with external entities used in a service will have derive(PartialEq) attribute whereas the external entities don't implement the trait.

Steps

  1. cargo sails new-program idl-external-type
  2. create sample shared crated: cd idl-external-type/ & cargo new --lib common
  3. common/lib.rs:
    
    #![no_std]

use sails_rs::{TypeInfo, Decode, Encode};

[derive(Clone, Debug, Decode, Encode, TypeInfo)]

[codec(crate = sails_rs::scale_codec)]

[scale_info(crate = sails_rs::scale_info)]

pub struct Inner(pub u32);

[derive(Clone, Debug, Decode, Encode, TypeInfo)]

[codec(crate = sails_rs::scale_codec)]

[scale_info(crate = sails_rs::scale_info)]

pub struct Data(pub Inner);

4. add the following to `idl-external-type/app/src/lib.rs`:
```rust
use common::Data;

#[derive(Clone, Debug, Decode, TypeInfo)]
#[codec(crate = sails_rs::scale_codec)]
#[scale_info(crate = sails_rs::scale_info)]
pub struct Message(pub Data);

// ...

    pub fn set_message(&mut self, message: Message) {
    }
  1. add .with_external_type("Data", "common::Data") to client/build.rs
  2. build the workspace

It produces the following code (in idl_external_type_client.rs):

#[derive(PartialEq, Debug, Encode, Decode, TypeInfo)]
#[codec(crate = sails_rs::scale_codec)]
#[scale_info(crate = sails_rs::scale_info)]
pub struct Message(pub Data);

Check to attached log for the compiler error output.

Possible Solution

No response

Notes

It is not an issue to derive PartialEq in that very case. However it not quite correct overall.

Relevant Log Output

Click to expand/collapse

``` error[E0369]: binary operation `==` cannot be applied to type `Data` --> /Users/user/devel/gear-tech/sails/idl-external-type/target/debug/build/idl-external-type-client-200f8f28330f558c/out/idl_external_type_client.rs:97:20 | 94 | #[derive(PartialEq, Debug, Encode, Decode, TypeInfo)] | --------- in this derive macro expansion ... 97 | pub struct Message(pub Data); | ^^^^^^^^ | note: the foreign item type `Data` doesn't implement `sails_rs::PartialEq` --> /Users/user/devel/gear-tech/sails/idl-external-type/common/src/lib.rs:13:1 | 13 | pub struct Data(pub Inner); | ^^^^^^^^^^^^^^^ not implement `sails_rs::PartialEq` = note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info) ```

DennisInSky commented 6 days ago

We need to take a decision whether it is better to generate Rust code according to rules of thumb, i.e. generate implementations for common traits like PartialEq, Clone, Debug or minimize a number of requirements on code used for client generation assuming the traits like PartialEq, Clone will not be used frequently, and if they are needed, one can implement them manually in the crate which includes code generate for client. Another assumption in taking decision might be it shouldn't be a big issue for user to derive impls for the traits in question provided the types used for contracts are internal to this contract