From 5a6568adf4b81dfd44e7366cf0dce5b65f347b77 Mon Sep 17 00:00:00 2001 From: tottoto Date: Wed, 18 Jun 2025 06:48:39 +0900 Subject: [PATCH] chore: replace once_lock with standard library --- Cargo.lock | 5 ----- examples/postgres/axum-social-with-tests/Cargo.toml | 1 - examples/postgres/axum-social-with-tests/src/http/user.rs | 4 ++-- sqlx-core/Cargo.toml | 1 - sqlx-core/src/any/driver.rs | 2 +- sqlx-macros-core/Cargo.toml | 1 - sqlx-macros-core/src/database/mod.rs | 7 +++---- sqlx-macros-core/src/lib.rs | 4 ++-- sqlx-macros-core/src/query/data.rs | 6 +++--- sqlx-macros-core/src/query/mod.rs | 6 +++--- sqlx-mysql/Cargo.toml | 1 - sqlx-mysql/src/testing/mod.rs | 4 ++-- sqlx-postgres/Cargo.toml | 1 - sqlx-postgres/src/advisory_lock.rs | 2 +- sqlx-postgres/src/testing/mod.rs | 4 ++-- 15 files changed, 19 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dae145216a..4b490a4a4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3473,7 +3473,6 @@ dependencies = [ "mac_address", "memchr", "native-tls", - "once_cell", "percent-encoding", "regex", "rust_decimal", @@ -3512,7 +3511,6 @@ dependencies = [ "argon2", "axum", "dotenvy", - "once_cell", "rand", "regex", "serde", @@ -3633,7 +3631,6 @@ dependencies = [ "either", "heck 0.5.0", "hex", - "once_cell", "proc-macro2", "quote", "serde", @@ -3675,7 +3672,6 @@ dependencies = [ "log", "md-5", "memchr", - "once_cell", "percent-encoding", "rand", "rsa", @@ -3723,7 +3719,6 @@ dependencies = [ "md-5", "memchr", "num-bigint", - "once_cell", "rand", "rust_decimal", "serde", diff --git a/examples/postgres/axum-social-with-tests/Cargo.toml b/examples/postgres/axum-social-with-tests/Cargo.toml index 6cb3b73ce5..05257a617c 100644 --- a/examples/postgres/axum-social-with-tests/Cargo.toml +++ b/examples/postgres/axum-social-with-tests/Cargo.toml @@ -24,7 +24,6 @@ validator = { version = "0.16.0", features = ["derive"] } # Auxilliary crates anyhow = "1.0.58" dotenvy = "0.15.1" -once_cell = "1.13.0" thiserror = "2.0.0" tracing = "0.1.35" diff --git a/examples/postgres/axum-social-with-tests/src/http/user.rs b/examples/postgres/axum-social-with-tests/src/http/user.rs index 55f7f05bab..e796449fd6 100644 --- a/examples/postgres/axum-social-with-tests/src/http/user.rs +++ b/examples/postgres/axum-social-with-tests/src/http/user.rs @@ -1,9 +1,9 @@ use axum::http::StatusCode; use axum::{routing::post, Extension, Json, Router}; -use once_cell::sync::Lazy; use rand::Rng; use regex::Regex; use std::time::Duration; +use std::cell::LazyCell; use serde::Deserialize; use sqlx::{PgExecutor, PgPool}; @@ -18,7 +18,7 @@ pub fn router() -> Router { Router::new().route("/v1/user", post(create_user)) } -static USERNAME_REGEX: Lazy = Lazy::new(|| Regex::new(r"^[0-9A-Za-z_]+$").unwrap()); +static USERNAME_REGEX: LazyCell = LazyCell::new(|| Regex::new(r"^[0-9A-Za-z_]+$").unwrap()); // CREATE USER diff --git a/sqlx-core/Cargo.toml b/sqlx-core/Cargo.toml index 51b82fa68e..1bba2d65bb 100644 --- a/sqlx-core/Cargo.toml +++ b/sqlx-core/Cargo.toml @@ -67,7 +67,6 @@ futures-intrusive = "0.5.0" futures-util = { version = "0.3.19", default-features = false, features = ["alloc", "sink", "io"] } log = { version = "0.4.18", default-features = false } memchr = { version = "2.4.1", default-features = false } -once_cell = "1.9.0" percent-encoding = "2.1.0" regex = { version = "1.5.5", optional = true } serde = { version = "1.0.132", features = ["derive", "rc"], optional = true } diff --git a/sqlx-core/src/any/driver.rs b/sqlx-core/src/any/driver.rs index cf97b84812..f4dab7e29b 100644 --- a/sqlx-core/src/any/driver.rs +++ b/sqlx-core/src/any/driver.rs @@ -5,8 +5,8 @@ use crate::connection::Connection; use crate::database::Database; use crate::Error; use futures_core::future::BoxFuture; -use once_cell::sync::OnceCell; use std::fmt::{Debug, Formatter}; +use std::cell::OnceCell; use url::Url; static DRIVERS: OnceCell<&'static [AnyDriver]> = OnceCell::new(); diff --git a/sqlx-macros-core/Cargo.toml b/sqlx-macros-core/Cargo.toml index d78cbe3d63..e58540ae2c 100644 --- a/sqlx-macros-core/Cargo.toml +++ b/sqlx-macros-core/Cargo.toml @@ -60,7 +60,6 @@ dotenvy = { workspace = true } hex = { version = "0.4.3" } heck = { version = "0.5" } either = "1.6.1" -once_cell = "1.9.0" proc-macro2 = { version = "1.0.79", default-features = false } serde = { version = "1.0.132", features = ["derive"] } serde_json = { version = "1.0.73" } diff --git a/sqlx-macros-core/src/database/mod.rs b/sqlx-macros-core/src/database/mod.rs index a2d0a1fa0d..6b272641eb 100644 --- a/sqlx-macros-core/src/database/mod.rs +++ b/sqlx-macros-core/src/database/mod.rs @@ -1,8 +1,7 @@ use std::collections::hash_map; use std::collections::HashMap; use std::sync::Mutex; - -use once_cell::sync::Lazy; +use std::cell::LazyCell; use sqlx_core::connection::Connection; use sqlx_core::database::Database; @@ -30,14 +29,14 @@ pub trait DatabaseExt: Database + TypeChecking { #[allow(dead_code)] pub struct CachingDescribeBlocking { - connections: Lazy>>, + connections: LazyCell>>, } #[allow(dead_code)] impl CachingDescribeBlocking { pub const fn new() -> Self { CachingDescribeBlocking { - connections: Lazy::new(|| Mutex::new(HashMap::new())), + connections: LazyCell::new(|| Mutex::new(HashMap::new())), } } diff --git a/sqlx-macros-core/src/lib.rs b/sqlx-macros-core/src/lib.rs index e8804f57fe..d43c119e7e 100644 --- a/sqlx-macros-core/src/lib.rs +++ b/sqlx-macros-core/src/lib.rs @@ -57,12 +57,12 @@ where { #[cfg(feature = "_rt-tokio")] { - use once_cell::sync::Lazy; + use std::cell::LazyCell; use tokio::runtime::{self, Runtime}; // We need a single, persistent Tokio runtime since we're caching connections, // otherwise we'll get "IO driver has terminated" errors. - static TOKIO_RT: Lazy = Lazy::new(|| { + static TOKIO_RT: LazyCell = LazyCell::new(|| { runtime::Builder::new_current_thread() .enable_all() .build() diff --git a/sqlx-macros-core/src/query/data.rs b/sqlx-macros-core/src/query/data.rs index ddf55c8bb2..9accbd8691 100644 --- a/sqlx-macros-core/src/query/data.rs +++ b/sqlx-macros-core/src/query/data.rs @@ -5,8 +5,8 @@ use std::io::Write as _; use std::marker::PhantomData; use std::path::{Path, PathBuf}; use std::sync::Mutex; +use std::cell::LazyCell; -use once_cell::sync::Lazy; use serde::{Serialize, Serializer}; use sqlx_core::database::Database; @@ -65,8 +65,8 @@ impl Serialize for SerializeDbName { } } -static OFFLINE_DATA_CACHE: Lazy>> = - Lazy::new(Default::default); +static OFFLINE_DATA_CACHE: LazyCell>> = + LazyCell::new(Default::default); /// Offline query data #[derive(Clone, serde::Deserialize)] diff --git a/sqlx-macros-core/src/query/mod.rs b/sqlx-macros-core/src/query/mod.rs index a51137413e..4a1bd5e43f 100644 --- a/sqlx-macros-core/src/query/mod.rs +++ b/sqlx-macros-core/src/query/mod.rs @@ -2,8 +2,8 @@ use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; use std::{fs, io}; +use std::cell::LazyCell; -use once_cell::sync::Lazy; use proc_macro2::TokenStream; use syn::Type; @@ -108,7 +108,7 @@ impl Metadata { } } -static METADATA: Lazy>> = Lazy::new(Default::default); +static METADATA: LazyCell>> = LazyCell::new(Default::default); // If we are in a workspace, lookup `workspace_root` since `CARGO_MANIFEST_DIR` won't // reflect the workspace dir: https://github.com/rust-lang/cargo/issues/3946 @@ -198,7 +198,7 @@ pub fn expand_input<'a>( database_url_parsed, .. } => Err(format!( - "no database driver found matching URL scheme {:?}; the corresponding Cargo feature may need to be enabled", + "no database driver found matching URL scheme {:?}; the corresponding Cargo feature may need to be enabled", database_url_parsed.scheme() ).into()), QueryDataSource::Cached(data) => { diff --git a/sqlx-mysql/Cargo.toml b/sqlx-mysql/Cargo.toml index 9bba7aa0a9..52717c4207 100644 --- a/sqlx-mysql/Cargo.toml +++ b/sqlx-mysql/Cargo.toml @@ -62,7 +62,6 @@ hex = "0.4.3" itoa = "1.0.1" log = "0.4.18" memchr = { version = "2.4.1", default-features = false } -once_cell = "1.9.0" percent-encoding = "2.1.0" smallvec = "1.7.0" stringprep = "0.1.2" diff --git a/sqlx-mysql/src/testing/mod.rs b/sqlx-mysql/src/testing/mod.rs index 2b6d46718c..ce78cceabb 100644 --- a/sqlx-mysql/src/testing/mod.rs +++ b/sqlx-mysql/src/testing/mod.rs @@ -1,6 +1,7 @@ use std::ops::Deref; use std::str::FromStr; use std::time::Duration; +use std::cell::OnceCell; use futures_core::future::BoxFuture; @@ -9,7 +10,6 @@ use crate::executor::Executor; use crate::pool::{Pool, PoolOptions}; use crate::query::query; use crate::{MySql, MySqlConnectOptions, MySqlConnection, MySqlDatabaseError}; -use once_cell::sync::OnceCell; use sqlx_core::connection::Connection; use sqlx_core::query_builder::QueryBuilder; use sqlx_core::query_scalar::query_scalar; @@ -152,7 +152,7 @@ async fn test_context(args: &TestArgs) -> Result, Error> { -- BLOB/TEXT columns can only be used as index keys with a prefix length: -- https://dev.mysql.com/doc/refman/8.4/en/column-indexes.html#column-indexes-prefix primary key(db_name(63)) - ); + ); "#, ) .await?; diff --git a/sqlx-postgres/Cargo.toml b/sqlx-postgres/Cargo.toml index f9328d03ca..411d9438d3 100644 --- a/sqlx-postgres/Cargo.toml +++ b/sqlx-postgres/Cargo.toml @@ -63,7 +63,6 @@ itoa = "1.0.1" log = "0.4.18" memchr = { version = "2.4.1", default-features = false } num-bigint = { version = "0.4.3", optional = true } -once_cell = "1.9.0" smallvec = { version = "1.7.0", features = ["serde"] } stringprep = "0.1.2" thiserror = "2.0.0" diff --git a/sqlx-postgres/src/advisory_lock.rs b/sqlx-postgres/src/advisory_lock.rs index 047ede6be6..d96abc8d46 100644 --- a/sqlx-postgres/src/advisory_lock.rs +++ b/sqlx-postgres/src/advisory_lock.rs @@ -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::cell::OnceCell; /// A mutex-like type utilizing [Postgres advisory locks]. /// diff --git a/sqlx-postgres/src/testing/mod.rs b/sqlx-postgres/src/testing/mod.rs index af20fe87ea..e752cc3f1d 100644 --- a/sqlx-postgres/src/testing/mod.rs +++ b/sqlx-postgres/src/testing/mod.rs @@ -2,10 +2,10 @@ use std::fmt::Write; use std::ops::Deref; use std::str::FromStr; use std::time::Duration; +use std::cell::OnceCell; use futures_core::future::BoxFuture; -use once_cell::sync::OnceCell; use sqlx_core::connection::Connection; use sqlx_core::query_scalar::query_scalar; @@ -146,7 +146,7 @@ async fn test_context(args: &TestArgs) -> Result, Error> { created_at timestamptz not null default now() ); - create index if not exists databases_created_at + create index if not exists databases_created_at on _sqlx_test.databases(created_at); create sequence if not exists _sqlx_test.database_ids;