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
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

#![no_std]

use core::cell::UnsafeCell;
use core::sync::atomic::{AtomicBool, Ordering};

// NOTE(rustc_wishlist) use SyncUnsafeCell once #![feature(sync_unsafe_cell)] stabilizes
pub struct ImmediateSyncOnceCell<T> {
    init_started: AtomicBool,
    init_completed: AtomicBool,
    inner: UnsafeCell<Option<T>>,
}

unsafe impl<T> Sync for ImmediateSyncOnceCell<T> {}

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

impl<T> ImmediateSyncOnceCell<T> {
    pub const fn new() -> Self {
        Self {
            init_started: AtomicBool::new(false),
            init_completed: AtomicBool::new(false),
            inner: UnsafeCell::new(None),
        }
    }

    pub fn get(&self) -> Option<&T> {
        if self.init_completed.load(Ordering::Acquire) {
            Some(unsafe { self.inner.get().as_ref().unwrap().as_ref().unwrap() })
        } else {
            None
        }
    }

    pub fn set(&self, value: T) -> Result<(), T> {
        if self.init_started.swap(true, Ordering::SeqCst) {
            Err(value)
        } else {
            unsafe {
                *self.inner.get().as_mut().unwrap() = Some(value);
            }
            self.init_completed.store(true, Ordering::SeqCst);
            Ok(())
        }
    }
}