chore: replace once_cell OnceCell/Lazy with std OnceLock/LazyLock (#3709)

This commit is contained in:
Paolo Barbolini
2025-06-18 01:38:02 +02:00
committed by GitHub
parent df47ffedd2
commit 764ae2f702
15 changed files with 44 additions and 41 deletions

View File

@@ -2,9 +2,9 @@ use crate::error::Result;
use crate::Either;
use crate::PgConnection;
use hkdf::Hkdf;
use once_cell::sync::OnceCell;
use sha2::Sha256;
use std::ops::{Deref, DerefMut};
use std::sync::OnceLock;
/// A mutex-like type utilizing [Postgres advisory locks].
///
@@ -37,7 +37,7 @@ use std::ops::{Deref, DerefMut};
pub struct PgAdvisoryLock {
key: PgAdvisoryLockKey,
/// The query to execute to release this lock.
release_query: OnceCell<String>,
release_query: OnceLock<String>,
}
/// A key type natively used by Postgres advisory locks.
@@ -163,7 +163,7 @@ impl PgAdvisoryLock {
pub fn with_key(key: PgAdvisoryLockKey) -> Self {
Self {
key,
release_query: OnceCell::new(),
release_query: OnceLock::new(),
}
}

View File

@@ -1,11 +1,11 @@
use std::fmt::Write;
use std::ops::Deref;
use std::str::FromStr;
use std::sync::OnceLock;
use std::time::Duration;
use futures_core::future::BoxFuture;
use once_cell::sync::OnceCell;
use sqlx_core::connection::Connection;
use sqlx_core::query_scalar::query_scalar;
@@ -17,8 +17,8 @@ use crate::{PgConnectOptions, PgConnection, Postgres};
pub(crate) use sqlx_core::testing::*;
// Using a blocking `OnceCell` here because the critical sections are short.
static MASTER_POOL: OnceCell<Pool<Postgres>> = OnceCell::new();
// Using a blocking `OnceLock` here because the critical sections are short.
static MASTER_POOL: OnceLock<Pool<Postgres>> = OnceLock::new();
// Automatically delete any databases created before the start of the test binary.
impl TestSupport for Postgres {
@@ -106,7 +106,7 @@ async fn test_context(args: &TestArgs) -> Result<TestContext<Postgres>, Error> {
.after_release(|_conn, _| Box::pin(async move { Ok(false) }))
.connect_lazy_with(master_opts);
let master_pool = match MASTER_POOL.try_insert(pool) {
let master_pool = match once_lock_try_insert_polyfill(&MASTER_POOL, pool) {
Ok(inserted) => inserted,
Err((existing, pool)) => {
// Sanity checks.
@@ -199,3 +199,12 @@ async fn do_cleanup(conn: &mut PgConnection, db_name: &str) -> Result<(), Error>
Ok(())
}
fn once_lock_try_insert_polyfill<T>(this: &OnceLock<T>, value: T) -> Result<&T, (&T, T)> {
let mut value = Some(value);
let res = this.get_or_init(|| value.take().unwrap());
match value {
None => Ok(res),
Some(value) => Err((res, value)),
}
}