mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 04:09:23 +00:00
Shim LazyLock when before 1.70
This commit is contained in:
@@ -7,7 +7,7 @@ repository = "https://github.com/serai-dex/serai/tree/develop/common/std-shims"
|
|||||||
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
||||||
keywords = ["nostd", "no_std", "alloc", "io"]
|
keywords = ["nostd", "no_std", "alloc", "io"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.70"
|
rust-version = "1.64"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
all-features = true
|
all-features = true
|
||||||
|
|||||||
@@ -25,41 +25,101 @@ mod mutex_shim {
|
|||||||
}
|
}
|
||||||
pub use mutex_shim::{ShimMutex as Mutex, MutexGuard};
|
pub use mutex_shim::{ShimMutex as Mutex, MutexGuard};
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::sync::OnceLock;
|
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
pub use spin::Once as OnceLock;
|
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)]
|
/// Shim for `std::sync::OnceLock`.
|
||||||
mod before_1_80_lazylock {
|
pub struct OnceLock<T> {
|
||||||
use core::ops::Deref;
|
value: Cell<*mut T>,
|
||||||
use super::{Mutex, OnceLock};
|
init: RwLock<bool>,
|
||||||
|
}
|
||||||
|
// We use the `RwLock` (which is `Sync`) to control access to the `!Sync` `RefCell`
|
||||||
|
unsafe impl<T: Sync> Sync for OnceLock<T> {}
|
||||||
|
|
||||||
/// Shim for `std::sync::LazyLock`.
|
impl<T> OnceLock<T> {
|
||||||
pub struct LazyLock<T, F = fn() -> T> {
|
/// Shim for `std::sync::OnceLock::new`.
|
||||||
f: Mutex<Option<F>>,
|
pub const fn new() -> Self {
|
||||||
once: OnceLock<T>,
|
Self { value: Cell::new(core::ptr::null_mut()), init: RwLock::new(false) }
|
||||||
}
|
}
|
||||||
impl<T, F: FnOnce() -> T> LazyLock<T, F> {
|
/// Shim for `std::sync::OnceLock::get_or_init`.
|
||||||
/// Shim for `std::sync::LazyLock::new`.
|
pub fn get_or_init<F>(&'a self, f: F) -> &'a T
|
||||||
pub const fn new(f: F) -> Self {
|
where
|
||||||
Self { f: Mutex::new(Some(f)), once: OnceLock::new() }
|
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 {
|
// SAFETY: `OnceLock` doesn't implement `Clone` so this doesn't risk dropping the `Box`
|
||||||
// Since this initializer will only be called once, the value in the Mutex will be `Some`
|
// multiple times
|
||||||
self.once.get_or_init(|| (self.f.lock().take().unwrap())())
|
impl<T> Drop for OnceLock<T> {
|
||||||
}
|
fn drop(&mut self) {
|
||||||
}
|
unsafe { drop(Box::from_raw(self.value.get())) }
|
||||||
impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {
|
}
|
||||||
type Target = T;
|
|
||||||
fn deref(&self) -> &T {
|
|
||||||
self.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)]
|
#[cfg(feature = "std")]
|
||||||
pub use before_1_80_lazylock::LazyLock;
|
pub use std_oncelock::OnceLock;
|
||||||
|
|
||||||
#[rustversion::since(1.80)]
|
#[cfg(not(feature = "std"))]
|
||||||
pub use std::sync::LazyLock;
|
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 = fn() -> T> {
|
||||||
|
f: Mutex<Option<F>>,
|
||||||
|
once: OnceLock<T>,
|
||||||
|
}
|
||||||
|
impl<T, F: FnOnce() -> T> LazyLock<T, F> {
|
||||||
|
/// 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, F: FnOnce() -> T> Deref for LazyLock<T, F> {
|
||||||
|
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;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ repository = "https://github.com/serai-dex/serai/tree/develop/crypto/ciphersuite
|
|||||||
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
||||||
keywords = ["ciphersuite", "ff", "group"]
|
keywords = ["ciphersuite", "ff", "group"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.79"
|
rust-version = "1.74"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
all-features = true
|
all-features = true
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ repository = "https://github.com/serai-dex/serai/tree/develop/crypto/dkg"
|
|||||||
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
||||||
keywords = ["dkg", "multisig", "threshold", "ff", "group"]
|
keywords = ["dkg", "multisig", "threshold", "ff", "group"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.79"
|
rust-version = "1.74"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
all-features = true
|
all-features = true
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ repository = "https://github.com/serai-dex/serai/tree/develop/crypto/dkg/dealer"
|
|||||||
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
||||||
keywords = ["dkg", "multisig", "threshold", "ff", "group"]
|
keywords = ["dkg", "multisig", "threshold", "ff", "group"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.79"
|
rust-version = "1.74"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
all-features = true
|
all-features = true
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ repository = "https://github.com/serai-dex/serai/tree/develop/crypto/dkg/recover
|
|||||||
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
||||||
keywords = ["dkg", "multisig", "threshold", "ff", "group"]
|
keywords = ["dkg", "multisig", "threshold", "ff", "group"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.80"
|
rust-version = "1.74"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
all-features = true
|
all-features = true
|
||||||
|
|||||||
Reference in New Issue
Block a user