take-once 0.1.3

A thread-safe container for one-time storage and one-time consumption of a value.
Documentation
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
//! A thread-safe container for one-time storage and one-time consumption of a value.
//!
//! This module provides the [`TakeOnce`] type, which enables safe storage
//! and consumption of a value in concurrent contexts. It ensures that:
//!
//! * A value can be stored exactly once
//! * A stored value can be taken out exactly once
//! * All operations are thread-safe
//!
//! This is similar to [`std::sync::OnceLock`], but with different semantics regarding
//! value consumption. `TakeOnce` allows the stored value to be taken out (moved) exactly once,
//! whereas `OnceLock` allows the value to be accessed in place multiple times.
//!
//! # Example
//!
#![cfg_attr(feature = "_shuttle", doc = "```ignore")]
#![cfg_attr(not(feature = "_shuttle"), doc = "```rust")]
//! use take_once::TakeOnce;
//!
//! let cell = TakeOnce::new();
//!
//! // Store a value
//! assert_eq!(cell.store(42), Ok(()));
//!
//! // Subsequent stores do not consume the value.
//! assert_eq!(cell.store(24), Err(24));
//!
//! // Take the stored value
//! assert_eq!(cell.take(), Some(42));
//!
//! // Value can only be taken once
//! assert_eq!(cell.take(), None);
//! ```
//!
//! # Thread Safety
//!
//! `TakeOnce<T>` is both [`Send`] and [`Sync`] when `T: Send`, making it suitable for
//! sharing across thread boundaries. All operations are atomic and properly synchronized.
//!
#![cfg_attr(feature = "_shuttle", doc = "```ignore")]
#![cfg_attr(not(feature = "_shuttle"), doc = "```rust")]
//! # use std::sync::Arc;
//! # use std::thread;
//! # use take_once::TakeOnce;
//! let shared = Arc::new(TakeOnce::new());
//! let shared2 = shared.clone();
//!
//! // Store in one thread
//! thread::spawn(move || {
//!     shared.store(42).unwrap();
//! }).join().unwrap();
//!
//! // Take in another thread
//! thread::spawn(move || {
//!     if let Some(value) = shared2.take() {
//!         println!("Got value: {}", value);
//!     }
//! });

use std::marker::PhantomData;

#[cfg(feature = "_shuttle")]
pub(crate) use shuttle::sync::{
    atomic::{AtomicPtr, Ordering},
    Once,
};

#[cfg(not(feature = "_shuttle"))]
pub(crate) use std::sync::{
    atomic::{AtomicPtr, Ordering},
    Once,
};

/// A thread-safe container that allows storing a value once and taking it exactly once.
/// This is useful in scenarios where:
///
/// * A value needs to be initialized lazily but only once
/// * The initialized value should be consumable (moved out) rather than just accessible.
///   See [`std::sync::OnceLock`] for a similar use case where the value can be accessed in place.
/// * Multiple threads might attempt to initialize or consume the value, but only one should succeed
///
/// # Thread Safety
///
/// `TakeOnce<T>` implements `Send` and `Sync` when `T: Send`, making it safe to share
/// across thread boundaries. All operations are atomic and properly synchronized.
///
/// # Memory Management
///
/// The stored value is heap-allocated and properly cleaned up when the `TakeOnce` is dropped.
///
/// # Examples
///
/// Basic usage:
#[cfg_attr(feature = "_shuttle", doc = "```ignore")]
#[cfg_attr(not(feature = "_shuttle"), doc = "```rust")]
/// use take_once::TakeOnce;
///
/// let cell = TakeOnce::new();
///
/// // Initial store succeeds
/// assert_eq!(cell.store(42), Ok(()));
///
/// // Subsequent stores return the provided value
/// assert_eq!(cell.store(24), Err(24));
///
/// // Take the value
/// assert_eq!(cell.take(), Some(42));
///
/// // Can't take twice
/// assert_eq!(cell.take(), None);
/// ```
///
/// Concurrent usage:
#[cfg_attr(feature = "_shuttle", doc = "```ignore")]
#[cfg_attr(not(feature = "_shuttle"), doc = "```rust")]
/// use std::sync::Arc;
/// use std::thread;
/// use take_once::TakeOnce;
///
/// let shared = Arc::new(TakeOnce::new());
/// let threads: Vec<_> = (0..3)
///     .map(|i| {
///         let shared = shared.clone();
///         thread::spawn(move || {
///             // Only one thread will successfully store
///             shared.store(i)
///         })
///     })
///     .collect();
/// ```
#[derive(Debug)]
pub struct TakeOnce<T> {
    once: Once,
    // Whether or not the value is initialized is tracked by `once.is_completed()`.
    value: AtomicPtr<T>,
    _marker: PhantomData<T>,
}

