Skip to main content

dive_eval/
capture.rs

1//! Per-thread capture of `Print` output, for a parallel checker that
2//! releases output in TLC's sequential order.
3//!
4//! While a [`PrintCapture`] is active on a thread, `Print`, `PrintT` and the
5//! evaluator's once-per-run warnings evaluated on that thread go into the
6//! capture instead of the evaluator's sink. The checker drains the capture
7//! at the boundaries of the reference order (before a successor, after its
8//! constraints, after its invariants, ...), tags each batch with its
9//! position, and later releases the batches in position order through
10//! [`crate::Evaluator::release`].
11//!
12//! Warnings are printed once per run. Which evaluation prints a warning
13//! must not depend on scheduling, so a captured warning is not checked
14//! against the run's warning history when it is raised: [`Captured::warning`]
15//! marks it, and [`crate::Evaluator::release`] keeps it only if it is the
16//! first of its text released in the run.
17//!
18//! ```
19//! use dive_eval::capture::PrintCapture;
20//!
21//! let cap = PrintCapture::start();
22//! assert!(cap.take().is_empty());
23//! drop(cap); // output goes to the evaluator's sink again
24//! ```
25
26use std::cell::RefCell;
27use std::marker::PhantomData;
28use std::rc::Rc;
29
30/// One captured line.
31#[derive(Clone, Debug, PartialEq, Eq)]
32pub struct Captured {
33    /// The text, as the sink would receive it.
34    pub text: String,
35    /// Whether it is a once-per-run warning (see the module documentation).
36    pub warning: bool,
37}
38
39thread_local! {
40    static CAPTURE: RefCell<Option<Vec<Captured>>> = const { RefCell::new(None) };
41}
42
43/// Captures `Print` output on the current thread while it lives. Captures
44/// nest: dropping one restores the enclosing capture, whose lines are kept.
45#[derive(Debug)]
46pub struct PrintCapture {
47    saved: Option<Vec<Captured>>,
48    /// Bound to the thread it started on.
49    _thread: PhantomData<Rc<()>>,
50}
51
52impl PrintCapture {
53    /// Starts capturing on the current thread.
54    #[must_use]
55    pub fn start() -> Self {
56        let saved = CAPTURE.with(|c| c.borrow_mut().replace(Vec::new()));
57        Self {
58            saved,
59            _thread: PhantomData,
60        }
61    }
62
63    /// Takes the lines captured since the last call.
64    #[must_use]
65    pub fn take(&self) -> Vec<Captured> {
66        CAPTURE.with(|c| {
67            c.borrow_mut()
68                .as_mut()
69                .map(std::mem::take)
70                .unwrap_or_default()
71        })
72    }
73}
74
75impl Drop for PrintCapture {
76    fn drop(&mut self) {
77        let saved = self.saved.take();
78        CAPTURE.with(|c| *c.borrow_mut() = saved);
79    }
80}
81
82/// Adds a line to the current thread's capture. Returns the line back if no
83/// capture is active.
84pub(crate) fn push(text: String, warning: bool) -> Option<String> {
85    CAPTURE.with(|c| match c.borrow_mut().as_mut() {
86        Some(lines) => {
87            lines.push(Captured { text, warning });
88            None
89        }
90        None => Some(text),
91    })
92}
93
94/// Whether a capture is active on the current thread.
95pub(crate) fn active() -> bool {
96    CAPTURE.with(|c| c.borrow().is_some())
97}