6
#[derive(Encode,Decode,TypeInfo)
#[scale_info(skip_type_params(T))]
Pub struct Asset<T:Config> {
  Id:###
  Name:##
}

So in the above example if you don't include the second macro. The compiler will yell at you that typeinfo is not implemented in type T. But if you include the macro it compiles fine. So what does the macro do to the struct and how does it tackle the issue?

2

2 Answers 2

7

The answer to this question is similar to the answer here:

How to fix `parity_scale_codec::MaxEncodedLen` is not implemented for `T`?

Basically, Rust macros are not that intelligent. In the case of the TypeInfo derive macro, we parse the underlying object, and try to turn it into some JSON expressed type which can be put in the metadata and used by front-ends.

While this type is generic over <T: Config>, we don't actually want to (or need to) include this information in the TypeInfo generation. This is really a Rust generic bound, so it does not make sense to include it in this context.

However, the macro does not know that, so we have #[scale_info(skip_type_params(T))] to be able to tell the macro to simply skip this when generating the TypeInfo, and that makes everything work.

1
  • While this answer does make sense I have a follow up question, why not entirely skip trait bounds by default when deriving TypeInfo? Are there cases where we actually need this ? Commented Apr 26, 2023 at 4:53
0

I want to add that the purpose of DebugNoBound, CloneNoBound and similar derive macros is similar: If the deriving type is generic over T: Config, these derives can be used other than the default ones.

See the full list of such derives by searching NoBound in the substrate rust-docs.

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.