use std::panic::{RefUnwindSafe, UnwindSafe};
use std::rc::Rc;
use std::sync::Arc;
#[cfg(test)]
use crate::ObservationBagSnapshot;
use crate::{Magnitude, ObservationBag, ObservationBagSync, Observations, Sealed};
#[expect(private_bounds, reason = "intentionally sealed trait")]
pub trait PublishModel: PublishModelPrivate + Sealed {}
pub(crate) trait PublishModelPrivate {
fn insert(&self, magnitude: Magnitude, count: usize);
#[cfg(test)]
fn snapshot(&self) -> ObservationBagSnapshot;
}
#[derive(Debug)]
pub struct Push {
pub(crate) observations: Rc<ObservationBag>,
}
impl UnwindSafe for Push {}
impl RefUnwindSafe for Push {}
impl Sealed for Push {}
impl PublishModel for Push {}
impl PublishModelPrivate for Push {
#[cfg(test)]
fn snapshot(&self) -> ObservationBagSnapshot {
self.observations.snapshot()
}
fn insert(&self, magnitude: Magnitude, count: usize) {
self.observations.insert(magnitude, count);
}
}
#[derive(Debug)]
pub struct Pull {
pub(crate) observations: Arc<ObservationBagSync>,
}
impl Sealed for Pull {}
impl PublishModel for Pull {}
impl PublishModelPrivate for Pull {
#[cfg(test)]
fn snapshot(&self) -> ObservationBagSnapshot {
self.observations.snapshot()
}
fn insert(&self, magnitude: Magnitude, count: usize) {
self.observations.insert(magnitude, count);
}
}
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use std::panic::{RefUnwindSafe, UnwindSafe};
use static_assertions::assert_impl_all;
use super::*;
assert_impl_all!(Push: UnwindSafe, RefUnwindSafe);
assert_impl_all!(Pull: UnwindSafe, RefUnwindSafe);
}