use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
static REVISION: AtomicU64 = AtomicU64::new(0);
pub fn bump_revision() -> u64 {
REVISION.fetch_add(1, Ordering::SeqCst) + 1
}
pub fn current_revision() -> u64 {
REVISION.load(Ordering::SeqCst)
}
#[derive(Debug)]
pub struct Snapshot {
revision: u64,
_data: Arc<()>,
}
impl Snapshot {
pub fn new() -> Self {
Self {
revision: current_revision(),
_data: Arc::new(()),
}
}
pub fn is_current(&self) -> bool {
self.revision == current_revision()
}
pub fn is_cancelled(&self) -> bool {
!self.is_current()
}
}
impl Default for Snapshot {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_snapshot_cancellation() {
let snap = Snapshot::new();
assert!(snap.is_current());
bump_revision();
assert!(snap.is_cancelled());
}
}