You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To quote the tracking issue (I believe it describes the motivating use case as well as the alternatives)
The Once synchronization primitive provides a poison method for marking the internal OnceState as poisoned, which is combined with the call_once_force method in OnceLock::initialize to call initialization functions until one succeeds without panicking. This provides a way for users to use fallible initialization functions where they only observe the OnceLock being initialized once a function doesn't panic:
fninitialize<F,E>(&self,f:F) -> Result<(),E>whereF:FnOnce() -> Result<T,E>,{letmut res:Result<(),E> = Ok(());let slot = &self.value;// Ignore poisoning from other threads// If another thread panics, then we'll be able to run our closureself.once.call_once_force(|p| {matchf(){Ok(value) => {unsafe{(&mut*slot.get()).write(value)};}Err(e) => {
res = Err(e);// Treat the underlying `Once` as poisoned since we// failed to initialize our value.
p.poison();}}});
res
}
This tracking issue is for marking the poison method on OnceState as pub, rather than pub(crate). This has no impact on the OnceCell or OnceLock APIs, but allows downstream libraries to build out similar functionality. The motivation for this is for the twice-cell crate, where use of this API would simplify the implementation.
Public API
// std::sync::oncepubstructOnceState{pub(crate)inner: sys::OnceState,}implOnceState{/// Poison the associated [`Once`] without explicitly panicking.#[inline]pubfnpoison(&self){self.inner.poison();}}
Proposal
To quote the tracking issue (I believe it describes the motivating use case as well as the alternatives)
Links and related work
OnceState::poison
public rust#130327OnceState::poison
aspub
rust#133240The text was updated successfully, but these errors were encountered: