Skip to content

Commit f15c633

Browse files
committed
feat: add helpers for OnceBox
1 parent d119eea commit f15c633

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/race.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ mod once_box {
332332
OnceBox { inner: AtomicPtr::new(ptr::null_mut()), ghost: PhantomData }
333333
}
334334

335+
/// Creates a new cell with the given value.
336+
pub fn with_value(value: Box<T>) -> Self {
337+
OnceBox { inner: AtomicPtr::new(Box::into_raw(value)), ghost: PhantomData }
338+
}
339+
335340
/// Gets a reference to the underlying value.
336341
pub fn get(&self) -> Option<&T> {
337342
let ptr = self.inner.load(Ordering::Acquire);
@@ -410,6 +415,15 @@ mod once_box {
410415

411416
unsafe impl<T: Sync + Send> Sync for OnceBox<T> {}
412417

418+
impl<T: Clone> Clone for OnceBox<T> {
419+
fn clone(&self) -> Self {
420+
match self.get() {
421+
Some(value) => OnceBox::with_value(Box::new(value.clone())),
422+
None => OnceBox::new(),
423+
}
424+
}
425+
}
426+
413427
/// ```compile_fail
414428
/// struct S(*mut ());
415429
/// unsafe impl Sync for S {}

0 commit comments

Comments
 (0)