use crate::clear::{Clear, Clearable};
pub use aspect::{Advice, Enter, OnResult, OnResultMut};
use serde::Serialize;
use std::marker::PhantomData;
pub trait Metric<R>: Default + OnResultMut<R> + Clear + Serialize {}
#[doc(hidden)]
pub fn on_result<R, A: Metric<R>>(metric: &A, _enter: <A as Enter>::E, _result: &mut R) -> Advice {
metric.on_result(_enter, _result)
}
pub struct ExitGuard<'a, R, M: Metric<R>> {
metric: &'a M,
enter: Option<<M as Enter>::E>,
_phantom: PhantomData<R>,
}
impl<'a, R, M: Metric<R>> ExitGuard<'a, R, M> {
pub fn new(metric: &'a M) -> Self {
Self {
metric,
enter: Some(metric.enter()),
_phantom: PhantomData,
}
}
pub fn on_result(mut self, result: &mut R) {
if let Some(enter) = self.enter.take() {
self.metric.on_result(enter, result);
} else {
}
}
}
impl<'a, R, M: Metric<R>> Drop for ExitGuard<'a, R, M> {
fn drop(&mut self) {
if let Some(enter) = self.enter.take() {
self.metric.leave_scope(enter);
} else {
}
}
}
pub trait Counter: Default + Clear + Clearable + Serialize {
fn incr(&self) {
self.incr_by(1)
}
fn incr_by(&self, count: usize);
}
pub trait Gauge: Default + Clear + Serialize {
fn incr(&self) {
self.incr_by(1)
}
fn decr(&self) {
self.decr_by(1)
}
fn incr_by(&self, count: usize);
fn decr_by(&self, count: usize);
}
pub trait Histogram: Clear + Serialize {
fn with_bound(max_value: u64) -> Self;
fn record(&self, value: u64);
}