Skip to content

FRAME: Simplify and extend pallets config definition by using the stabilized feature: associated type bounds #3743

@gui1117

Description

@gui1117

Associated type bounds is stabilized: rust-lang/rust#122055

So we can do code like this:

#![feature(associated_type_bounds)]

trait Foo: Bar<B: Default> {
}

trait Bar {
	type B;
}

fn f<T: Foo>() {
	<<T as Bar>::B as Default>::default();
}

Instead of doing this:

trait Foo: Bar<B> {
	type B: IsType<Bar::B> + Default;
}

trait Bar {
	type B;
}

fn f<T: Foo>() {
	// Do back and forth conversion between <T as Foo>::B and <T as Bar>::B
}

1: simplify Config definition

So for pallets other than frame_system we no longer need the associated types: RuntimeEvent, RuntimeOrigin, RuntimeCall, etc...

Instead we should be able to modify the pallet Config trait definition like this:

        #[pallet::config]
-       pub trait Config: frame_system::Config {
-               /// Overarching event type.
-               type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
-
+       pub trait Config: frame_system::Config<RuntimeEvent: From<Event<Self>>> {
                /// The type in which the assets for swapping are measured.
                type Balance: Balance;
 

2: extend Config definition

We should be able to add constraint on pallet dependencies way more easily without having to write where ... for each implementation block.

To illustrate we could have a pallet which depends on pallet_balances but requires at least u128 as currency type

#[pallet::config]
pub trait Config: pallet_balances::Config<Balance: From<u128>> {
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    D2-substantialCan be fixed by an experienced coder with a working knowledge of the codebase.T1-FRAMEThis PR/Issue is related to core FRAME, the framework.

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions