Only drop OnceLock value if initialized

This commit is contained in:
Luke Parker
2025-08-19 17:50:04 -04:00
parent 1e0240123d
commit da3a85efe5

View File

@@ -48,7 +48,7 @@ mod std_oncelock {
Self { value: Cell::new(core::ptr::null_mut()), init: RwLock::new(false) } Self { value: Cell::new(core::ptr::null_mut()), init: RwLock::new(false) }
} }
/// Shim for `std::sync::OnceLock::get_or_init`. /// Shim for `std::sync::OnceLock::get_or_init`.
pub fn get_or_init<F>(&'a self, f: F) -> &'a T pub fn get_or_init<F>(&self, f: F) -> &T
where where
F: FnOnce() -> T, F: FnOnce() -> T,
{ {
@@ -72,10 +72,12 @@ mod std_oncelock {
// multiple times // multiple times
impl<T> Drop for OnceLock<T> { impl<T> Drop for OnceLock<T> {
fn drop(&mut self) { fn drop(&mut self) {
if *self.init.read().unwrap() {
unsafe { drop(Box::from_raw(self.value.get())) } unsafe { drop(Box::from_raw(self.value.get())) }
} }
} }
} }
}
#[rustversion::before(1.70)] #[rustversion::before(1.70)]
pub use before_1_70_oncelock::OnceLock; pub use before_1_70_oncelock::OnceLock;
#[rustversion::since(1.70)] #[rustversion::since(1.70)]