pub struct VoterSet<Id: Eq + Ord> { /* private fields */ }
Expand description

A (non-empty) set of voters and associated weights.

A VoterSet identifies all voters that are permitted to vote in a round of the protocol and their associated weights. A VoterSet is furthermore equipped with a total order, given by the ordering of the voter’s IDs.

Implementations§

source§

impl<Id: Eq + Ord> VoterSet<Id>

source

pub fn new<I>(weights: I) -> Option<Self>where Id: Ord + Clone, I: IntoIterator<Item = (Id, u64)>,

Create a voter set from a weight distribution produced by the given iterator.

If the distribution contains multiple weights for the same voter ID, they are understood to be partial weights and are accumulated. As a result, the order in which the iterator produces the weights is irrelevant.

Returns None if the iterator does not yield a valid voter set, which is the case if it either produced no non-zero weights or, i.e. the voter set would be empty, or if the total voter weight exceeds u64::MAX.

source

pub fn get(&self, id: &Id) -> Option<&VoterInfo>

Get the voter info for the voter with the given ID, if any.

source

pub fn len(&self) -> NonZeroUsize

Get the size of the set.

source

pub fn contains(&self, id: &Id) -> bool

Whether the set contains a voter with the given ID.

source

pub fn nth_mod(&self, n: usize) -> (&Id, &VoterInfo)

Get the nth voter in the set, modulo the size of the set, as per the associated total order.

source

pub fn nth(&self, n: usize) -> Option<(&Id, &VoterInfo)>

Get the nth voter in the set, if any.

Returns None if n >= len.

source

pub fn threshold(&self) -> VoterWeight

Get the threshold vote weight required for supermajority w.r.t. this set of voters.

source

pub fn total_weight(&self) -> VoterWeight

Get the total weight of all voters.

source

pub fn iter(&self) -> impl Iterator<Item = (&Id, &VoterInfo)>

Get an iterator over the voters in the set, as given by the associated total order.

Trait Implementations§

source§

impl<Id: Clone + Eq + Ord> Clone for VoterSet<Id>

source§

fn clone(&self) -> VoterSet<Id>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<Id: Debug + Eq + Ord> Debug for VoterSet<Id>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Id: PartialEq + Eq + Ord> PartialEq<VoterSet<Id>> for VoterSet<Id>

source§

fn eq(&self, other: &VoterSet<Id>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<Id: Eq + Eq + Ord> Eq for VoterSet<Id>

source§

impl<Id: Eq + Ord> StructuralEq for VoterSet<Id>

source§

impl<Id: Eq + Ord> StructuralPartialEq for VoterSet<Id>

Auto Trait Implementations§

§

impl<Id> RefUnwindSafe for VoterSet<Id>where Id: RefUnwindSafe,

§

impl<Id> Send for VoterSet<Id>where Id: Send,

§

impl<Id> Sync for VoterSet<Id>where Id: Sync,

§

impl<Id> Unpin for VoterSet<Id>where Id: Unpin,

§

impl<Id> UnwindSafe for VoterSet<Id>where Id: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.