Skip to content

Commit 7d90ff0

Browse files
chore: replace once_cell OnceCell/Lazy with std OnceLock/LazyLock
1 parent b859914 commit 7d90ff0

File tree

15 files changed

+42
-38
lines changed

15 files changed

+42
-38
lines changed

Cargo.lock

-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/postgres/axum-social-with-tests/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ validator = { version = "0.16.0", features = ["derive"] }
2424
# Auxilliary crates
2525
anyhow = "1.0.58"
2626
dotenvy = "0.15.1"
27-
once_cell = "1.13.0"
2827
thiserror = "2.0.0"
2928
tracing = "0.1.35"
3029

sqlx-bench/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ sqlite = ["sqlx/sqlite"]
2828
[dependencies]
2929
criterion = "0.5.1"
3030
dotenvy = "0.15.0"
31-
once_cell = "1.4"
3231
sqlx = { workspace = true, default-features = false, features = ["macros"] }
3332

3433
chrono = "0.4.19"

sqlx-core/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ futures-intrusive = "0.5.0"
6565
futures-util = { version = "0.3.19", default-features = false, features = ["alloc", "sink", "io"] }
6666
log = { version = "0.4.18", default-features = false }
6767
memchr = { version = "2.4.1", default-features = false }
68-
once_cell = "1.9.0"
6968
percent-encoding = "2.1.0"
7069
regex = { version = "1.5.5", optional = true }
7170
serde = { version = "1.0.132", features = ["derive", "rc"], optional = true }

sqlx-core/src/any/driver.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use crate::connection::Connection;
55
use crate::database::Database;
66
use crate::Error;
77
use futures_core::future::BoxFuture;
8-
use once_cell::sync::OnceCell;
98
use std::fmt::{Debug, Formatter};
9+
use std::sync::OnceLock;
1010
use url::Url;
1111

12-
static DRIVERS: OnceCell<&'static [AnyDriver]> = OnceCell::new();
12+
static DRIVERS: OnceLock<&'static [AnyDriver]> = OnceLock::new();
1313

