-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add a safe OnceLock data structure
Taken from rust, very minor modifications on my side.
- Loading branch information
Showing
5 changed files
with
128 additions
and
39 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Taken from https://doc.rust-lang.org/nightly/src/std/sync/once_lock.rs.html#121-128 | ||
// Not mine! | ||
use spin::Once; | ||
|
||
use core::cell::UnsafeCell; | ||
use core::fmt; | ||
use core::mem::MaybeUninit; | ||
|
||
pub struct OnceLock<T> { | ||
once: Once, | ||
value: UnsafeCell<MaybeUninit<T>>, | ||
} | ||
|
||
impl<T> OnceLock<T> { | ||
pub const fn new() -> OnceLock<T> { | ||
OnceLock { | ||
once: Once::new(), | ||
value: UnsafeCell::new(MaybeUninit::uninit()), | ||
} | ||
} | ||
|
||
pub fn get(&self) -> Option<&T> { | ||
if self.is_initialized() { | ||
Some(unsafe { self.get_unchecked() }) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub fn set(&self, value: T) -> Result<(), T> { | ||
match self.try_insert(value) { | ||
Ok(_) => Ok(()), | ||
Err((_, value)) => Err(value), | ||
} | ||
} | ||
|
||
fn try_insert(&self, value: T) -> Result<&T, (&T, T)> { | ||
let mut value = Some(value); | ||
let res = self.get_or_init(|| value.take().unwrap()); | ||
match value { | ||
None => Ok(res), | ||
Some(value) => Err((res, value)), | ||
} | ||
} | ||
|
||
pub fn get_or_init<F>(&self, f: F) -> &T | ||
where | ||
F: FnOnce() -> T, | ||
{ | ||
match self.get_or_try_init(|| Ok::<T, !>(f())) { | ||
Ok(val) => val, | ||
Err(_) => unreachable!(), // never type above | ||
} | ||
} | ||
|
||
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E> | ||
where | ||
F: FnOnce() -> Result<T, E>, | ||
{ | ||
// Fast path check | ||
// NOTE: We need to perform an acquire on the state in this method | ||
// in order to correctly synchronize `LazyLock::force`. This is | ||
// currently done by calling `self.get()`, which in turn calls | ||
// `self.is_initialized()`, which in turn performs the acquire. | ||
if let Some(value) = self.get() { | ||
return Ok(value); | ||
} | ||
self.initialize(f)?; | ||
|
||
debug_assert!(self.is_initialized()); | ||
|
||
// SAFETY: The inner value has been initialized | ||
Ok(unsafe { self.get_unchecked() }) | ||
} | ||
|
||
fn is_initialized(&self) -> bool { | ||
self.once.is_completed() | ||
} | ||
|
||
fn initialize<F, E>(&self, f: F) -> Result<(), E> | ||
where | ||
F: FnOnce() -> Result<T, E>, | ||
{ | ||
let mut res: Result<(), E> = Ok(()); | ||
let slot = &self.value; | ||
|
||
self.once.call_once(|| match f() { | ||
Ok(value) => { | ||
unsafe { (*slot.get()).write(value) }; | ||
} | ||
Err(e) => { | ||
res = Err(e); | ||
} | ||
}); | ||
res | ||
} | ||
|
||
unsafe fn get_unchecked(&self) -> &T { | ||
debug_assert!(self.is_initialized()); | ||
(*self.value.get()).assume_init_ref() | ||
} | ||
} | ||
|
||
impl<T: fmt::Debug> fmt::Debug for OnceLock<T> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let mut d = f.debug_tuple("OnceLock"); | ||
match self.get() { | ||
Some(v) => d.field(v), | ||
None => d.field(&format_args!("<uninit>")), | ||
}; | ||
d.finish() | ||
} | ||
} | ||
|
||
unsafe impl<T: Sync + Send> Sync for OnceLock<T> {} |