use crate::events::ProofEvent;
pub trait ProofEventSink: Send + Sync + 'static {
fn emit(&self, event: ProofEvent);
fn flush(&self) {}
}
pub struct NullSink;
impl ProofEventSink for NullSink {
#[inline(always)]
fn emit(&self, _: ProofEvent) {}
}
pub struct ProofSink {
inner: Option<Box<dyn ProofEventSink>>,
}
impl ProofSink {
pub fn none() -> Self {
Self { inner: None }
}
pub fn new(s: impl ProofEventSink) -> Self {
Self {
inner: Some(Box::new(s)),
}
}
#[inline]
pub fn is_active(&self) -> bool {
self.inner.is_some()
}
#[inline]
pub fn emit(&self, event: ProofEvent) {
if let Some(s) = &self.inner {
s.emit(event);
}
}
#[inline]
pub fn emit_if(&self, f: impl FnOnce() -> ProofEvent) {
if self.inner.is_some() {
self.emit(f());
}
}
pub fn flush(&self) {
if let Some(s) = &self.inner {
s.flush();
}
}
}
pub struct ChannelSink {
tx: crossbeam::channel::Sender<ProofEvent>,
}
impl ChannelSink {
pub fn new(capacity: usize) -> (Self, crossbeam::channel::Receiver<ProofEvent>) {
let (tx, rx) = crossbeam::channel::bounded(capacity);
(Self { tx }, rx)
}
}
impl ProofEventSink for ChannelSink {
#[inline]
fn emit(&self, event: ProofEvent) {
let _ = self.tx.try_send(event);
}
}
use std::sync::{Arc, Mutex};
#[derive(Clone)]
pub struct CollectingSink {
events: Arc<Mutex<Vec<ProofEvent>>>,
}
impl CollectingSink {
pub fn new() -> Self {
Self {
events: Arc::new(Mutex::new(Vec::new())),
}
}
pub fn drain(&self) -> Vec<ProofEvent> {
self.events.lock().unwrap().drain(..).collect()
}
pub fn snapshot(&self) -> Vec<ProofEvent> {
self.events.lock().unwrap().clone()
}
}
impl Default for CollectingSink {
fn default() -> Self {
Self::new()
}
}
impl ProofEventSink for CollectingSink {
fn emit(&self, event: ProofEvent) {
self.events.lock().unwrap().push(event);
}
}