PkmnLib_rs/src/utils/value_identifier.rs

34 lines
861 B
Rust

use std::sync::atomic::{AtomicUsize, Ordering};
/// The current index for the value counter.
static CURRENT: AtomicUsize = AtomicUsize::new(1);
/// An extremely basic way to identify a piece of data.
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[repr(C)]
pub struct ValueIdentifier(usize);
impl Default for ValueIdentifier {
fn default() -> Self {
Self(CURRENT.fetch_add(1, Ordering::SeqCst))
}
}
impl ValueIdentifier {
/// Creates an identifier by number.
pub(crate) fn new(v: usize) -> Self {
Self(v)
}
/// Get the underlying numeric integer of the value
pub fn value(&self) -> usize {
self.0
}
}
/// An object with a specific identifier.
pub trait ValueIdentifiable {
/// Get the identifier for the current object.
fn value_identifier(&self) -> ValueIdentifier;
}