Something went wrong. Try again.
An experiment in static, refcounted, procedural language with ownership
Something went wrong. Try again.
792 B · 42 lines
Rust
at master
12345678910111213141516171819202122232425262728293031323334353637383940414243use std::marker::PhantomData;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]pub struct Counter<T> { value: usize, marker: PhantomData<T>,}
impl<T> Default for Counter<T> { fn default() -> Self { Self::new() }}
impl<T> Counter<T> { pub fn new() -> Self { Self { value: 0, marker: PhantomData } }}
impl<T: From<usize>> Counter<T> { pub fn value(&self) -> T { self.value.into() }
pub fn increment(&mut self) -> T { self.value += 1; self.value() }}
impl<T> From<usize> for Counter<T> { fn from(value: usize) -> Self { Counter { value, marker: PhantomData } }}
impl<T> From<Counter<T>> for usize { fn from(value: Counter<T>) -> Self { value.value }}