Sometimes traits do not use the impl keyword. Why?

In the code below it looks like a trait is being implemented for the type called Module, but the statement does not start with impl. Why is this?

trait Store for Module<T: Trait> as TemplateModule {
    /// The storage item for our proofs.
    /// It maps a proof to the user who made the claim and when they made it.
    Proofs: map hasher(blake2_128_concat) Vec<u8> => (T::AccountId, T::BlockNumber);
}

This does not look like valid Rust code. Is this inside a macro invocation by any chance?

2 Likes

Actually yes it is. I guess that's the reason, but I don't understand how that works. Can you explain?

decl_storage! {
    trait Store for Module<T: Trait> as TemplateModule {
        /// The storage item for our proofs.
        /// It maps a proof to the user who made the claim and when they made it.
        Proofs: map hasher(blake2_128_concat) Vec<u8> => (T::AccountId, T::BlockNumber);
    }
}

In general, macros can contain pretty much anything syntactically, even—well, IDK, how about Python code? It’s up to the macro definition that determines what kind of syntax is valid and how it’s translated into Rust code.

To find out more about how to use a specific macro, the best place to start looking is usually its documentation. (Unless it’s not well-documented of course...)

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.