Skip to content

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

Open

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>> {
}

Activity

  1. added
    T1-FRAMEThis PR/Issue is related to core FRAME, the framework.
    D2-substantialCan be fixed by an experienced coder with a working knowledge of the codebase.
    on Mar 19, 2024
  2. changed the title [-]FRAME: Simplify and extend pallets config definition by using the stabilized associated type bounds[/-] [+]FRAME: Simplify and extend pallets config definition by using the stabilized feature: associated type bounds[/+] on Mar 19, 2024
  3. MrishoLukamba commented on Mar 20, 2024

    @MrishoLukamba
    Contributor

    This looks like a good issue to tackle and get crazy on rust types and traits.
    So essentially , we would have this syntax where we tight couple every pallet? @ggwpez @thiolliere

  4. ggwpez commented on Mar 20, 2024

    @ggwpez
    Member

    So essentially , we would have this syntax where we tight couple every pallet?

    I think a good start would be to use it for one or two pallets as a proof-of-concept. For example as gui pointed out, in the RuntimeEvent or RuntimeCall types of a pallet config.
    Best to start small with this so that we can quickly review it.

  5. dastansam commented on Jan 17, 2025

    @dastansam
    Contributor

    So essentially , we would have this syntax where we tight couple every pallet?

    I think a good start would be to use it for one or two pallets as a proof-of-concept. For example as gui pointed out, in the RuntimeEvent or RuntimeCall types of a pallet config. Best to start small with this so that we can quickly review it.

    I opened an initial PoC PR for RuntimeEvent as an associated type bound. I will be gradually tackling RuntimeCall and other where clauses if the above approach looks good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions