use std::marker::PhantomData; #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Counter { value: usize, marker: PhantomData, } impl Default for Counter { fn default() -> Self { Self::new() } } impl From for Counter { fn from(value: usize) -> Self { Self { value, marker: PhantomData } } } impl Counter { #[must_use] pub fn new() -> Self { Self { value: 0, marker: PhantomData } } } impl> Counter { #[must_use] pub fn value(&self) -> T { self.value.into() } pub fn increment(&mut self) -> T { self.increment_by(1) } pub fn increment_by(&mut self, by: usize) -> T { self.value += by; self.value() } } impl core::fmt::Debug for Counter { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Counter({})", self.value) } }