From 1e0240123df7b47de5c19720b5a9b34fdfa30d20 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Tue, 19 Aug 2025 17:40:19 -0400 Subject: [PATCH] Shim LazyLock when before 1.70 --- common/std-shims/Cargo.toml | 2 +- common/std-shims/src/sync.rs | 118 +++++++++++++++++++++++++-------- crypto/ciphersuite/Cargo.toml | 2 +- crypto/dkg/Cargo.toml | 2 +- crypto/dkg/dealer/Cargo.toml | 2 +- crypto/dkg/recovery/Cargo.toml | 2 +- 6 files changed, 94 insertions(+), 34 deletions(-) diff --git a/common/std-shims/Cargo.toml b/common/std-shims/Cargo.toml index ae56004e..0525f515 100644 --- a/common/std-shims/Cargo.toml +++ b/common/std-shims/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/serai-dex/serai/tree/develop/common/std-shims" authors = ["Luke Parker "] keywords = ["nostd", "no_std", "alloc", "io"] edition = "2021" -rust-version = "1.70" +rust-version = "1.64" [package.metadata.docs.rs] all-features = true diff --git a/common/std-shims/src/sync.rs b/common/std-shims/src/sync.rs index 949bf57e..65313b5c 100644 --- a/common/std-shims/src/sync.rs +++ b/common/std-shims/src/sync.rs @@ -25,41 +25,101 @@ mod mutex_shim { } pub use mutex_shim::{ShimMutex as Mutex, MutexGuard}; -#[cfg(feature = "std")] -pub use std::sync::OnceLock; #[cfg(not(feature = "std"))] pub use spin::Once as OnceLock; +#[cfg(feature = "std")] +mod std_oncelock { + #[rustversion::before(1.70)] + mod before_1_70_oncelock { + use core::cell::Cell; + use std::sync::RwLock; -#[rustversion::before(1.80)] -mod before_1_80_lazylock { - use core::ops::Deref; - use super::{Mutex, OnceLock}; + /// Shim for `std::sync::OnceLock`. + pub struct OnceLock { + value: Cell<*mut T>, + init: RwLock, + } + // We use the `RwLock` (which is `Sync`) to control access to the `!Sync` `RefCell` + unsafe impl Sync for OnceLock {} - /// Shim for `std::sync::LazyLock`. - pub struct LazyLock T> { - f: Mutex>, - once: OnceLock, - } - impl T> LazyLock { - /// Shim for `std::sync::LazyLock::new`. - pub const fn new(f: F) -> Self { - Self { f: Mutex::new(Some(f)), once: OnceLock::new() } + impl OnceLock { + /// Shim for `std::sync::OnceLock::new`. + pub const fn new() -> Self { + Self { value: Cell::new(core::ptr::null_mut()), init: RwLock::new(false) } + } + /// Shim for `std::sync::OnceLock::get_or_init`. + pub fn get_or_init(&'a self, f: F) -> &'a T + where + F: FnOnce() -> T, + { + let initialized = *self.init.read().unwrap(); + if !initialized { + // Obtain an exclusive reference + let mut initialized = self.init.write().unwrap(); + // If this still isn't initialized (by someone who first obtained an exlusive reference) + if !*initialized { + // Set the value and mark it initialized + self.value.set(Box::into_raw(Box::new(f()))); + *initialized = true; + } + } + // SAFETY: We always initialize the value before this and it's only written to once + unsafe { &*self.value.get() } + } } - /// Shim for `std::sync::LazyLock::get_or_init`. - pub fn get(&self) -> &T { - // Since this initializer will only be called once, the value in the Mutex will be `Some` - self.once.get_or_init(|| (self.f.lock().take().unwrap())()) - } - } - impl T> Deref for LazyLock { - type Target = T; - fn deref(&self) -> &T { - self.get() + + // SAFETY: `OnceLock` doesn't implement `Clone` so this doesn't risk dropping the `Box` + // multiple times + impl Drop for OnceLock { + fn drop(&mut self) { + unsafe { drop(Box::from_raw(self.value.get())) } + } } } + #[rustversion::before(1.70)] + pub use before_1_70_oncelock::OnceLock; + #[rustversion::since(1.70)] + pub use std::sync::OnceLock; } -#[rustversion::before(1.80)] -pub use before_1_80_lazylock::LazyLock; +#[cfg(feature = "std")] +pub use std_oncelock::OnceLock; -#[rustversion::since(1.80)] -pub use std::sync::LazyLock; +#[cfg(not(feature = "std"))] +pub use spin::Lazy as LazyLock; +#[cfg(feature = "std")] +mod std_lazylock { + #[rustversion::before(1.80)] + mod before_1_80_lazylock { + use core::ops::Deref; + use crate::sync::{Mutex, OnceLock}; + + /// Shim for `std::sync::LazyLock`. + pub struct LazyLock T> { + f: Mutex>, + once: OnceLock, + } + impl T> LazyLock { + /// Shim for `std::sync::LazyLock::new`. + pub const fn new(f: F) -> Self { + Self { f: Mutex::new(Some(f)), once: OnceLock::new() } + } + /// Shim for `std::sync::LazyLock::get`. + pub fn get(&self) -> &T { + // Since this initializer will only be called once, the value in the Mutex will be `Some` + self.once.get_or_init(|| (self.f.lock().take().unwrap())()) + } + } + impl T> Deref for LazyLock { + type Target = T; + fn deref(&self) -> &T { + self.get() + } + } + } + #[rustversion::before(1.80)] + pub use before_1_80_lazylock::LazyLock; + #[rustversion::since(1.80)] + pub use std::sync::LazyLock; +} +#[cfg(feature = "std")] +pub use std_lazylock::LazyLock; diff --git a/crypto/ciphersuite/Cargo.toml b/crypto/ciphersuite/Cargo.toml index 5fe4550c..9fcf60a6 100644 --- a/crypto/ciphersuite/Cargo.toml +++ b/crypto/ciphersuite/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/serai-dex/serai/tree/develop/crypto/ciphersuite authors = ["Luke Parker "] keywords = ["ciphersuite", "ff", "group"] edition = "2021" -rust-version = "1.79" +rust-version = "1.74" [package.metadata.docs.rs] all-features = true diff --git a/crypto/dkg/Cargo.toml b/crypto/dkg/Cargo.toml index 57baaebb..d3529b7f 100644 --- a/crypto/dkg/Cargo.toml +++ b/crypto/dkg/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/serai-dex/serai/tree/develop/crypto/dkg" authors = ["Luke Parker "] keywords = ["dkg", "multisig", "threshold", "ff", "group"] edition = "2021" -rust-version = "1.79" +rust-version = "1.74" [package.metadata.docs.rs] all-features = true diff --git a/crypto/dkg/dealer/Cargo.toml b/crypto/dkg/dealer/Cargo.toml index ee008ab9..2790e7d6 100644 --- a/crypto/dkg/dealer/Cargo.toml +++ b/crypto/dkg/dealer/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/serai-dex/serai/tree/develop/crypto/dkg/dealer" authors = ["Luke Parker "] keywords = ["dkg", "multisig", "threshold", "ff", "group"] edition = "2021" -rust-version = "1.79" +rust-version = "1.74" [package.metadata.docs.rs] all-features = true diff --git a/crypto/dkg/recovery/Cargo.toml b/crypto/dkg/recovery/Cargo.toml index 17d7b0c3..68aefc4c 100644 --- a/crypto/dkg/recovery/Cargo.toml +++ b/crypto/dkg/recovery/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/serai-dex/serai/tree/develop/crypto/dkg/recover authors = ["Luke Parker "] keywords = ["dkg", "multisig", "threshold", "ff", "group"] edition = "2021" -rust-version = "1.80" +rust-version = "1.74" [package.metadata.docs.rs] all-features = true