pub struct Election<K, V> { /* private fields */ }Expand description
An election for one setting: tracks per-voter (K) votes for a value of type V and elects
the winning value (the result).
This is what Linux charger drivers call a votable (Qualcomm pmic-voter) or an election
(Google gvotable): a setting that several clients vote on, together with the rule that elects
its value.
Implementations§
Source§impl<K: Ord + Clone, V: Copy> Election<K, V>
impl<K: Ord + Clone, V: Copy> Election<K, V>
Sourcepub fn new(cmp: fn(&V, &V) -> Ordering) -> Self
pub fn new(cmp: fn(&V, &V) -> Ordering) -> Self
Creates an Election with no votes that elects the winning vote using cmp
(Ordering::Greater wins).
Sourcepub fn with_override_voter(self, override_voter: K) -> Self
pub fn with_override_voter(self, override_voter: K) -> Self
Designates override_voter as taking precedence over all other voters when present.
Sourcepub fn is_overridden(&self) -> bool
pub fn is_overridden(&self) -> bool
Returns whether override_voter currently has an active vote.
Sourcepub fn result(&self) -> Option<V>
pub fn result(&self) -> Option<V>
Returns the winning vote across all active voters, or None if no votes are active.
Sourcepub fn cast(&self, voter: K, vote: V) -> Option<V>
pub fn cast(&self, voter: K, vote: V) -> Option<V>
Casts voter’s vote, replacing any previous vote, and returns the previous vote.
Sourcepub fn withdraw<Q>(&self, voter: &Q) -> Option<V>
pub fn withdraw<Q>(&self, voter: &Q) -> Option<V>
Withdraws voter’s vote and returns it, or None if voter had no vote.
Sourcepub fn set_vote(&self, voter: K, vote: Option<V>) -> Option<V>
pub fn set_vote(&self, voter: K, vote: Option<V>) -> Option<V>
Casts (Some) or withdraws (None) voter’s vote and returns the previous vote, so that
callers can restore it if applying the new result fails.
Sourcepub async fn try_cast<E, F, Fut>(
&self,
voter: K,
vote: V,
apply: F,
) -> Result<bool, E>
pub async fn try_cast<E, F, Fut>( &self, voter: K, vote: V, apply: F, ) -> Result<bool, E>
Casts voter’s vote and, if the result changes, runs apply(result). See
Election::try_set_vote.
Sourcepub async fn try_withdraw<E, F, Fut>(
&self,
voter: K,
apply: F,
) -> Result<bool, E>
pub async fn try_withdraw<E, F, Fut>( &self, voter: K, apply: F, ) -> Result<bool, E>
Withdraws voter’s vote and, if the result changes, runs apply(result). See
Election::try_set_vote.
Sourcepub async fn try_set_vote<E, F, Fut>(
&self,
voter: K,
vote: Option<V>,
apply: F,
) -> Result<bool, E>
pub async fn try_set_vote<E, F, Fut>( &self, voter: K, vote: Option<V>, apply: F, ) -> Result<bool, E>
Casts (Some) or withdraws (None) voter’s vote and, if the result changes, runs
apply(result).
Restores voter’s previous vote if apply fails or if the returned future is dropped
before apply completes, so that the election matches what was last applied. A failed or
cancelled apply may still have partially taken effect.
Returns Ok(true) if the result changed and apply succeeded, or Ok(false) if the result
was unchanged (and apply did not run).
Trait Implementations§
Auto Trait Implementations§
impl<K, V> !Freeze for Election<K, V>
impl<K, V> !RefUnwindSafe for Election<K, V>
impl<K, V> Send for Election<K, V>
impl<K, V> Sync for Election<K, V>
impl<K, V> Unpin for Election<K, V>
impl<K, V> UnsafeUnpin for Election<K, V>where
fn(&V, &V) -> Ordering: UnsafeUnpin,
Mutex<RawSyncMutex, BTreeMap<K, V>>: UnsafeUnpin,
Option<K>: UnsafeUnpin,
impl<K, V> UnwindSafe for Election<K, V>where
fn(&V, &V) -> Ordering: UnwindSafe,
Mutex<RawSyncMutex, BTreeMap<K, V>>: UnwindSafe,
Option<K>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T, D> Encode<Ambiguous1, D> for Twhere
D: ResourceDialect,
impl<T, D> Encode<Ambiguous1, D> for Twhere
D: ResourceDialect,
§impl<T, D> Encode<Ambiguous2, D> for Twhere
D: ResourceDialect,
impl<T, D> Encode<Ambiguous2, D> for Twhere
D: ResourceDialect,
§impl<F, N> FidlIntoNative<Box<N>> for Fwhere
F: FidlIntoNative<N>,
impl<F, N> FidlIntoNative<Box<N>> for Fwhere
F: FidlIntoNative<N>,
fn fidl_into_native(self) -> Box<N>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more