Gauge

Struct Gauge 

Source
#[repr(align(64))]
pub struct Gauge { /* private fields */ }
Expand description

Ultra-fast atomic gauge for f64 values

Uses IEEE 754 bit manipulation for atomic operations on floating-point values. Cache-line aligned to prevent false sharing.

Implementations§

Source§

impl Gauge

Source

pub fn new() -> Self

Create new gauge starting at zero

Source

pub fn try_add(&self, delta: f64) -> Result<()>

Try to add to current value with validation

Returns Err(MetricsError::InvalidValue) if delta is not finite (NaN or ±inf) or if the resulting value would become non-finite. Returns Err(MetricsError::Overflow) if the sum overflows to a non-finite value.

Example

use metrics_lib::{Gauge, MetricsError};
let g = Gauge::with_value(1.5);
g.try_add(2.5).unwrap();
assert_eq!(g.get(), 4.0);
assert!(matches!(g.try_add(f64::INFINITY), Err(MetricsError::InvalidValue{..})));
Source

pub fn try_sub(&self, delta: f64) -> Result<()>

Try to subtract from current value (validated)

Validation semantics are identical to Gauge::try_add but apply to subtraction (-delta).

Example

use metrics_lib::Gauge;
let g = Gauge::with_value(10.0);
g.try_sub(4.0).unwrap();
assert_eq!(g.get(), 6.0);
Source

pub fn with_value(initial: f64) -> Self

Create gauge with initial value

Source

pub fn set(&self, value: f64)

Set gauge value - THE FASTEST PATH

This is optimized for maximum speed:

  • Convert f64 to IEEE 754 bits
  • Single atomic store instruction
  • Relaxed memory ordering for speed
  • Inlined for zero function call overhead
Source

pub fn try_set(&self, value: f64) -> Result<()>

Try to set gauge value with validation

Returns Err(MetricsError::InvalidValue) if value is not finite (NaN or ±inf).

Example

use metrics_lib::{Gauge, MetricsError};
let g = Gauge::new();
assert!(g.try_set(42.0).is_ok());
assert!(matches!(g.try_set(f64::NAN), Err(MetricsError::InvalidValue{..})));
Source

pub fn get(&self) -> f64

Get current value - single atomic load

Source

pub fn add(&self, delta: f64)

Add to current value - atomic read-modify-write loop

Uses compare-and-swap loop for lock-free addition

Source

pub fn sub(&self, delta: f64)

Subtract from current value

Source

pub fn set_max(&self, value: f64)

Set to maximum of current value and new value

Source

pub fn try_set_max(&self, value: f64) -> Result<()>

Try to set to maximum of current value and new value

Returns Err(MetricsError::InvalidValue) if value is not finite. Otherwise sets the gauge to max(current, value) and returns Ok(()).

Example

use metrics_lib::{Gauge, MetricsError};
let g = Gauge::with_value(5.0);
g.try_set_max(10.0).unwrap();
assert_eq!(g.get(), 10.0);
assert!(matches!(g.try_set_max(f64::INFINITY), Err(MetricsError::InvalidValue{..})));
Source

pub fn set_min(&self, value: f64)

Set to minimum of current value and new value

Source

pub fn try_set_min(&self, value: f64) -> Result<()>

Try to set to minimum of current value and new value

Returns Err(MetricsError::InvalidValue) if value is not finite. Otherwise sets the gauge to min(current, value) and returns Ok(()).

Example

use metrics_lib::{Gauge, MetricsError};
let g = Gauge::with_value(10.0);
g.try_set_min(7.0).unwrap();
assert_eq!(g.get(), 7.0);
assert!(matches!(g.try_set_min(f64::NAN), Err(MetricsError::InvalidValue{..})));
Source

pub fn compare_and_swap(&self, expected: f64, new: f64) -> Result<f64, f64>

Atomic compare-and-swap

Returns Ok(previous_value) if successful, Err(current_value) if failed

Source

pub fn reset(&self)

Reset to zero

Source

pub fn multiply(&self, factor: f64)

Multiply current value by factor

Source

pub fn divide(&self, divisor: f64)

Divide current value by divisor

Source

pub fn abs(&self)

Set to absolute value of current value

Source

pub fn clamp(&self, min: f64, max: f64)

Clamp value to range [min, max]

Source

pub fn update_ema(&self, sample: f64, alpha: f64)

Exponential moving average update

new_value = alpha * sample + (1 - alpha) * old_value

Source

pub fn stats(&self) -> GaugeStats

Get comprehensive statistics

Source

pub fn age(&self) -> Duration

Get age since creation

Source

pub fn is_zero(&self) -> bool

Check if gauge is zero

Source

pub fn is_positive(&self) -> bool

Check if value is positive

Source

pub fn is_negative(&self) -> bool

Check if value is negative

Source

pub fn is_finite(&self) -> bool

Check if value is finite (not NaN or infinity)

Trait Implementations§

Source§

impl Debug for Gauge

Source§

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

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

impl Default for Gauge

Source§

fn default() -> Self

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

impl Display for Gauge

Source§

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

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

impl Send for Gauge

Source§

impl Sync for Gauge

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

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

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.