Struct InvocationCount

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

Counts the number of invocations of a function.

The constructor with_fn constructs the metric and a counted function which is used in place of the original function. with_fn_mut is for FnMut functions.

The record and store methods take a Never type and cannot be called directly, instead the count is incremented by invoking the counted function.

⚠️ Since many closures and all function pointers are Copy, be careful not to reuse the uncounted original function after creating the metric.

let uncounted_fn = |x: i32| x * 2;
let (metric1, counted_fn) = InvocationCount::with_fn(uncounted_fn);

assert_eq!(uncounted_fn(1), 2); // not counted
assert_eq!(uncounted_fn(2), 4); // not counted

assert_eq!(counted_fn(1), 2);
assert_eq!(counted_fn(2), 4);
assert_eq!(counted_fn(3), 6);

assert_eq!(metric1.value(), 3);

Implementations§

Source§

impl InvocationCount

Source

pub fn value(&self) -> u64

The number of times the function has been invoked.

Source

pub fn with_fn<F, X, O>(f: F) -> (Self, impl Fn(X) -> O)
where F: Fn(X) -> O,

Creates the metric and a wrapped counted function, where target function is Fn

Source

pub fn with_fn_mut<F, X, O>(f: F) -> (Self, impl FnMut(X) -> O)
where F: FnMut(X) -> O,

Creates the metric and a wrapped counted function, where target function is FnMut

Trait Implementations§

Source§

impl Clone for InvocationCount

Source§

fn clone(&self) -> InvocationCount

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for InvocationCount

Source§

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

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

impl Default for InvocationCount

Source§

fn default() -> InvocationCount

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

impl Metric<Never> for InvocationCount

Source§

type Output = u64

The type of elements recorded by the metric. For a delta of last vs current, this might be an n-dimentional vector. It typically needs to be an Owned type, rather than a reference. Read more
Source§

fn observe_opt(&mut self, _: Never) -> Option<Self::Output>

Similar to observe except None is returned instead of f64::NAN to indicate not enough data collected. Useful if you like Read more
Source§

fn observe(&mut self, x: Input) -> f64
where Self::Output: Into<f64>,

Records the value, and return the calculated metric. If there are not enough samples to calculate the metric, f64::NAN is returned, which will always compare false. So a tolerance check observe(x) < 0.0001 will fail until enough samples have been collected. If you don’t like NANs, then Metric::observe_opt is your friend.
Source§

impl StatefulMetric<Never> for InvocationCount

Source§

fn store(&mut self, _: Never)

Stores the value, and likely discards older values.
Source§

fn value_opt(&self) -> Option<Self::Output>

The calculated value. Read more
Source§

fn observe_opt(&mut self, x: Input) -> Option<Self::Output>

Source§

fn value(&self) -> f64
where Self::Output: Into<f64>,

The calculated value. Read more

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