1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! Cooperative cancellation for long-running operators.
//!
//! Long operators (meshers, solvers, refinement loops) can run for a while.
//! To let a caller stop them early, they take a [`Cancel`] token and poll it
//! periodically; returning [`PyrucastError::Interrupted`] aborts the
//! computation cleanly (no partial result is committed).
//!
//! The key design point is the **Rust/Python boundary**: [`Cancel`] is a
//! plain Rust trait with **no dependency on PyO3**. The *operator* never
//! knows how cancellation is signalled — that is the frontend's job:
//!
//! - **Pure Rust** callers pass [`NoCancel`] (never cancels), a
//! [`Deadline`] (timeout), or an [`std::sync::atomic::AtomicBool`] flipped
//! by their own `Ctrl+C` handler (e.g. the `ctrlc` crate) or a control
//! thread. The same `AtomicBool` is the natural way to cancel
//! shared-memory parallel work, where each worker polls the flag.
//! - The **Python** binding (under the `python-api` feature, in `src/py`)
//! provides its own token that calls `Python::check_signals`, turning a
//! `Ctrl+C` (`SIGINT`) into [`PyrucastError::Interrupted`] →
//! `KeyboardInterrupt`. That token lives entirely in the FFI layer, so the
//! operator core stays PyO3-free and usable from a pure Rust program.
use crate;
/// A cancellation check polled periodically by long-running operators.
///
/// `check` returns `Ok(())` to continue, or `Err(PyrucastError::Interrupted)`
/// to request a clean stop. Implementations decide *how* cancellation is
/// signalled, keeping operators free of any frontend concern.
// ANCHOR: trait
///
/// ```
/// # use pyrucast::error::PyrucastError;
/// # use pyrucast::interrupt::{Cancel, Deadline};
/// # use std::sync::atomic::{AtomicBool, Ordering};
/// // The token decides **how** the stop is signalled; the operators
/// // n'en savent rien, et restent libres de toute considération de
/// // frontal — Ctrl+C, délai, bouton d'interface.
/// let stop = AtomicBool::new(false);
/// assert!(stop.check().is_ok());
/// stop.store(true, Ordering::Relaxed);
/// assert!(matches!(stop.check(), Err(PyrucastError::Interrupted)));
/// ```
// ANCHOR_END: trait
/// Forward through shared references, so `&token` is itself a [`Cancel`].
/// Never cancels. The default for Rust callers that don't need it.
///
/// ```
/// # use pyrucast::interrupt::{Cancel, NoCancel};
/// // The token that never arms: what the public functions pass.
/// assert!(NoCancel.check().is_ok());
/// ```
;
/// `()` also works as a no-op token, for the terse call site `&()`.
/// Cancel as soon as the flag reads `true` — set it from a signal handler, a
/// control thread, or (later) the supervisor of a parallel region.
/// Cancel once a deadline has passed (a wall-clock timeout).
///
/// ```
/// # use pyrucast::error::PyrucastError;
/// # use pyrucast::interrupt::{Cancel, Deadline};
/// # use std::time::{Duration, Instant};
/// // A wall-clock deadline: the same interface as a hand-armed token.
/// assert!(Deadline(Instant::now() + Duration::from_secs(3600)).check().is_ok());
/// assert!(Deadline(Instant::now() - Duration::from_secs(1)).check().is_err());
/// ```
;