register_count/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(not(feature = "std"), no_std)]
3
4#[cfg(feature = "std")]
5use std::sync::{Arc, Weak};
6
7#[cfg(not(feature = "std"))]
8extern crate alloc;
9
10#[cfg(not(feature = "std"))]
11use alloc::sync::{Arc, Weak};
12
13/// A counter that can be used to count the number of `Register`s referencing it.
14#[derive(Clone)]
15pub struct Counter(Arc<()>);
16
17/// A register.
18#[derive(Clone)]
19pub struct Register(Weak<()>);
20
21impl Counter {
22    /// Create a new counter.
23    pub fn new() -> Self {
24        Self(Arc::new(()))
25    }
26
27    /// Create a new register referencing this counter.
28    pub fn reg(&self) -> Register {
29        Register(Arc::downgrade(&self.0))
30    }
31
32    /// Get the number of registers referencing this counter.
33    pub fn count(&self) -> usize {
34        Arc::weak_count(&self.0)
35    }
36}