diff --git a/crates/precompile/src/interface.rs b/crates/precompile/src/interface.rs index f4651c7a63..f05cd83bf4 100644 --- a/crates/precompile/src/interface.rs +++ b/crates/precompile/src/interface.rs @@ -11,12 +11,7 @@ static CRYPTO: OnceLock> = OnceLock::new(); /// Install a custom crypto provider globally. pub fn install_crypto(crypto: C) -> bool { - if CRYPTO.get().is_some() { - return false; - } - - CRYPTO.get_or_init(|| Box::new(crypto)); - true + CRYPTO.set(Box::new(crypto)).is_ok() } /// Get the installed crypto provider, or the default if none is installed. diff --git a/crates/primitives/src/once_lock.rs b/crates/primitives/src/once_lock.rs index 90d129ea6a..c86091ccd4 100644 --- a/crates/primitives/src/once_lock.rs +++ b/crates/primitives/src/once_lock.rs @@ -41,6 +41,15 @@ mod no_std_impl { pub fn get(&self) -> Option<&T> { self.inner.get() } + + /// Sets the value of the OnceLock, returning Err with the value if it was already set. + #[inline] + pub fn set(&self, value: T) -> Result<(), T> + where + T: Into>, + { + self.inner.set(value.into()) + } } } @@ -51,3 +60,17 @@ pub use std::sync::OnceLock; #[cfg(not(feature = "std"))] pub use no_std_impl::OnceLock; + +#[cfg(feature = "std")] +pub trait OnceLockExt { + /// Sets the value of the OnceLock, returning Err with the value if it was already set. + fn set(&self, value: T) -> Result<(), T>; +} + +#[cfg(feature = "std")] +impl OnceLockExt for OnceLock { + #[inline] + fn set(&self, value: T) -> Result<(), T> { + std::sync::OnceLock::set(self, value) + } +}