diff --git a/Cargo.lock b/Cargo.lock index 98f41495..85dc0610 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9695,6 +9695,7 @@ name = "std-shims" version = "0.1.2" dependencies = [ "hashbrown 0.14.5", + "rustversion", "spin 0.10.0", ] diff --git a/common/std-shims/Cargo.toml b/common/std-shims/Cargo.toml index 5900ca6a..ae56004e 100644 --- a/common/std-shims/Cargo.toml +++ b/common/std-shims/Cargo.toml @@ -1,13 +1,13 @@ [package] name = "std-shims" -version = "0.1.2" +version = "0.1.3" description = "A series of std shims to make alloc more feasible" license = "MIT" 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.80" +rust-version = "1.70" [package.metadata.docs.rs] all-features = true @@ -17,6 +17,7 @@ rustdoc-args = ["--cfg", "docsrs"] workspace = true [dependencies] +rustversion = { version = "1", default-features = false } spin = { version = "0.10", default-features = false, features = ["use_ticket_mutex", "once", "lazy"] } hashbrown = { version = "0.14", default-features = false, features = ["ahash", "inline-more"] } diff --git a/common/std-shims/src/sync.rs b/common/std-shims/src/sync.rs index 14d32721..949bf57e 100644 --- a/common/std-shims/src/sync.rs +++ b/common/std-shims/src/sync.rs @@ -26,6 +26,40 @@ mod mutex_shim { pub use mutex_shim::{ShimMutex as Mutex, MutexGuard}; #[cfg(feature = "std")] -pub use std::sync::{OnceLock, LazyLock}; +pub use std::sync::OnceLock; #[cfg(not(feature = "std"))] -pub use spin::{Once as OnceLock, Lazy as LazyLock}; +pub use spin::Once as OnceLock; + +#[rustversion::before(1.80)] +mod before_1_80_lazylock { + use core::ops::Deref; + use super::{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_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() + } + } +} +#[rustversion::before(1.80)] +pub use before_1_80_lazylock::LazyLock; + +#[rustversion::since(1.80)] +pub use std::sync::LazyLock; diff --git a/crypto/ciphersuite/Cargo.toml b/crypto/ciphersuite/Cargo.toml index b666dbaa..5fe4550c 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.80" +rust-version = "1.79" [package.metadata.docs.rs] all-features = true diff --git a/crypto/dkg/Cargo.toml b/crypto/dkg/Cargo.toml index 1ac689b8..57baaebb 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.80" +rust-version = "1.79" [package.metadata.docs.rs] all-features = true diff --git a/crypto/dkg/dealer/Cargo.toml b/crypto/dkg/dealer/Cargo.toml index 9bc2d5d5..ee008ab9 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.80" +rust-version = "1.79" [package.metadata.docs.rs] all-features = true diff --git a/crypto/dkg/musig/Cargo.toml b/crypto/dkg/musig/Cargo.toml index 42e508a1..1dfde36b 100644 --- a/crypto/dkg/musig/Cargo.toml +++ b/crypto/dkg/musig/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/serai-dex/serai/tree/develop/crypto/dkg/musig" authors = ["Luke Parker "] keywords = ["dkg", "multisig", "threshold", "ff", "group"] edition = "2021" -rust-version = "1.80" +rust-version = "1.79" [package.metadata.docs.rs] all-features = true diff --git a/crypto/multiexp/Cargo.toml b/crypto/multiexp/Cargo.toml index 36efbfe2..228b85ab 100644 --- a/crypto/multiexp/Cargo.toml +++ b/crypto/multiexp/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/serai-dex/serai/tree/develop/crypto/multiexp" authors = ["Luke Parker "] keywords = ["multiexp", "ff", "group"] edition = "2021" -rust-version = "1.80" +rust-version = "1.79" [package.metadata.docs.rs] all-features = true diff --git a/crypto/schnorr/Cargo.toml b/crypto/schnorr/Cargo.toml index 06a9710e..2ea04f5b 100644 --- a/crypto/schnorr/Cargo.toml +++ b/crypto/schnorr/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/serai-dex/serai/tree/develop/crypto/schnorr" authors = ["Luke Parker "] keywords = ["schnorr", "ff", "group"] edition = "2021" -rust-version = "1.80" +rust-version = "1.79" [package.metadata.docs.rs] all-features = true