use core::ops::Deref;
use crate::subsume::Shared;
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Snapshot<V> {
value: V,
}
impl<V> Snapshot<V> {
#[inline]
const fn new(value: V) -> Self {
Self { value }
}
#[inline]
pub const fn get(&self) -> &V {
&self.value
}
#[inline]
pub fn into_inner(self) -> V {
self.value
}
}
impl<V> Deref for Snapshot<V> {
type Target = V;
#[inline]
fn deref(&self) -> &V {
&self.value
}
}
pub struct WatchView<C, V, H> {
shared: Shared<C, V, H>,
}
impl<C, V, H> WatchView<C, V, H> {
#[inline]
pub(crate) fn new(shared: Shared<C, V, H>) -> Self {
Self { shared }
}
}
impl<C, V, H> core::fmt::Debug for WatchView<C, V, H> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("WatchView").finish_non_exhaustive()
}
}
impl<C, V, H> WatchView<C, V, H>
where
C: Ord + Clone,
{
#[inline]
#[must_use]
pub fn is_watched(&self, key: &[C]) -> bool {
self.shared.load().covers.get_ancestor(key).is_some()
}
#[inline]
#[must_use]
pub fn contains(&self, key: &[C]) -> bool {
self.shared.load().roots.contains(key)
}
#[inline]
#[must_use]
pub fn covering(&self, key: &[C]) -> Option<Snapshot<V>>
where
V: Clone,
{
self
.shared
.load()
.covers
.get_ancestor(key)
.map(|entry| Snapshot::new(entry.value().clone()))
}
#[inline]
#[must_use]
pub fn resolve(&self, key: &[C]) -> Option<Snapshot<V>>
where
V: Clone,
{
self.covering(key)
}
}
impl<C, V, H> WatchView<C, V, H> {
#[inline]
#[must_use]
pub fn len(&self) -> usize {
self.shared.load().roots.len()
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.shared.load().roots.is_empty()
}
}
impl<C, V, H> Clone for WatchView<C, V, H> {
#[inline]
fn clone(&self) -> Self {
Self {
shared: self.shared.clone(),
}
}
}