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
//! One-shot execution utility
//!
//! A simple utility to ensure a callback is executed only once.
//!
//! # Example
//!
//! ```rust,ignore
//! use revue::utils::Once;
//!
//! let mut once = Once::new();
//! for _ in 0..10 {
//! if once.call() {
//! println!("This will only print once!");
//! }
//! }
//! ```
use std::sync::atomic::{AtomicBool, Ordering};
/// A one-shot execution guard
///
/// Ensures that code is executed only once, even if `call()` is invoked multiple times.
/// This is useful for initialization, cleanup, or ensuring side effects happen once.
///
/// # Thread Safety
///
/// `Once` uses atomic operations and is safe to use across threads.
#[derive(Debug, Default)]
pub struct Once {
/// Flag indicating if the action has been executed
executed: AtomicBool,
}
impl Once {
/// Create a new `Once` guard
///
/// # Example
///
/// ```rust
/// use revue::utils::Once;
///
/// let once = Once::new();
/// assert!(!once.is_called());
/// ```
pub fn new() -> Self {
Self {
executed: AtomicBool::new(false),
}
}
/// Attempt to execute the one-shot action
///
/// Returns `true` on the first call (allowing execution), and `false` on all
/// subsequent calls (preventing re-execution).
///
/// # Example
///
/// ```rust
/// use revue::utils::Once;
///
/// let mut once = Once::new();
/// assert!(once.call()); // First call returns true
/// assert!(!once.call()); // Subsequent calls return false
/// assert!(!once.call()); // Always returns false after first
/// ```
#[inline]
pub fn call(&mut self) -> bool {
self.call_impl()
}
/// Internal implementation using atomic operations
#[inline]
fn call_impl(&self) -> bool {
!self.executed.swap(true, Ordering::AcqRel)
}
/// Check if the one-shot has been called
///
/// # Example
///
/// ```rust
/// use revue::utils::Once;
///
/// let mut once = Once::new();
/// assert!(!once.is_called());
/// once.call();
/// assert!(once.is_called());
/// ```
#[inline]
pub fn is_called(&self) -> bool {
self.executed.load(Ordering::Acquire)
}
/// Reset the one-shot, allowing it to be called again
///
/// # Warning
///
/// This can be useful in certain scenarios, but be careful not to create
/// unexpected behavior. Use with caution!
///
/// # Example
///
/// ```rust
/// use revue::utils::Once;
///
/// let mut once = Once::new();
/// once.call();
/// assert!(!once.call()); // Already called
///
/// once.reset();
/// assert!(once.call()); // Can call again after reset
/// ```
#[inline]
pub fn reset(&mut self) {
self.executed.store(false, Ordering::Release);
}
/// Create a new `Once` that's already in the called state
///
/// This is useful when you want to skip execution based on some condition.
///
/// # Example
///
/// ```rust
/// use revue::utils::Once;
///
/// let already_initialized = true;
/// let mut once = Once::from(already_initialized);
///
/// if once.call() {
/// println!("This won't print");
/// }
/// ```
pub fn from(called: bool) -> Self {
Self {
executed: AtomicBool::new(called),
}
}
}
impl Clone for Once {
fn clone(&self) -> Self {
Self {
executed: AtomicBool::new(self.is_called()),
}
}
}
/// Helper function to create a new `Once` guard
///
/// # Example
///
/// ```rust
/// use revue::utils::once;
///
/// let mut one_shot = once();
/// if one_shot.call() {
/// // Execute once
/// }
/// ```
pub fn once() -> Once {
Once::new()
}