ifc_lite_geometry/
pending_diagnostics.rs

1// SPDX-License-Identifier: MPL-2.0
2//! Ownership of pending thread-local reports during a synchronous work slice.
3//! Router logs, source-claim state and process-global measurements stay outside.
4use super::{take_pending_mapped_bool_failures, BoolFailure, PENDING_MAPPED_BOOL_FAILURES};
5use ifc_lite_core::IfcType;
6use std::marker::PhantomData;
7
8/// Curve-cap and refused-face reports pending on this thread.
9#[derive(Debug, Default)]
10struct SourceReports {
11    curve_capped: bool,
12    face_refused: Vec<IfcType>,
13}
14
15#[derive(Debug, Default)]
16struct Pending {
17    source: SourceReports,
18    mapped: Vec<BoolFailure>,
19}
20impl Pending {
21    fn take_current() -> Self {
22        Self {
23            source: SourceReports {
24                curve_capped: crate::processors::take_curve_capped(),
25                face_refused: crate::processors::take_face_refused(),
26            },
27            mapped: take_pending_mapped_bool_failures(),
28        }
29    }
30    fn publish(self) {
31        crate::processors::restore_reports(self.source.curve_capped, &self.source.face_refused);
32        PENDING_MAPPED_BOOL_FAILURES.with(|pending| pending.borrow_mut().extend(self.mapped));
33    }
34}
35
36/// Pending reports retained by one work owner between synchronous slices.
37/// The value may move between workers; an installed scope cannot. Starting a
38/// state neither adopts ambient reports nor drains router processor logs.
39#[derive(Debug, Default)]
40pub struct PendingDiagnosticState {
41    pending: Pending,
42}
43impl PendingDiagnosticState {
44    pub fn new() -> Self {
45        Self::default()
46    }
47
48    /// Deliberately adopt only the legacy pending-mapped CSG channel at owner
49    /// admission. Curve/face reports already pending for the caller stay ambient.
50    /// Call once at the owning executor's first live admission, not planning.
51    pub fn adopt_current_mapped(&mut self) {
52        let mut older = take_pending_mapped_bool_failures();
53        older.append(&mut self.pending.mapped);
54        self.pending.mapped = older;
55    }
56
57    /// Install owner reports, retain new leftovers on return/error/unwind, and
58    /// restore the caller's pending reports exactly. No scope crosses a yield.
59    /// Work that drains reports through its ordinary router still does so here;
60    /// this primitive does not introduce a per-poll processor drain.
61    pub fn with_scope<T>(&mut self, work: impl FnOnce() -> T) -> T {
62        let caller = Pending::take_current();
63        std::mem::take(&mut self.pending).publish();
64        let _scope = Scope {
65            owner: self,
66            caller: Some(caller),
67            _thread_bound: PhantomData,
68        };
69        work()
70    }
71
72    /// Publish this owner's remaining reports once at its validated terminal
73    /// boundary. The caller chooses success/error/cancellation delivery policy;
74    /// dropping a state never silently publishes it into another owner's TLS.
75    /// Existing caller face ordering and mapped-record prefixes are retained.
76    pub fn publish_pending(&mut self) {
77        std::mem::take(&mut self.pending).publish();
78    }
79}
80
81struct Scope<'a> {
82    owner: &'a mut PendingDiagnosticState,
83    caller: Option<Pending>,
84    _thread_bound: PhantomData<*const ()>,
85}
86impl Drop for Scope<'_> {
87    fn drop(&mut self) {
88        self.owner.pending = Pending::take_current();
89        if let Some(caller) = self.caller.take() {
90            caller.publish();
91        }
92    }
93}
94
95#[cfg(test)]
96#[path = "pending_diagnostics_tests.rs"]
97mod tests;