pub trait ElectionManager<Candidate>: InspectWeight<Candidate, Self::ElectionWeightOf>where
Candidate: Keyed,{
type ElectionWeight: Sortable;
type ElectionWeightOf: Buffer<Self::ElectionWeight> + Ord + Storable;
type Params: Buffer<(Candidate, Self::ElectionWeightOf)>;
type Elected: Buffer<Candidate>;
type ElectionModel: PurePluginModel<Self::Params, <Self::ElectionContext as ModelContext>::Context, Self::Elected> + Default;
type ElectionContext: ModelContext;
// Required methods
fn store(_elects: &Self::Elected) -> DispatchResult;
fn reveal() -> Option<Self::Elected>;
fn remove(who: &Candidate);
fn is_candidate(who: &Candidate) -> DispatchResult;
fn can_prepare(from: &Self::Params) -> DispatchResult;
// Provided methods
fn prepare(from: Self::Params) -> DispatchResult { ... }
fn run_model(input: Self::Params) -> Self::Elected { ... }
fn on_prepare_success(_elects: &Self::Elected) { ... }
fn on_prepare_fail(_err: DispatchError) { ... }
}Expand description
A trait for managing elections of candidates with associated weights.
This trait is designed to be highly generic and
plugin-driven, enabling multiple election models
to be used without modifying the trait itself.
Elections are inherently pluggable and diverse:
- Different elections may use distinct weighting rules or selection algorithms.
- Inputs consist of candidates paired with their backing weights.
- Outputs may represent either a single winner or multiple winners.
- A plugin-based design allows flexible implementations while preserving type safety and runtime configurability.
§Type Parameters
Candidate: The type representing a candidate in the election.
Required Associated Types§
Sourcetype ElectionWeight: Sortable
type ElectionWeight: Sortable
Represents a single vote or its associated weight.
This can either:
- Store a numeric weight for a vote, or
- Represent a single vote implicitly.
Must implement Ord to allow comparison and sorting within
Self::ElectionWeightOf.
Sourcetype ElectionWeightOf: Buffer<Self::ElectionWeight> + Ord + Storable
type ElectionWeightOf: Buffer<Self::ElectionWeight> + Ord + Storable
Sourcetype Params: Buffer<(Candidate, Self::ElectionWeightOf)>
type Params: Buffer<(Candidate, Self::ElectionWeightOf)>
Input type for election computation.
Each entry maps a candidate to a collection of associated weights.
This abstraction ensures that plugin implementations only accept well-structured and compatible input formats.
Sourcetype Elected: Buffer<Candidate>
type Elected: Buffer<Candidate>
Output type representing elected candidates.
This can represent:
- Multiple winners (e.g., committee selection), or
- A single winner (as a single-element collection)
If the output implies ranking, the elements are expected to be ordered by priority.
If truncation occurs (e.g., selecting top-N winners), the ordering must reflect the final priority of elected candidates.
Sourcetype ElectionModel: PurePluginModel<Self::Params, <Self::ElectionContext as ModelContext>::Context, Self::Elected> + Default
type ElectionModel: PurePluginModel<Self::Params, <Self::ElectionContext as ModelContext>::Context, Self::Elected> + Default
The plugin responsible for computing election results.
This model defines the election strategy by:
- Consuming
Self::Paramsas input, and - Producing
Self::Electedas output.
This abstraction allows multiple election strategies to be plugged in safely and interchangeably.
If the resulting Self::Elected is truncated (e.g., selecting top-N),
the candidates must be ordered by priority.
Sourcetype ElectionContext: ModelContext
type ElectionContext: ModelContext
Provides runtime configuration for the Self::ElectionModel computation.
This context supplies dynamic parameters such as:
- Thresholds
- Weights
- Other runtime-specific settings
It enables flexible behavior without requiring hardcoded values.
Required Methods§
Sourcefn store(_elects: &Self::Elected) -> DispatchResult
fn store(_elects: &Self::Elected) -> DispatchResult
Persist the election results. Must be implemented by the consumer.
Sourcefn remove(who: &Candidate)
fn remove(who: &Candidate)
Remove a candidate from the elected pool.
Sourcefn is_candidate(who: &Candidate) -> DispatchResult
fn is_candidate(who: &Candidate) -> DispatchResult
Check if a candidate exist in the elected pool.
Sourcefn can_prepare(from: &Self::Params) -> DispatchResult
fn can_prepare(from: &Self::Params) -> DispatchResult
Check if election preparation is possible with the given parameters.
Provided Methods§
Sourcefn prepare(from: Self::Params) -> DispatchResult
fn prepare(from: Self::Params) -> DispatchResult
Executes the election process and persists the results.
This function:
- Runs the election model using
Self::run_model - Stores the resulting elected candidates via
Self::store
Sourcefn run_model(input: Self::Params) -> Self::Elected
fn run_model(input: Self::Params) -> Self::Elected
Self::ElectionModel plugin output function.
Utilizes the plugin model’s context Self::ElectionContext
Sourcefn on_prepare_success(_elects: &Self::Elected)
fn on_prepare_success(_elects: &Self::Elected)
Hook called after a successful election preparation. Default is no-op.
Sourcefn on_prepare_fail(_err: DispatchError)
fn on_prepare_fail(_err: DispatchError)
Hook called after an election preparation failure. Default is no-op.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.