Skip to main content

WeightElement

Trait WeightElement 

Source
pub trait WeightElement:
    Clone
    + Default
    + 'static {
    type Sum: NumericSize;

    const IS_UNIT: bool;

    // Required methods
    fn unit() -> Self;
    fn validate_element(&self, context: &str) -> Result<(), ConstructionError>;
    fn to_sum(&self) -> Self::Sum;

    // Provided methods
    fn checked_add_to_sum(
        total: Self::Sum,
        value: Self::Sum,
        context: &str,
    ) -> Result<Self::Sum, EvaluationError> { ... }
    fn checked_mul_sum(
        left: Self::Sum,
        right: Self::Sum,
        context: &str,
    ) -> Result<Self::Sum, EvaluationError> { ... }
}
Expand description

Maps a weight element to its sum/metric type.

This decouples the per-element weight type from the accumulation type. Exact integer weights use a wider accumulation type: i64 and the unit weight One both use i64. Approximate f64 weights continue to sum into f64.

Required Associated Constants§

Source

const IS_UNIT: bool

Whether this is the unit weight type (One).

Required Associated Types§

Source

type Sum: NumericSize

The numeric type used for sums and comparisons.

Required Methods§

Source

fn unit() -> Self

Construct the multiplicative unit weight.

Source

fn validate_element(&self, context: &str) -> Result<(), ConstructionError>

Validate that an element belongs to the public weight domain.

Source

fn to_sum(&self) -> Self::Sum

Convert this weight element to the sum type.

Provided Methods§

Source

fn checked_add_to_sum( total: Self::Sum, value: Self::Sum, context: &str, ) -> Result<Self::Sum, EvaluationError>

Add one element to an evaluated objective without overflowing or producing a non-finite value.

Source

fn checked_mul_sum( left: Self::Sum, right: Self::Sum, context: &str, ) -> Result<Self::Sum, EvaluationError>

Multiply evaluated quantities without overflowing or producing a non-finite value.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl WeightElement for f64

Source§

const IS_UNIT: bool = false

Source§

type Sum = f64

Source§

fn unit() -> Self

Source§

fn validate_element(&self, context: &str) -> Result<(), ConstructionError>

Source§

fn to_sum(&self) -> f64

Source§

impl WeightElement for i64

Source§

const IS_UNIT: bool = false

Source§

type Sum = i64

Source§

fn unit() -> Self

Source§

fn validate_element(&self, _context: &str) -> Result<(), ConstructionError>

Source§

fn to_sum(&self) -> i64

Implementors§