Skip to content

Commit

Permalink
Fix unbounded recursion in Metric impl for CoreMetric (metriken v0.3.…
Browse files Browse the repository at this point in the history
…5) (#104)

* Fix unbounded recursion in Metric impl for CoreMetric

All these infinitely recurse. Unfortunately the recursion was indirect
so it was not caught by rustc.

* Bump to v0.3.5

* formatting
  • Loading branch information
swlynch99 authored Jan 20, 2024
1 parent 5eadcbf commit b7b30f8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion metriken/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "metriken"
version = "0.3.4"
version = "0.3.5"
edition = "2021"
authors = [
"Brian Martin <brian@iop.systems>",
Expand Down
12 changes: 6 additions & 6 deletions metriken/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ pub mod export {

impl<T: Metric> metriken_core::Metric for WrapMetric<T> {
fn is_enabled(&self) -> bool {
<Self as Metric>::is_enabled(self)
<T as Metric>::is_enabled(self)
}

fn as_any(&self) -> Option<&dyn std::any::Any> {
<Self as Metric>::as_any(self)
<T as Metric>::as_any(self)
}

fn value(&self) -> Option<metriken_core::Value> {
let value = <Self as Metric>::value(self)?;
let value = <T as Metric>::value(self)?;

Some(match value {
crate::Value::Counter(val) => metriken_core::Value::Counter(val),
Expand Down Expand Up @@ -310,17 +310,17 @@ impl std::fmt::Debug for MetricEntry {

impl<T: metriken_core::Metric> Metric for T {
fn is_enabled(&self) -> bool {
<Self as metriken_core::Metric>::is_enabled(self)
<T as metriken_core::Metric>::is_enabled(self)
}

fn as_any(&self) -> Option<&dyn Any> {
<Self as metriken_core::Metric>::as_any(self)
<T as metriken_core::Metric>::as_any(self)
}

fn value(&self) -> Option<Value> {
use metriken_core::Value as CoreValue;

Some(match <Self as metriken_core::Metric>::value(self)? {
Some(match <T as metriken_core::Metric>::value(self)? {
CoreValue::Counter(val) => Value::Counter(val),
CoreValue::Gauge(val) => Value::Gauge(val),
CoreValue::Other(val) => {
Expand Down
14 changes: 14 additions & 0 deletions metriken/tests/as_any.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use metriken::{metric, metrics, Counter};

#[metric]
static THE_METRIC: Counter = Counter::new();

#[test]
fn as_any_does_not_recurse_infinitely() {
let metrics = metrics().static_metrics();
let metric = metrics.iter().find(|entry| entry.is(&THE_METRIC)).unwrap();

let counter = metric.as_any().unwrap().downcast_ref::<Counter>().unwrap();

assert_eq!(counter.value(), THE_METRIC.value());
}

0 comments on commit b7b30f8

Please sign in to comment.