Skip to main content

TimingCounter

Trait TimingCounter 

Source
pub trait TimingCounter {
    const INITIAL: Self;

    // Required methods
    fn increment_count(&self);
    fn add_elapsed_ticks(&self, elapsed_ticks: u64);
}
Expand description

Receives measurements collected by timed_span!.

The macro calls Self::increment_count when a timed scope begins and Self::add_elapsed_ticks when it ends. Implementations may store those values directly, aggregate them differently, or update additional metrics. timing_entries! uses Self::INITIAL to create each element of a generated static counter array.

§Example

This counter computes a running average on demand from its sample count and total ticks:

use std::sync::atomic::{AtomicU64, Ordering};
use span_timing::TimingCounter;

struct AverageCounter {
    samples: AtomicU64,
    total_ticks: AtomicU64,
}

impl AverageCounter {
    fn average_ticks(&self) -> Option<u64> {
        let samples = self.samples.load(Ordering::Relaxed);
        (samples != 0).then(|| self.total_ticks.load(Ordering::Relaxed) / samples)
    }
}

impl TimingCounter for AverageCounter {
    const INITIAL: Self = Self {
        samples: AtomicU64::new(0),
        total_ticks: AtomicU64::new(0),
    };

    fn increment_count(&self) {
        self.samples.fetch_add(1, Ordering::Relaxed);
    }

    fn add_elapsed_ticks(&self, ticks: u64) {
        self.total_ticks.fetch_add(ticks, Ordering::Relaxed);
    }
}

Required Associated Constants§

Source

const INITIAL: Self

The const value used to initialize each element of a static counter collection.

Required Methods§

Source

fn increment_count(&self)

Records one invocation of the timed operation, before the timed scope runs.

Source

fn add_elapsed_ticks(&self, elapsed_ticks: u64)

Records the elapsed processor-counter ticks or nanoseconds when the scope ends.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§

Source§

impl TimingCounter for Counter

Available on target_has_atomic=64 only.
Source§

const INITIAL: Self