near / near-sdk-rs

Rust library for writing NEAR smart contracts
https://near-sdk.io
Apache License 2.0
466 stars 249 forks source link

`Self` shouldn't be concretized on traits #1200

Open mitinarseny opened 5 months ago

mitinarseny commented 5 months ago

Hey Team,

The #1001 fix makes it impossible to define a trait with #[init] method that returns Self:

#[ext_contract(ext_my_contract)]
pub trait MyContract {
    #[init]
    fn new() -> Self;
}

as it expands into

pub trait MyContract {
    fn new() -> MyContract;
}

pub mod ext_my_contract {
    // ...
}

And results into following error:

error[E0782]: trait objects must include the `dyn` keyword
 --> src/lib.rs:2:16:
  |
6 |     fn new() -> Self;
  |                 ^^^^
  |
help: add `dyn` keyword before this trait
  |
6 |     fn new() -> dyn Self;
  |                 +++
frol commented 5 months ago

@mitinarseny I think there is no meaning for #[init] in the traits. Does it work if you don't put #[init] there?

mitinarseny commented 4 months ago

Does it work if you don't put #[init] there?

Apparently, #[near] requires #[init] on all methods returning Self. Constructors usually do that

I think there is no meaning for #[init] in the traits

I respectfully disagree as it can be helpful if you want to write a Deployer contract which would be responsible for deploying other contracts, so one can use bindings for constructors of these contracts as well.

With that being said, it still wouldn't let you to write a Deployer contract that would deploy another contract and call #[init] method on it, as the current implementation of #[ext_contract] generates the code that creates Promise internally and doesn't let you to insert any other actions (such as .deploy(wasm)) prior to the function call itself.