impl<T> TakeOnce<T> {
    /// Creates a new empty cell.
    #[inline]
    #[must_use]
    pub const fn new() -> TakeOnce<T> {
        TakeOnce {
            once: Once::new(),
            value: AtomicPtr::new(std::ptr::null_mut()),
            _marker: PhantomData,
        }
    }

    /// Create and store a cell in a single operation
    ///
    #[cfg_attr(feature = "_shuttle", doc = "```ignore")]
    #[cfg_attr(not(feature = "_shuttle"), doc = "```rust")]
    /// use take_once::TakeOnce;
    ///
    /// let initialized = TakeOnce::new_with(true);
    /// assert_eq!(initialized.store(false), Err(false));
    /// assert_eq!(initialized.take(), Some(true));
    /// ```
    #[must_use]
    pub fn new_with(val: T) -> TakeOnce<T> {
        let cell = TakeOnce::new();
        let _ = cell.store(val);
        cell
    }

    /// Stores a value into this `TakeOnce` if it has not been initialized.
    ///
    /// If the `TakeOnce` has already been initialized, the value is returned as [`Err`].
    /// Otherwise, the value is stored and [`Ok`] is returned.
    ///
    /// This method allocates memory on the heap to store the value.
    #[inline]
    pub fn store(&self, val: T) -> Result<(), T> {
        let mut val = Some(val);
        self.once.call_once(|| {
            let val = val.take().unwrap();
            let ptr = Box::into_raw(Box::new(val));
            self.value.store(ptr, Ordering::Release);
        });

        val.map_or(Ok(()), Err)
    }

    /// Takes the value out of this `TakeOnce`, if it has been initialized.
    ///
    /// If the `TakeOnce` has not been initialized, or if the value has already been taken,
    /// this method returns `None`.
    #[inline]
    #[must_use]
    pub fn take(&self) -> Option<T> {
        if self.once.is_completed() {
            let ptr = self.value.swap(std::ptr::null_mut(), Ordering::Acquire);
            if ptr.is_null() {
                None
            } else {
                // SAFETY: `self.value` is initialized (since `self.once.is_completed()`)
                // and has not been taken before (since `ptr` is not null).
                Some(*unsafe { Box::from_raw(ptr) })
            }
        } else {
            None
        }
    }

    /// Returns true if the value has been initialized, regardless of whether it has been taken.
    /// In other words, this returns true if `store` has been called at least once.
    #[inline]
    #[must_use]
    pub fn is_completed(&self) -> bool {
        self.once.is_completed()
    }
}

impl<T> Default for TakeOnce<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Drop for TakeOnce<T> {
    fn drop(&mut self) {
        if self.once.is_completed() {
            let ptr = self.value.swap(std::ptr::null_mut(), Ordering::Acquire);
            if !ptr.is_null() {
                // SAFETY: `self.value` is initialized (since `self.once.is_completed()`)
                // and has not been taken before (since `ptr` is not null).
                drop(unsafe { Box::from_raw(ptr) });
            }
        }
    }
}

// SAFETY: `TakeOnce` is `Send` iff `T` is `Send`.
unsafe impl<T: Send> Send for TakeOnce<T> {}
// SAFETY: `TakeOnce` does not allow shared access to the inner value.
unsafe impl<T: Send> Sync for TakeOnce<T> {}

// #[cfg(all(test, feature = "_shuttle"))]
#[cfg(test)]
mod tests {
    use super::TakeOnce;
    use shuttle::sync::Arc;
    use shuttle::thread;

    const SHUTTLE_ITERS: usize = 10_000;

    #[test]
    fn concurrent_store_operations() {
        shuttle::check_random(
            || {
                let once_take = Arc::new(TakeOnce::new());
                let num_threads = 6;
                let threads: Vec<_> = (0..num_threads)
                    .map(|i| {
                        let once_take = once_take.clone();
                        thread::spawn(move || once_take.store(i))
                    })
                    .collect();

                let results: Vec<_> = threads.into_iter().map(|t| t.join().unwrap()).collect();

                // Exactly one store should succeed
                assert_eq!(results.iter().filter(|r| r.is_ok()).count(), 1);
                // All other stores should return Err
                assert_eq!(
                    results.iter().filter(|r| r.is_err()).count(),
                    num_threads - 1
                );
            },
            SHUTTLE_ITERS,
        );
    }

