Skip to main content

Election

Struct Election 

Source
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>

Source

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).

Source

pub fn with_override_voter(self, override_voter: K) -> Self

Designates override_voter as taking precedence over all other voters when present.

Source

pub fn is_overridden(&self) -> bool

Returns whether override_voter currently has an active vote.

Source

pub fn result(&self) -> Option<V>

Returns the winning vote across all active voters, or None if no votes are active.

Source

pub fn vote<Q>(&self, voter: &Q) -> Option<V>
where K: Borrow<Q>, Q: Ord + ?Sized,

Returns voter’s current vote, if any.

Source

pub fn cast(&self, voter: K, vote: V) -> Option<V>

Casts voter’s vote, replacing any previous vote, and returns the previous vote.

Source

pub fn withdraw<Q>(&self, voter: &Q) -> Option<V>
where K: Borrow<Q>, Q: Ord + ?Sized,

Withdraws voter’s vote and returns it, or None if voter had no vote.

Source

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.

Source

pub async fn try_cast<E, F, Fut>( &self, voter: K, vote: V, apply: F, ) -> Result<bool, E>
where V: PartialEq, F: FnOnce(Option<V>) -> Fut, Fut: Future<Output = Result<(), E>>,

Casts voter’s vote and, if the result changes, runs apply(result). See Election::try_set_vote.

Source

pub async fn try_withdraw<E, F, Fut>( &self, voter: K, apply: F, ) -> Result<bool, E>
where V: PartialEq, F: FnOnce(Option<V>) -> Fut, Fut: Future<Output = Result<(), E>>,

Withdraws voter’s vote and, if the result changes, runs apply(result). See Election::try_set_vote.

Source

pub async fn try_set_vote<E, F, Fut>( &self, voter: K, vote: Option<V>, apply: F, ) -> Result<bool, E>
where V: PartialEq, F: FnOnce(Option<V>) -> Fut, Fut: Future<Output = Result<(), 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).

Source§

impl<K: Ord + Clone + AsRef<str>, V: Copy> Election<K, V>

Source

pub fn record_inspect_with( &self, parent: &Node, name: &str, record: impl Fn(&V, &Node, &str), )

Records result, overridden, and per-voter votes under parent/<name>, using record to write each value.

Source§

impl<K: Ord + Clone + AsRef<str>, V: Copy + InspectValue> Election<K, V>

Source

pub fn record_inspect(&self, parent: &Node, name: &str)

Records result, overridden, and per-voter votes under parent/<name>.

Source§

impl<K: Ord + Clone, V: Copy + Ord> Election<K, V>

Source

pub fn min() -> Self

Creates an Election where the lowest vote wins (V::Ord).

Source

pub fn max() -> Self

Creates an Election where the highest vote wins (V::Ord).

Trait Implementations§

Source§

impl<K: Debug, V: Debug> Debug for Election<K, V>

Source§

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

Formats the value using the given formatter. Read more

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>
where fn(&V, &V) -> Ordering: Send, Mutex<RawSyncMutex, BTreeMap<K, V>>: Send, Option<K>: Send,

§

impl<K, V> Sync for Election<K, V>
where fn(&V, &V) -> Ordering: Sync, Mutex<RawSyncMutex, BTreeMap<K, V>>: Sync, Option<K>: Sync,

§

impl<K, V> Unpin for Election<K, V>
where fn(&V, &V) -> Ordering: Unpin, Mutex<RawSyncMutex, BTreeMap<K, V>>: Unpin, Option<K>: Unpin,

§

impl<K, V> UnsafeUnpin for Election<K, V>

§

impl<K, V> UnwindSafe for Election<K, V>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T, D> Encode<Ambiguous1, D> for T
where D: ResourceDialect,

§

unsafe fn encode( self, _encoder: &mut Encoder<'_, D>, _offset: usize, _depth: Depth, ) -> Result<(), Error>

Encodes the object into the encoder’s buffers. Any handles stored in the object are swapped for Handle::INVALID. Read more
§

impl<T, D> Encode<Ambiguous2, D> for T
where D: ResourceDialect,

§

unsafe fn encode( self, _encoder: &mut Encoder<'_, D>, _offset: usize, _depth: Depth, ) -> Result<(), Error>

Encodes the object into the encoder’s buffers. Any handles stored in the object are swapped for Handle::INVALID. Read more
§

impl<F, N> FidlIntoNative<Box<N>> for F
where F: FidlIntoNative<N>,

§

fn fidl_into_native(self) -> Box<N>

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.