0

Answering my own question here, but I had an issue where I had a shared Rust library (entropy-shared) with types that were shared between a Rust binary using subxt and some Substrate pallets it interacts with. The library also included all the impls on the types, too, but I couldn't use them because:

  • subxt wouldn't let me use my library types directly (eg entropy_shared::Constraints); it wanted me to use the reexported types from the subxt macro, but,
  • there were no impls on the reexported types. I couldn't even transcode the two types between each other, since they're all generated from the Substrate metadata.

My errors were as follows:

error[E0308]: mismatched types
   --> crypto/server/src/helpers/substrate.rs:153:9
    |
151 |     let update_constraints_tx = entropy::tx().constraints().update_constraints(
    |                                                             ------------------ arguments to this function are incorrect
152 |         sig_req_keyring.to_account_id(),
153 |         initial_constraints,
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `entropy_shared::constraints::Constraints`, found struct `entropy_shared::Constraints`
    |
    = note: struct `entropy_shared::Constraints` and struct `entropy_shared::constraints::Constraints` have similar names, but are actually distinct types
note: struct `entropy_shared::Constraints` is defined in crate `entropy_shared`
   --> /Users/jake/codebases/entropy/entropy-core/crypto/shared/src/constraints.rs:19:1
    |
19  | pub struct Constraints {
    | ^^^^^^^^^^^^^^^^^^^^^^
note: struct `entropy_shared::constraints::Constraints` is defined in the current crate
   --> crypto/server/src/chain_api.rs:5:1
    |
5   | #[subxt::subxt(runtime_metadata_path = "entropy_metadata.scale")]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: associated function defined here
   --> crypto/server/src/chain_api.rs:5:1
    |
5   | #[subxt::subxt(runtime_metadata_path = "entropy_metadata.scale")]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: this error originates in the attribute macro `subxt::subxt` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0308`.
warning: `server` (bin "server" test) generated 1 warning
error: could not compile `server` due to previous error; 1 warning emitted

1 Answer 1

1

The solution was to use the substitute_type macro argument, as shown below:

#[subxt::subxt(runtime_metadata_path = "entropy_metadata.scale")]
pub mod entropy {
    #[subxt::subxt(substitute_type = "entropy_shared::constraints::Constraints")]
    use ::entropy_shared::Constraints;
}

I actually had to find it after digging through cargo-contracts from the dependents section of the subxt page. substitute_type is all documented in the subxt-macro docs.rs page, which is not mentioned anywhere in the docs.rs subxt page or subsections.

In hindsight, it makes sense that macro docs would be in another repo, but I think adding some reference to subxt_macro on the main subxt page might save others a lot of time.

1
  • Agreed; I do have plans to do a proper documentation pass over Subxt in general; I think a bunch of stuff here and there is lacking; the subxt macro is linked from the main crate but not documented by the looks of it! (There is an example in the examples folder for substitute types, but you'd have to know to look there, and the need for them is fairly common so should be highlighted better!)
    – jsdw
    Commented Mar 13, 2023 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.