4

I'm trying to constrain the value of an associated type of a trait inside a benchmark's where bound, like this:

#[benchmarks(where T: currency::Config<Balance = u128>)]
pub mod benchmarks {
    use super::*;

    #[benchmark]
    pub fn set_nomination_enabled() {
        #[extrinsic_call]
        _(RawOrigin::Root, true);
    }
}

However, this results in an error:

error[E0284]: type annotations needed
    |
122 | #[benchmarks(where T: currency::Config<Balance = u128>)]
    |                                        ^^^^^^^^^^^^^^ cannot infer type
    |
    = note: cannot satisfy `<T as currency::Config>::Balance == _`

The type annotations needed error does not make any sense to me, and I suspect it may be because of some cleverness by the macro.. Am I doing something wrong?

1 Answer 1

2

Instead of requiring an exact type, you can use a trait like From<u128>, or better: From<u32>.
Keep in mind that these bounds should be as general as possible.
Otherwise it artificially restricts the runtimes that your pallet can be deployed in.
For example when forcing it to u128, it wont work anymore with runtimes that use u64.

The correct bound would be:

#[benchmarks(where <T as pallet_balances::Config>::Balance: From<u32>)]

Then you can use .into() to consturct it like let balance = 10u32.into().

2
  • Thanks, I'll give that a try. But I think where bounds should be able to constrain associated type to exact types? See e.g. the function f here: play.rust-lang.org/…
    – Sander
    Apr 28, 2023 at 13:21
  • Ah you are right, for associated types it works. I assumed it is a generic, but is should be an associated type. Anyway, keeping it generic is still a good idea. I updated the answer. Apr 28, 2023 at 13:45

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.