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

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

Open
gshep opened this issue Sep 29, 2024 · 1 comment
Open

Comments

@gshep
Copy link
Member

gshep commented Sep 29, 2024

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);
  1. add the following to idl-external-type/app/src/lib.rs:
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
Copy link
Member

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

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

No branches or pull requests

2 participants