Skip to main content

Candidate

Struct Candidate 

Source
pub struct Candidate { /* private fields */ }
Expand description

One option to balance between, described by a bag of named values (its state): a hostname, whether it is online, its load.

Keys follow PHP array rules: a decimal key such as "0" is stored as the integer 0, so Candidate::states is the array PHP would hold.

PHP: Utopia\Balancer\Option.

use php_std::Value;
use utopia_balancer::Candidate;

let mut worker = Candidate::default();
worker.set("hostname", Value::from("worker-1")).set("cpu", Value::from(35));

assert_eq!(worker.get("cpu"), Some(&Value::Int(35)));
assert_eq!(worker.get_or("memory", &Value::Int(0)), &Value::Int(0));
assert_eq!(worker.states().len(), 2);
use Utopia\Balancer\Option;

$worker = new Option([]);
$worker->setState('hostname', 'worker-1')->setState('cpu', 35);

var_dump($worker->getState('cpu'));         // int(35)
var_dump($worker->getState('memory', 0));   // int(0)
var_dump(\count($worker->getStates()));     // int(2)

Implementations§

Source§

impl Candidate

Source

pub fn new(state: Array) -> Self

A candidate with this initial state.

PHP: new Option($state).

Source

pub fn get(&self, key: &str) -> Option<&Value>

The value of key, or None when it is missing or null.

PHP: getState($key), which returns null for both.

Source

pub fn get_or<'a>(&'a self, key: &str, default: &'a Value) -> &'a Value

The value of key, or default when it is missing or null.

PHP: getState($key, $default) ($state[$key] ?? $default).

Source

pub fn set(&mut self, key: &str, value: Value) -> &mut Self

Sets key to value, replacing any previous value in place.

PHP: setState($key, $value).

Source

pub fn remove(&mut self, key: &str) -> &mut Self

Removes key, if present.

PHP: deleteState($key).

Source

pub fn states(&self) -> &Array

Every value, in the order the keys were first set.

PHP: getStates().

Trait Implementations§

Source§

impl Clone for Candidate

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Candidate

Source§

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

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

impl Default for Candidate

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<Array> for Candidate

Source§

fn from(state: Array) -> Self

A candidate with this state.

Source§

impl<const N: usize> From<[(&str, Value); N]> for Candidate

Source§

fn from(pairs: [(&str, Value); N]) -> Self

A candidate from (key, value) pairs, in order.

Source§

impl PartialEq for Candidate

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Candidate

Auto Trait Implementations§

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

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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