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

Add 'Static' type and improve type substitution codegen to accept it #886

Merged
merged 14 commits into from
Mar 31, 2023

Conversation

jsdw
Copy link
Collaborator

@jsdw jsdw commented Mar 27, 2023

This PR:

  • Adds a subxt::utils::Static<T> type If the type T impls Encode/Decode (ie static encoding and decoding), then the Static type itself implements EncodeAsType and DecodeAsType (which it'll do by statically encoding and decoding).
  • Update type substitution to support concrete types and nested generic substitution (essentially to allow users to wrap their substitute types in subxt::utils::Static to make it possible to substitute in types which only impl Encode and Decode (albeit wrapped in this Static wrapper).
  • Add a codegen CLI command to allow type substitution on the command line using the same logic (closes [feature request] Type substitution from CLI for codegen command #851).
  • Move errors into a separate errors.rs module to be more consistent, and extend them with TypeSubstitutionErrors.

The overall reason for this is to help provide a path forwards for users who are currently using type substitution to pull in types which don't impl EncodeAsType and DecodeAsType (eg types from sp_runtime/sp_core etc), now that those traits are required to be implemented on generated types.

So a user can now do things like:

#[subxt::subxt(
    runtime_metadata_path = "/path/to/metadata.scale",
    substitute_type(
        type = "sp_runtime::multiaddress::MultiAddress<A, B>",
        with = "::subxt::utils::Static<::sp_runtime::multiaddress::MultiAddress<A, B>>"
    )
)]
pub mod node_runtime {}

For instance, to use sp_runtime's MultiAddress instead of our own Subxt implementation.

Note that, in general, it's better to rely on types which do impl EncodeAsType and DecodeAsType where at all possible (Subxt provides a utils::MultiAddress and utils::AccountId32 for instance which do impl these traits).

@jsdw jsdw requested a review from a team March 28, 2023 10:27
@lexnv
Copy link
Collaborator

lexnv commented Mar 29, 2023

The clippy CI failed step is unrelated to this PR, I've also encountered while #887 and found the commit helped resolve it.


impl CodegenError {
/// Fetch the location for this error.
// Todo: Probably worth storing location outside of the variant,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you file an issue this?

codegen/src/error.rs Outdated Show resolved Hide resolved
codegen/src/error.rs Outdated Show resolved Hide resolved
Copy link
Member

@niklasad1 niklasad1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great

jsdw and others added 3 commits March 31, 2023 16:06
@jsdw jsdw deleted the jsdw-static-type branch March 31, 2023 15:56
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

Successfully merging this pull request may close these issues.

[feature request] Type substitution from CLI for codegen command
3 participants