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
impl InvocationCount
Sourcepub fn with_fn<F, X, O>(f: F) -> (Self, impl Fn(X) -> O)where
F: Fn(X) -> O,
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
Sourcepub fn with_fn_mut<F, X, O>(f: F) -> (Self, impl FnMut(X) -> O)where
F: FnMut(X) -> O,
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
impl Clone for InvocationCount
Source§fn clone(&self) -> InvocationCount
fn clone(&self) -> InvocationCount
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for InvocationCount
impl Debug for InvocationCount
Source§impl Default for InvocationCount
impl Default for InvocationCount
Source§fn default() -> InvocationCount
fn default() -> InvocationCount
Returns the “default value” for a type. Read more
Source§impl Metric<Never> for InvocationCount
impl Metric<Never> for InvocationCount
Source§type Output = u64
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>
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 moreSource§fn observe(&mut self, x: Input) -> f64
fn observe(&mut self, x: Input) -> 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
impl StatefulMetric<Never> for InvocationCount
fn observe_opt(&mut self, x: Input) -> Option<Self::Output>
Auto Trait Implementations§
impl Freeze for InvocationCount
impl RefUnwindSafe for InvocationCount
impl Send for InvocationCount
impl Sync for InvocationCount
impl Unpin for InvocationCount
impl UnwindSafe for InvocationCount
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more