    #[test]
    fn concurrent_take_operations() {
        shuttle::check_random(
            || {
                let once_take = Arc::new(TakeOnce::new());

                // First store
                assert_eq!(once_take.store(42), Ok(()));

                // Concurrent takes
                let threads: Vec<_> = (0..3)
                    .map(|_| {
                        let once_take = once_take.clone();
                        thread::spawn(move || once_take.take())
                    })
                    .collect();

                let results: Vec<_> = threads.into_iter().map(|t| t.join().unwrap()).collect();

                // Exactly one take should succeed
                assert_eq!(results.iter().filter(|r| r.is_some()).count(), 1);
                // The successful take should return 42
                assert!(results.iter().any(|r| r == &Some(42)));
                // All other takes should return None
                assert_eq!(results.iter().filter(|r| r.is_none()).count(), 2);
            },
            SHUTTLE_ITERS,
        );
    }

    #[test]
    fn mixed_store_take_operations() {
        shuttle::check_random(
            || {
                let once_take = Arc::new(TakeOnce::new());

                // Alternate between store and take
                let num_threads = 6;
                let threads: Vec<_> = (0..num_threads)
                    .map(|i| {
                        let once_take = once_take.clone();
                        thread::spawn(move || {
                            if i % 2 == 0 {
                                once_take.store(i)
                            } else {
                                once_take.take().map_or(Err(i), |_| Ok(()))
                            }
                        })
                    })
                    .collect();

                let results = threads
                    .into_iter()
                    .map(|t| t.join().unwrap())
                    .collect::<Vec<_>>();

                // At least one operation should succeed
                assert!(results.iter().any(|r| r.is_ok()));
            },
            SHUTTLE_ITERS,
        );
    }

    #[test]
    fn completion_status_consistency() {
        shuttle::check_random(
            || {
                let once_take = Arc::new(TakeOnce::new());
                let _once_take2 = once_take.clone();

                assert!(!once_take.is_completed());

                let t1 = thread::spawn(move || {
                    once_take.store(42).unwrap();
                    once_take.is_completed()
                });

                let completed_after_store = t1.join().unwrap();
                assert!(completed_after_store);
            },
            SHUTTLE_ITERS,
        );
    }

    #[test]
    fn store_take_ordering() {
        shuttle::check_random(
            || {
                let once_take = Arc::new(TakeOnce::new());
                let once_take2 = once_take.clone();
                let once_take3 = once_take.clone();

                let t1 = thread::spawn(move || {
                    once_take.store(42).unwrap();
                    // This release store should be visible to the take thread
                    true
                });

                let t2 = thread::spawn(move || {
                    // This acquire load should see the store if completed
                    if once_take2.is_completed() {
                        once_take2.take()
                    } else {
                        None
                    }
                });

                let t3 = thread::spawn(move || {
                    // Should never see a partial state
                    if once_take3.is_completed() {
                        assert!(once_take3.take().is_some() || once_take3.take().is_none());
                    }
                });

                assert!(t1.join().unwrap());
                t2.join().unwrap();
                t3.join().unwrap();
            },
            SHUTTLE_ITERS,
        );
    }

    #[test]
    fn drop_consistency() {
        shuttle::check_random(
            || {
                let once_take = Arc::new(TakeOnce::new());
                let once_take2 = once_take.clone();

                // Store a value that implements Drop
                #[derive(Debug, PartialEq)]
                struct DropTest(i32);
                static DROPPED: shuttle::sync::Once = shuttle::sync::Once::new();
                impl Drop for DropTest {
                    fn drop(&mut self) {
                        let mut called = false;
                        DROPPED.call_once(|| called = true);
                        assert!(called);
                    }
                }

                once_take.store(DropTest(42)).unwrap();

                let t = thread::spawn(move || {
                    // Take the value in another thread
                    once_take2.take()
                });

                // The value should only be dropped once.
                let taken = t.join().unwrap();
                if let Some(val) = taken {
                    assert_eq!(val.0, 42);
                    drop(val);
                }

                assert!(DROPPED.is_completed());
            },
            SHUTTLE_ITERS,
        );
    }
}