| 1 | // Based on unstable std::sync::OnceLock. |
| 2 | // |
| 3 | // Source: https://github.com/rust-lang/rust/blob/8e9c93df464b7ada3fc7a1c8ccddd9dcb24ee0a0/library/std/src/sync/once_lock.rs |
| 4 | |
| 5 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::UnsafeCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/UnsafeCell#; |
| 6 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#; |
| 7 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Oncerust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once/Once#; |
| 8 | |
| 9 | pub(craterust-analyzer cargo crossbeam-epoch 0.9.18 crate/) struct OnceLockrust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#<T> { |
| 10 | oncerust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#once.: Oncerust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once/Once#, |
| 11 | valuerust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#value.: UnsafeCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/UnsafeCell#<MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>>, |
| 12 | // Unlike std::sync::OnceLock, we don't need PhantomData here because |
| 13 | // we don't use #[may_dangle]. |
| 14 | } |
| 15 | |
| 16 | unsafe impl<T: Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#> Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# for OnceLockrust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#<T> {} |
| 17 | unsafe impl<T: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#> Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# for OnceLockrust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#<T> {} |
| 18 | |
| 19 | impl<T> OnceLockrust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#<T> { |
| 20 | /// Creates a new empty cell. |
| 21 | #[must_use] |
| 22 | pub(craterust-analyzer cargo crossbeam-epoch 0.9.18 crate/) const fn newrust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/impl#[`OnceLock<T>`]new().() -> Selfrust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/impl#[`OnceLock<T>`] { |
| 23 | Selfrust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/impl#[`OnceLock<T>`] { |
| 24 | oncerust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#once.: Oncerust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once/Once#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once/impl#[Once]new().(), |
| 25 | valuerust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#value.: UnsafeCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/UnsafeCell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`UnsafeCell<T>`]new().(MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#::uninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]uninit().()), |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /// Gets the contents of the cell, initializing it with `f` if the cell |
| 30 | /// was empty. |
| 31 | /// |
| 32 | /// Many threads may call `get_or_init` concurrently with different |
| 33 | /// initializing functions, but it is guaranteed that only one function |
| 34 | /// will be executed. |
| 35 | /// |
| 36 | /// # Panics |
| 37 | /// |
| 38 | /// If `f` panics, the panic is propagated to the caller, and the cell |
| 39 | /// remains uninitialized. |
| 40 | /// |
| 41 | /// It is an error to reentrantly initialize the cell from `f`. The |
| 42 | /// exact outcome is unspecified. Current implementation deadlocks, but |
| 43 | /// this may be changed to a panic in the future. |
| 44 | pub(craterust-analyzer cargo crossbeam-epoch 0.9.18 crate/) fn get_or_initrust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/impl#[`OnceLock<T>`]get_or_init().<F>(&self, f: F) -> &T |
| 45 | where |
| 46 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T, |
| 47 | { |
| 48 | // Fast path check |
| 49 | if self.oncerust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#once..is_completedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once/impl#[Once]is_completed().() { |
| 50 | // SAFETY: The inner value has been initialized |
| 51 | return unsafe { self.get_uncheckedrust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/impl#[`OnceLock<T>`]get_unchecked().() }; |
| 52 | } |
| 53 | self.initializerust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/impl#[`OnceLock<T>`]initialize().(f); |
| 54 | |
| 55 | // SAFETY: The inner value has been initialized |
| 56 | unsafe { self.get_uncheckedrust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/impl#[`OnceLock<T>`]get_unchecked().() } |
| 57 | } |
| 58 | |
| 59 | #[cold] |
| 60 | fn initializerust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/impl#[`OnceLock<T>`]initialize().<F>(&self, f: F) |
| 61 | where |
| 62 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T, |
| 63 | { |
| 64 | let slot = self.valuerust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#value..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`UnsafeCell<T>`]get().(); |
| 65 | |
| 66 | self.oncerust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#once..call_oncerust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once/impl#[Once]call_once().(|| { |
| 67 | let value = f(); |
| 68 | unsafe { slot.writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]write().(MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]new().(value)) } |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | /// # Safety |
| 73 | /// |
| 74 | /// The value must be initialized |
| 75 | unsafe fn get_uncheckedrust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/impl#[`OnceLock<T>`]get_unchecked().(&self) -> &T { |
| 76 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(self.oncerust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#once..is_completedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once/impl#[Once]is_completed().()); |
| 77 | &*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().self.valuerust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#value..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`UnsafeCell<T>`]get().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<T>() |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | impl<T> Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for OnceLockrust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#<T> { |
| 82 | fn droprust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/impl#[`OnceLock<T>`][Drop]drop().(&mut self) { |
| 83 | if self.oncerust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#once..is_completedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once/impl#[Once]is_completed().() { |
| 84 | // SAFETY: The inner value has been initialized |
| 85 | unsafe { (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().self.valuerust-analyzer cargo crossbeam-epoch 0.9.18 sync/once_lock/OnceLock#value..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`UnsafeCell<T>`]get().()).assume_init_droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]assume_init_drop().() }; |
| 86 | } |
| 87 | } |
| 88 | } |