use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use tokio::sync::RwLock;
pub struct ProjectionHandle<P> {
pub(crate) state: Arc<RwLock<P>>,
pub(crate) offset: Arc<AtomicU64>,
}
impl<P> Clone for ProjectionHandle<P> {
fn clone(&self) -> Self {
Self { state: self.state.clone(), offset: self.offset.clone() }
}
}
impl<P: Send + Sync + 'static> ProjectionHandle<P> {
pub fn offset(&self) -> u64 {
self.offset.load(Ordering::Acquire)
}
pub async fn snapshot(&self) -> tokio::sync::RwLockReadGuard<'_, P> {
self.state.read().await
}
pub async fn read<R>(&self, f: impl FnOnce(&P) -> R) -> R {
let guard = self.state.read().await;
f(&*guard)
}
}