use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Version(usize);
impl Version {
pub(super) fn next() -> Version {
Self(NEXT_VERSION.fetch_add(1, SeqCst))
}
pub fn current() -> Version {
Self(NEXT_VERSION.load(SeqCst) - 1)
}
pub fn number(self) -> usize {
self.0
}
}
static NEXT_VERSION: AtomicUsize = AtomicUsize::new(2);
#[cfg(test)]
mod tests {
use super::Version;
#[test]
fn version() {
let next = Version::next();
assert_eq!(next, Version::current());
assert_eq!(next, Version::current());
assert_eq!(next.0 + 1, Version::next().0)
}
}