Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gui1117 opened this issue Mar 19, 2024 · 2 comments
Labels
D2-substantial Can be fixed by an experienced coder with a working knowledge of the codebase. T1-FRAME This PR/Issue is related to core FRAME, the framework.

Comments

@gui1117
Copy link
Contributor

gui1117 commented Mar 19, 2024

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>> {
}
@ggwpez ggwpez added T1-FRAME This PR/Issue is related to core FRAME, the framework. D2-substantial Can be fixed by an experienced coder with a working knowledge of the codebase. labels Mar 19, 2024
@gui1117 gui1117 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 Mar 19, 2024
@MrishoLukamba
Copy link

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

@ggwpez
Copy link
Member

ggwpez commented Mar 20, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
D2-substantial Can be fixed by an experienced coder with a working knowledge of the codebase. T1-FRAME This PR/Issue is related to core FRAME, the framework.
Projects
Status: Backlog
Development

No branches or pull requests

3 participants