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

Shorter type names in recursive types #33

Closed
wants to merge 2 commits into from

Conversation

pkhry
Copy link
Contributor

@pkhry pkhry commented Jun 4, 2024

Description:

This is a workaround for trait bounds generated for recursive types in scale-codec.(see paritytech/parity-scale-codec#603, fixes paritytech/subxt#1603).
To generate short Paths in recursive types we will be temporarily storing parent_path inside the settings struct that's passed around when generating TokenStreams.
So instead of passing just the alloc_crate_path to the TypePath::to_syn_type we will pass all of the settings to make use of parent_path. So when we encounter a Path with a param and it will be equal to the parent_path we will only leave the last segment inside of it.

Examples:

  #[allow(unused)]
    #[derive(TypeInfo)]
    #[scale_info(skip_type_params(A))]
    struct Example<A, B> {
        id: A,
        rest: B,
        inner: Vec<Example<A, B>>,
    }
    ...
    // Registered:  
    let for_codegen = Testgen::new()
        .with::<Example<String, u32>>()
        .with::<Example<u8, u32>>()
        .with::<Example<u16, u32>>()
        .try_gen_tests_mod(Default::default(), true)
        .unwrap();
    ... 
    /// Generated 
        let expected = quote! {
        pub mod tests {
            use super::types;
            pub struct Example1<_1> {
                pub id: ::std::string::String,
                pub rest: _1,
                // Note the short path for `Example<_>`
                pub inner: ::std::vec::Vec<Example1<_1> >,
            }
            pub struct Example2<_1> {
                pub id: ::core::primitive::u8,
                pub rest: _1,
                pub inner: ::std::vec::Vec<Example2<_1> >,
            }
            pub struct Example3<_1> {
                pub id: ::core::primitive::u16,
                pub rest: _1,
                pub inner: ::std::vec::Vec<Example3<_1> >,
            }
        }
    };

@pkhry pkhry requested a review from jsdw June 4, 2024 18:22
@pkhry pkhry changed the title Shorted type names in recursive types Shorter type names in recursive types Jun 4, 2024
@pkhry pkhry requested a review from niklasad1 June 4, 2024 18:24
@jsdw
Copy link
Collaborator

jsdw commented Jun 5, 2024

Do we need this or can we add the #[codec(dumb_trait_bound)] attr by default to the codegen?

@pkhry
Copy link
Contributor Author

pkhry commented Jun 5, 2024

Do we need this or can we add the #[codec(dumb_trait_bound)] attr by default to the codegen?

We can, but i'm not sure if we will break something wrt trait bounds, because we will add this attribute to every enum/struct that there is

EDIT:
tested with subxt-codegen with just indiscriminately adding dumb_trait_bound to every struct/enum and it compiles. (tested only for polkadot-full metadata)

@jsdw
Copy link
Collaborator

jsdw commented Jun 5, 2024

tested with subxt-codegen with just indiscriminately adding dumb_trait_bound to every struct/enum and it compiles. (tested only for polkadot-full metadata)

That's probably a good sign then; I'd be happy if we just went with that approach for now :)

@pkhry
Copy link
Contributor Author

pkhry commented Jun 5, 2024

closing in favor of paritytech/subxt#1630

@pkhry pkhry closed this Jun 5, 2024
@pkhry pkhry removed request for jsdw and niklasad1 June 5, 2024 14:33
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.

[0.34.0] the trait bound Vec<XYZ> : subxt::ext::parity_scale_codec::Decode is not satisfied
2 participants