Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

parameterisable modules #1688

@gui1117

Description

@gui1117

Something we can't do at the moment is to use the same module with two instance with two different configuration. If one module wants 2 balances for example it is not possible.

I propose a way to do that:

pub trait Balance {}

mod balances {
    pub struct Instance0;
    pub struct Instance1;

    pub trait Trait<Instance = Instance0> {}

    pub struct Module<T: Trait<Instance>, Instance = Instance0> {
        _p: core::marker::PhantomData<(T, Instance)>,
    }

    impl<T: Trait<Instance>, Instance> super::Balance for Module<T, Instance> {}
}

// A module using two balances
mod example1 {
    pub trait Trait {
        type balance: super::Balance;
        type balance1: super::Balance;
    }
}

// A module that is bound to the default balances
// The same way as the old way, no breakage
mod example2 {
    pub trait Trait: super::balances::Trait {
    }
}

// A module that is bound to the instance1 balances
mod example3 {
    pub trait Trait: super::balances::Trait<super::balances::Instance1> {
    }
}

struct Runtime;

// Default instance implementation is same as the old way,
// no breakage
impl balances::Trait for Runtime {}

impl balances::Trait<balances::Instance1> for Runtime {}

impl example1::Trait for Runtime {
    // same as the old way, no breakage
    type balance = balances::Module<Self>;
    type balance1 = balances::Module<Self, balances::Instance1>;
}

impl example2::Trait for Runtime {
}

impl example3::Trait for Runtime {
}

fn main() {
    let _ = Runtime;
}

To do so we need to modify decl_module! to be able to declare such a module.
If this is wanted I would like to work on it.

Metadata

Metadata

Assignees

Labels

J0-enhancementAn additional feature request.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions