Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions crates/precompile/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ static CRYPTO: OnceLock<Box<dyn Crypto>> = OnceLock::new();

/// Install a custom crypto provider globally.
pub fn install_crypto<C: Crypto + 'static>(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.
Expand Down
23 changes: 23 additions & 0 deletions crates/primitives/src/once_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<T>>,
{
self.inner.set(value.into())
}
}
}

Expand All @@ -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<T> {
/// 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<T> OnceLockExt<T> for OnceLock<T> {
#[inline]
fn set(&self, value: T) -> Result<(), T> {
std::sync::OnceLock::set(self, value)
}
}
Loading