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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! `guarden` provides scoped guard macros for deferred cleanup and manual triggers.
//!
//! The public API centers on three macros:
//!
//! - [`guarded!`] for binding a guard to a local variable that runs on Drop.
//! - [`guard!`] for creating a guard value that can be triggered manually.
//! - [`defer!`] as a convenience alias for [`guarded!`].
//!
//! The macros support synchronous and asynchronous bodies, explicit capture lists,
//! and export controls for captured values.
//!
//! ### ⚠️ Critical Usage Note: Diverging Expressions
//!
//! Do not use "naked" diverging expressions—such as `panic!`, `todo!`, or `loop {}`—as
//! the sole content of a sync guard closure. This prevents the compiler from
//! distinguishing between synchronous (`ASYNC = false`) and asynchronous
//! (`ASYNC = true`) implementations, leading to a type inference error (E0277).
//!
//! ### Technical Context
//!
//! The `!` (Never Type) is a bottom type that can be coerced into any other type.
//! Because it satisfies both the `()` return type requirement for sync guards and the `Future`
//! trait requirement for async guards, the compiler encounters an inference deadlock.
//!
//! ### Workaround
//!
//! For macros like `guard!` or `guarded!`, force the closure to resolve to `()`
//! by explicitly setting the guard to `sync`:
//!
//! ```rust,should_panic
//! # use guarden::guarded;
//! let val = "critical failure".to_string();
//! guarded! {
//! sync [val] {
//! panic!("{}", val);
//! }
//! }
//! ```
extern crate self as guarden;
pub use __guarded;
/// Creates a [`ContextGuard`](guard::ContextGuard) object, binding it to a variable within the local scope.
///
/// ### Examples
///
/// ```rust
/// # use guarden::guarded;
/// let v1 = "1".to_string();
/// let v2 = "2".to_string();
/// let mut v4 = "4".to_string();
/// {
/// let v5 = "5".to_string();
/// guarded! {
/// guard => sync move export(all) [
/// v1,
/// mut v2,
/// v3 = "3".to_string(),
/// mut v4 = &mut v4
/// ] {
/// v2 += &v1;
/// *v4 += &v2;
/// *v4 += &v3;
/// *v4 += &v5;
/// assert_eq!(v2, "2.1");
/// }
/// }
/// *v2 += ".";
/// **v4 += ".";
/// assert_eq!(v1, "1");
/// assert_eq!(v2, "2.");
/// assert_eq!(v3, "3");
/// assert_eq!(*v4, "4.");
/// }
/// assert_eq!(v4, "4.2.135");
/// ```
///
/// #### Options
///
/// > **Syntax Order:** The macro requires options to appear in the exact order shown below if they are used.
///
/// * `[mut] guard =>` (**Optional**): The name of the variable to which the guard will be bound. Can be prefixed with `mut` to allow mutable access to the guard. If omitted, a default hidden variable is used, and the guard will be automatically triggered at the end of the current scope.
/// * `sync` (**Optional**): Forces the guard to be evaluated synchronously. Essential for avoiding type inference deadlocks when using diverging expressions (like `panic!`) as the sole content of the closure.
/// * `move` (**Optional**): Forces the underlying closure to take ownership of the captured variables.
/// * `export(all)` | `export(wrapped)` (**Optional**): Controls which captured variables are re-exported (made accessible) to the surrounding scope after the macro invocation.
/// * `export(all)`: Re-exports all captured variables, including explicitly initialized ones (e.g., `a = a.clone()`). **Note: This may shadow existing local variables in the outer scope.**
/// * `export(wrapped)`: Wraps all captured variables in a local struct, which can be accessed via `Deref`/`DerefMut` of the guard. No bindings are exported to the outer scope.
/// * **Default** (when omitted): Only shorthand captures (`mut arg` or `arg`) are re-exported.
///
/// > **⚠️ WARNING on Implicit Export Shadowing:**
/// > By default, or when using `export(all)`, captured variables are physically *moved* into the guard
/// > and then re-exported back to the outer scope as **references** (e.g., `&mut T` or `&T`) that shadow
/// > the original bindings. This alters their type and ownership semantics. If you need to preserve
/// > original ownership, either capture them explicitly as references (`a = &mut a`), or use `export(wrapped)`.
///
/// * `[ ... captures ... ]` (**Optional**): A comma-separated list of context variables to capture and make available within the guard. Supports:
/// * `mut arg = expr` (Mutable initialization)
/// * `arg = expr` (Immutable initialization)
/// * `mut arg` (Mutable shorthand capture)
/// * `arg` (Immutable shorthand capture)
/// * `{ ... }` or `expr` (**Required**): The body of the guard to be executed when triggered.
///
/// **Note:** For usage with `panic!` or `loop`, see the [module-level documentation](self)
/// regarding type inference deadlocks.
///
/// #### Named binding + mut arg visible outside + explicit drop
/// ```rust
/// # use guarden::guarded;
/// # use std::sync::atomic::{AtomicUsize, Ordering};
/// # use std::sync::Arc;
/// let sink = Arc::new(AtomicUsize::new(0));
/// {
/// let mut value = 6usize;
/// let delta = 1usize;
/// guarded!(guard => [mut value, delta, sink = sink.clone()] {
/// sink.store(value + delta, Ordering::SeqCst);
/// });
///
/// *value += 10;
/// assert_eq!(*value, 16);
/// assert_eq!(*delta, 1);
/// drop(guard);
/// }
/// assert_eq!(sink.load(Ordering::SeqCst), 17);
/// ```
///
/// #### Unnamed statement + expression body + implicit drop at scope end
/// ```rust
/// # use guarden::guarded;
/// # use std::sync::atomic::{AtomicUsize, Ordering};
/// # use std::sync::Arc;
/// let sink = Arc::new(AtomicUsize::new(0));
/// {
/// guarded!([n = 7usize, sink = sink.clone()] sink.store(n, Ordering::SeqCst));
/// }
/// assert_eq!(sink.load(Ordering::SeqCst), 7);
/// ```
///
/// #### Explicit sync + panic path propagates on drop
/// ```rust
/// # use guarden::guarded;
/// let dropped = std::panic::catch_unwind(|| {
/// guarded! {
/// sync {
/// panic!("boom");
/// }
/// }
/// });
/// assert!(dropped.is_err());
/// ```
///
/// #### Async inference + detaches and executes on drop
/// ```rust
/// # #[cfg(feature = "tokio")]
/// # {
/// # tokio_test::block_on(async {
/// # use guarden::guarded;
/// let (tx, rx) = tokio::sync::oneshot::channel();
/// {
/// let tx = Some(tx);
/// guarded!([mut tx] {
/// let tx = tx.take();
/// async move {
/// if let Some(tx) = tx {
/// let _ = tx.send(9usize);
/// }
/// }
/// });
/// }
/// let detached = tokio::time::timeout(std::time::Duration::from_secs(1), rx)
/// .await
/// .expect("detached task should complete")
/// .expect("detached task should send value");
/// assert_eq!(detached, 9);
/// # })
/// # }
/// ```
///
/// #### Init captures stay private and do not shadow outer locals
/// ```rust
/// # use guarden::guarded;
/// # use std::sync::atomic::{AtomicUsize, Ordering};
/// # use std::sync::Arc;
/// let sink = Arc::new(AtomicUsize::new(0));
/// {
/// let mut total = 100usize;
/// let step = 200usize;
///
/// guarded!(guard => [mut total = 10usize, step = 3usize, sink = sink.clone()] {
/// sink.store(total + step, Ordering::SeqCst);
/// });
///
/// total += 2;
/// assert_eq!(total, 102);
/// assert_eq!(step, 200);
/// drop(guard);
/// }
/// assert_eq!(sink.load(Ordering::SeqCst), 13);
/// ```
///
/// #### Async inference + init captures
/// ```rust
/// # #[cfg(feature = "tokio")]
/// # {
/// # tokio_test::block_on(async {
/// # use guarden::guarded;
/// let (tx, rx) = tokio::sync::oneshot::channel();
/// {
/// guarded!([mut tx = Some(tx), value = 13usize] {
/// let tx = tx.take();
/// async move {
/// if let Some(tx) = tx {
/// let _ = tx.send(value);
/// }
/// }
/// });
/// }
/// let detached = tokio::time::timeout(std::time::Duration::from_secs(1), rx)
/// .await
/// .expect("detached init-capture task should complete")
/// .expect("detached init-capture task should send value");
/// assert_eq!(detached, 13);
/// # })
/// # }
/// ```
///
/// #### Export all captured variables
/// ```rust
/// # use guarden::guarded;
/// # use std::sync::atomic::{AtomicUsize, Ordering};
/// # use std::sync::Arc;
/// let sink = Arc::new(AtomicUsize::new(0));
/// {
/// guarded! {
/// guard => export(all) [
/// mut a = 5usize,
/// b = 4usize,
/// sink = sink.clone()
/// ] {
/// sink.store(a + b, Ordering::SeqCst);
/// }
/// }
///
/// // Both `a` and `b` are exported because of export(all)
/// *a += 5;
/// assert_eq!(*a, 10);
/// assert_eq!(*b, 4);
/// }
/// assert_eq!(sink.load(Ordering::SeqCst), 14); // 10 + 4
/// ```
///
/// #### Wrapped captures accessed via mutable guard
/// ```rust
/// # use guarden::guarded;
/// # use std::sync::atomic::{AtomicUsize, Ordering};
/// # use std::sync::Arc;
/// let sink = Arc::new(AtomicUsize::new(0));
/// {
/// guarded! {
/// mut guard => export(wrapped) [
/// mut a = 5usize,
/// b = 4usize,
/// sink = sink.clone()
/// ] {
/// sink.store(a + b, Ordering::SeqCst);
/// }
/// }
///
/// // Modify 'a' through the guard's DerefMut
/// guard.a += 5;
/// assert_eq!(guard.a, 10);
/// assert_eq!(guard.b, 4);
/// }
/// assert_eq!(sink.load(Ordering::SeqCst), 14); // 10 + 4
/// ```
/// Creates a [`ContextGuard`](guard::ContextGuard) object, without binding it to a variable.
/// The macro evaluates to an expression returning the guard.
///
/// **Note:** For usage with `panic!` or `loop`, see the [module-level documentation](self)
/// regarding type inference deadlocks.
///
/// ### Examples
///
/// ```rust
/// # use guarden::guard;
/// let v1 = "1".to_string();
/// let v2 = "2".to_string();
/// let mut v4 = "4".to_string();
/// {
/// let v5 = "5".to_string();
/// struct Ctx<'s> {
/// v1: String,
/// v2: String,
/// v3: String,
/// v4: &'s mut String,
/// }
/// let mut guard = guard! {
/// sync move [
/// ctx = Ctx {
/// v1,
/// v2,
/// v3: "3".to_string(),
/// v4: &mut v4,
/// }
/// ] {
/// let Ctx { v1, mut v2, v3, v4 } = ctx;
/// v2 += &v1;
/// *v4 += &v2;
/// *v4 += &v3;
/// *v4 += &v5;
/// assert_eq!(v2, "2.1");
/// }
/// };
/// let Ctx { v1, v2, v3, v4 } = &mut *guard;
/// *v2 += ".";
/// **v4 += ".";
/// assert_eq!(v1, "1");
/// assert_eq!(v2, "2.");
/// assert_eq!(v3, "3");
/// assert_eq!(*v4, "4.");
/// }
/// assert_eq!(v4, "4.2.135");
/// ```
///
/// ##### Options
///
/// > **Syntax Order:** The macro requires options to appear in the exact order shown below if they are used.
///
/// * `sync` (**Optional**): Forces the guard to be evaluated synchronously. Essential for avoiding type inference deadlocks when using diverging expressions (like `panic!`) as the sole content of the closure.
/// * `move` (**Optional**): Forces the underlying closure to take ownership of the captured variables.
/// * `[ ... captures ... ]` (**Optional**): A comma-separated list of context variables to capture and make available within the guard. Supports:
/// * `mut arg = expr` (Mutable initialization)
/// * `arg = expr` (Immutable initialization)
/// * `mut arg` (Mutable shorthand capture)
/// * `arg` (Immutable shorthand capture)
/// * `{ ... }` or `expr` (**Required**): The body of the guard to be executed when triggered.
///
/// #### Block body + trailing comma inits + trigger()
/// ```rust
/// # use guarden::guard;
/// # use std::sync::atomic::{AtomicUsize, Ordering};
/// # use std::sync::Arc;
/// let sink = Arc::new(AtomicUsize::new(0));
/// let guard = guard!([v = 1usize, sink = sink.clone(),] {
/// sink.store(v, Ordering::SeqCst);
/// });
/// guard.trigger();
/// assert_eq!(sink.load(Ordering::SeqCst), 1);
/// ```
///
/// #### Expression body (no braces)
/// ```rust
/// # use guarden::guard;
/// # use std::sync::atomic::{AtomicUsize, Ordering};
/// # use std::sync::Arc;
/// let sink = Arc::new(AtomicUsize::new(0));
/// let guard = guard!([v = 2usize, sink = sink.clone()] sink.store(v, Ordering::SeqCst));
/// guard.trigger();
/// assert_eq!(sink.load(Ordering::SeqCst), 2);
/// ```
///
/// #### Explicit sync + no inits form
/// ```rust
/// # use guarden::guard;
/// # use std::sync::atomic::{AtomicUsize, Ordering};
/// # use std::sync::Arc;
/// let sink = Arc::new(AtomicUsize::new(0));
/// let guard = guard!(sync [sink = sink.clone()] {
/// loop {
/// sink.store(3, Ordering::SeqCst);
/// break;
/// }
/// });
/// guard.trigger();
/// assert_eq!(sink.load(Ordering::SeqCst), 3);
/// ```
///
/// #### Move capture + defuse() prevents execution
/// ```rust
/// # use guarden::guard;
/// # use std::sync::atomic::{AtomicUsize, Ordering};
/// # use std::sync::Arc;
/// let sink = Arc::new(AtomicUsize::new(0));
/// let guard = guard!([
/// owned = String::from("owned"),
/// sink = sink.clone()
/// ] {
/// if owned == "owned" {
/// sink.store(4, Ordering::SeqCst);
/// }
/// });
/// let (owned, _) = guard.defuse();
/// assert_eq!(owned, "owned");
/// assert_eq!(sink.load(Ordering::SeqCst), 0);
/// ```
///
/// #### Async block inference + trigger() returns task
/// ```rust
/// # #[cfg(feature = "tokio")]
/// # {
/// # tokio_test::block_on(async {
/// # use guarden::guard;
/// # use std::sync::atomic::{AtomicUsize, Ordering};
/// # use std::sync::Arc;
/// let sink = Arc::new(AtomicUsize::new(0));
/// let guard = guard!([n = 5usize, sink = sink.clone()] async move {
/// sink.fetch_add(n, Ordering::SeqCst);
/// });
/// guard.trigger().await;
/// assert_eq!(sink.load(Ordering::SeqCst), 5);
/// # })
/// # }
/// ```
///
/// #### Init capture (immutable) + trigger()
/// ```rust
/// # use guarden::guard;
/// # use std::sync::atomic::{AtomicUsize, Ordering};
/// # use std::sync::Arc;
/// let sink = Arc::new(AtomicUsize::new(0));
/// let guard = guard!([value = 10usize, sink = sink.clone()] {
/// sink.store(value, Ordering::SeqCst);
/// });
/// guard.trigger();
/// assert_eq!(sink.load(Ordering::SeqCst), 10);
/// ```
///
/// #### Init capture (mutable) + trigger()
/// ```rust
/// # use guarden::guard;
/// # use std::sync::atomic::{AtomicUsize, Ordering};
/// # use std::sync::Arc;
/// let sink = Arc::new(AtomicUsize::new(0));
/// let guard = guard!([mut text = String::from("a"), sink = sink.clone()] {
/// text.push_str("b");
/// sink.store(text.len(), Ordering::SeqCst);
/// });
/// guard.trigger();
/// assert_eq!(sink.load(Ordering::SeqCst), 2);
/// ```
///
/// #### Wrapped captures returned as expression
/// ```rust
/// # use guarden::guard;
/// # use std::sync::atomic::{AtomicUsize, Ordering};
/// # use std::sync::Arc;
/// let sink = Arc::new(AtomicUsize::new(0));
/// let mut guard = guard!(export(wrapped) [
/// mut text = String::from("a"),
/// sink = sink.clone()
/// ] {
/// text.push_str("b");
/// sink.store(text.len(), Ordering::SeqCst);
/// });
///
/// guard.text.push_str("x");
/// guard.trigger();
/// assert_eq!(sink.load(Ordering::SeqCst), 3); // "ax" + "b" = "axb"
/// ```
/// Alias for [`guarded!`].
///
/// **Note:** For usage with `panic!` or `loop`, see the [module-level documentation](self)
/// regarding type inference deadlocks.