-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix cyclic compilation: Use vendored once_cell (#1154)
- Loading branch information
Showing
5 changed files
with
64 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub(crate) mod async_executor; | ||
pub(crate) mod job_token; | ||
pub(crate) mod once_lock; | ||
pub(crate) mod stderr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::{ | ||
cell::UnsafeCell, | ||
marker::PhantomData, | ||
mem::MaybeUninit, | ||
panic::{RefUnwindSafe, UnwindSafe}, | ||
sync::Once, | ||
}; | ||
|
||
pub(crate) struct OnceLock<T> { | ||
once: Once, | ||
value: UnsafeCell<MaybeUninit<T>>, | ||
_marker: PhantomData<T>, | ||
} | ||
|
||
impl<T> OnceLock<T> { | ||
pub(crate) const fn new() -> Self { | ||
Self { | ||
once: Once::new(), | ||
value: UnsafeCell::new(MaybeUninit::uninit()), | ||
_marker: PhantomData, | ||
} | ||
} | ||
|
||
pub(crate) fn get_or_init(&self, f: impl FnOnce() -> T) -> &T { | ||
self.once.call_once(|| { | ||
unsafe { &mut *self.value.get() }.write(f()); | ||
}); | ||
unsafe { (&*self.value.get()).assume_init_ref() } | ||
} | ||
} | ||
|
||
unsafe impl<T: Sync + Send> Sync for OnceLock<T> {} | ||
unsafe impl<T: Send> Send for OnceLock<T> {} | ||
|
||
impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for OnceLock<T> {} | ||
impl<T: UnwindSafe> UnwindSafe for OnceLock<T> {} | ||
|
||
impl<T> Drop for OnceLock<T> { | ||
#[inline] | ||
fn drop(&mut self) { | ||
if self.once.is_completed() { | ||
// SAFETY: The cell is initialized and being dropped, so it can't | ||
// be accessed again. | ||
unsafe { self.value.get_mut().assume_init_drop() }; | ||
} | ||
} | ||
} |