1414
#[macro_export]
1515
macro_rules! declare_driver_with_optional_migrate {

sqlx-macros-core/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ dotenvy = { workspace = true }
5858
hex = { version = "0.4.3" }
5959
heck = { version = "0.5" }
6060
either = "1.6.1"
61-
once_cell = "1.9.0"
6261
proc-macro2 = { version = "1.0.79", default-features = false }
6362
serde = { version = "1.0.132", features = ["derive"] }
6463
serde_json = { version = "1.0.73" }

sqlx-macros-core/src/database/mod.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::collections::hash_map;
22
use std::collections::HashMap;
3-
use std::sync::Mutex;
4-
5-
use once_cell::sync::Lazy;
3+
use std::sync::{LazyLock, Mutex};
64

75
use sqlx_core::connection::Connection;
86
use sqlx_core::database::Database;
@@ -30,14 +28,14 @@ pub trait DatabaseExt: Database + TypeChecking {
3028

3129
#[allow(dead_code)]
3230
pub struct CachingDescribeBlocking<DB: DatabaseExt> {
33-
connections: Lazy<Mutex<HashMap<String, DB::Connection>>>,
31+
connections: LazyLock<Mutex<HashMap<String, DB::Connection>>>,
3432
}
3533

3634
#[allow(dead_code)]
3735
impl<DB: DatabaseExt> CachingDescribeBlocking<DB> {
3836
pub const fn new() -> Self {
3937
CachingDescribeBlocking {
40-
connections: Lazy::new(|| Mutex::new(HashMap::new())),
38+
connections: LazyLock::new(|| Mutex::new(HashMap::new())),
4139
}
4240
}
4341

sqlx-macros-core/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@ where
5757
{
5858
#[cfg(feature = "_rt-tokio")]
5959
{
60-
use once_cell::sync::Lazy;
60+
use std::sync::LazyLock;
61+
6162
use tokio::runtime::{self, Runtime};
6263

6364
// We need a single, persistent Tokio runtime since we're caching connections,
6465
// otherwise we'll get "IO driver has terminated" errors.
65-
static TOKIO_RT: Lazy<Runtime> = Lazy::new(|| {
66+
static TOKIO_RT: LazyLock<Runtime> = LazyLock::new(|| {
6667
runtime::Builder::new_current_thread()
6768
.enable_all()
6869
.build()

sqlx-macros-core/src/query/data.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use std::fs;
44
use std::io::Write as _;
55
use std::marker::PhantomData;
66
use std::path::{Path, PathBuf};
7-
use std::sync::Mutex;
7+
use std::sync::{LazyLock, Mutex};
88

9-
use once_cell::sync::Lazy;
109
use serde::{Serialize, Serializer};
1110

1211
use sqlx_core::database::Database;
@@ -65,8 +64,8 @@ impl<DB: Database> Serialize for SerializeDbName<DB> {
6564
}
6665
}
6766

68-
static OFFLINE_DATA_CACHE: Lazy<Mutex<HashMap<PathBuf, DynQueryData>>> =
69-
Lazy::new(Default::default);
67+
static OFFLINE_DATA_CACHE: LazyLock<Mutex<HashMap<PathBuf, DynQueryData>>> =
68+
LazyLock::new(Default::default);
7069

7170
/// Offline query data
7271
#[derive(Clone, serde::Deserialize)]

sqlx-macros-core/src/query/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::path::PathBuf;
2-
use std::sync::{Arc, Mutex};
2+
use std::sync::{Arc, LazyLock, Mutex};
33
use std::{fs, io};
44

5-
use once_cell::sync::Lazy;
65
use proc_macro2::TokenStream;
76
use syn::Type;
87

@@ -108,7 +107,7 @@ impl Metadata {
108107

109108
// If we are in a workspace, lookup `workspace_root` since `CARGO_MANIFEST_DIR` won't
110109
// reflect the workspace dir: https://github.com/rust-lang/cargo/issues/3946
111-
static METADATA: Lazy<Metadata> = Lazy::new(|| {
110+
static METADATA: LazyLock<Metadata> = LazyLock::new(|| {
112111
let manifest_dir: PathBuf = env("CARGO_MANIFEST_DIR")
113112
.expect("`CARGO_MANIFEST_DIR` must be set")
114113
.into();

sqlx-mysql/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ hex = "0.4.3"
6262
itoa = "1.0.1"
6363
log = "0.4.18"
6464
memchr = { version = "2.4.1", default-features = false }
65-
once_cell = "1.9.0"
6665
percent-encoding = "2.1.0"
6766
smallvec = "1.7.0"
6867
stringprep = "0.1.2"

sqlx-mysql/src/testing/mod.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::ops::Deref;
22
use std::str::FromStr;
3+
use std::sync::OnceLock;
34
use std::time::Duration;
45

56
use futures_core::future::BoxFuture;
67

7-
use once_cell::sync::OnceCell;
88
use sqlx_core::connection::Connection;
99
use sqlx_core::query_builder::QueryBuilder;
1010
use sqlx_core::query_scalar::query_scalar;
@@ -18,8 +18,8 @@ use crate::{MySql, MySqlConnectOptions, MySqlConnection};
1818

1919
pub(crate) use sqlx_core::testing::*;
2020

21-
// Using a blocking `OnceCell` here because the critical sections are short.
22-
static MASTER_POOL: OnceCell<Pool<MySql>> = OnceCell::new();
21+
// Using a blocking `OnceLock` here because the critical sections are short.
22+
static MASTER_POOL: OnceLock<Pool<MySql>> = OnceLock::new();
2323

2424
impl TestSupport for MySql {
2525
fn test_context(args: &TestArgs) -> BoxFuture<'_, Result<TestContext<Self>, Error>> {
@@ -119,7 +119,7 @@ async fn test_context(args: &TestArgs) -> Result<TestContext<MySql>, Error> {
119119
.after_release(|_conn, _| Box::pin(async move { Ok(false) }))
120120
.connect_lazy_with(master_opts);
121121

122-
let master_pool = match MASTER_POOL.try_insert(pool) {
122+
let master_pool = match once_lock_try_insert_polyfill(&MASTER_POOL, pool) {
123123
Ok(inserted) => inserted,
124124
Err((existing, pool)) => {
125125
// Sanity checks.
@@ -195,3 +195,12 @@ async fn do_cleanup(conn: &mut MySqlConnection, db_name: &str) -> Result<(), Err
195195

196196
Ok(())
197197
}
198+
199+
fn once_lock_try_insert_polyfill<T>(this: &OnceLock<T>, value: T) -> Result<&T, (&T, T)> {
200+
let mut value = Some(value);
201+
let res = this.get_or_init(|| value.take().unwrap());
202+
match value {
203+
None => Ok(res),
204+
Some(value) => Err((res, value)),
205+
}
206+
}

sqlx-postgres/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ itoa = "1.0.1"
6161
log = "0.4.18"
6262
memchr = { version = "2.4.1", default-features = false }
6363
num-bigint = { version = "0.4.3", optional = true }
64-
once_cell = "1.9.0"
6564
smallvec = { version = "1.7.0", features = ["serde"] }
6665
stringprep = "0.1.2"
6766
thiserror = "2.0.0"

sqlx-postgres/src/advisory_lock.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use crate::error::Result;
22
use crate::Either;
33
use crate::PgConnection;
44
use hkdf::Hkdf;
5-
use once_cell::sync::OnceCell;
65
use sha2::Sha256;
76
use std::ops::{Deref, DerefMut};
7+
use std::sync::OnceLock;
88

99
/// A mutex-like type utilizing [Postgres advisory locks].
1010
///
@@ -37,7 +37,7 @@ use std::ops::{Deref, DerefMut};
3737
pub struct PgAdvisoryLock {
3838
key: PgAdvisoryLockKey,
3939
/// The query to execute to release this lock.
40-
release_query: OnceCell<String>,
40+
release_query: OnceLock<String>,
4141
}
4242

4343
/// A key type natively used by Postgres advisory locks.
@@ -163,7 +163,7 @@ impl PgAdvisoryLock {
163163
pub fn with_key(key: PgAdvisoryLockKey) -> Self {
164164
Self {
165165
key,
166-
release_query: OnceCell::new(),
166+
release_query: OnceLock::new(),
167167
}
168168
}
169169

sqlx-postgres/src/testing/mod.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::fmt::Write;
22
use std::ops::Deref;
33
use std::str::FromStr;
4+
use std::sync::OnceLock;
45
use std::time::Duration;
56

67
use futures_core::future::BoxFuture;
78

8-
use once_cell::sync::OnceCell;
99
use sqlx_core::connection::Connection;
1010
use sqlx_core::query_scalar::query_scalar;
1111

@@ -17,8 +17,8 @@ use crate::{PgConnectOptions, PgConnection, Postgres};
1717

1818
pub(crate) use sqlx_core::testing::*;
1919

20-
// Using a blocking `OnceCell` here because the critical sections are short.
21-
static MASTER_POOL: OnceCell<Pool<Postgres>> = OnceCell::new();
20+
// Using a blocking `OnceLock` here because the critical sections are short.
21+
static MASTER_POOL: OnceLock<Pool<Postgres>> = OnceLock::new();
2222
// Automatically delete any databases created before the start of the test binary.
2323

2424
impl TestSupport for Postgres {
@@ -106,7 +106,7 @@ async fn test_context(args: &TestArgs) -> Result<TestContext<Postgres>, Error> {
106106
.after_release(|_conn, _| Box::pin(async move { Ok(false) }))
107107
.connect_lazy_with(master_opts);
108108

109-
let master_pool = match MASTER_POOL.try_insert(pool) {
109+
let master_pool = match once_lock_try_insert_polyfill(&MASTER_POOL, pool) {
110110
Ok(inserted) => inserted,
111111
Err((existing, pool)) => {
112112
// Sanity checks.
@@ -198,3 +198,12 @@ async fn do_cleanup(conn: &mut PgConnection, db_name: &str) -> Result<(), Error>
198198

199199
Ok(())
200200
}
201+
202+
fn once_lock_try_insert_polyfill<T>(this: &OnceLock<T>, value: T) -> Result<&T, (&T, T)> {
203+
let mut value = Some(value);
204+
let res = this.get_or_init(|| value.take().unwrap());
205+
match value {
206+
None => Ok(res),
207+
Some(value) => Err((res, value)),
208+
}
209+
}

0 commit comments

Comments
 (0)