1
1
use std:: ops:: Deref ;
2
2
use std:: str:: FromStr ;
3
+ use std:: sync:: OnceLock ;
3
4
use std:: time:: Duration ;
4
5
5
6
use futures_core:: future:: BoxFuture ;
6
7
7
- use once_cell:: sync:: OnceCell ;
8
8
use sqlx_core:: connection:: Connection ;
9
9
use sqlx_core:: query_builder:: QueryBuilder ;
10
10
use sqlx_core:: query_scalar:: query_scalar;
@@ -18,8 +18,8 @@ use crate::{MySql, MySqlConnectOptions, MySqlConnection};
18
18
19
19
pub ( crate ) use sqlx_core:: testing:: * ;
20
20
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 ( ) ;
23
23
24
24
impl TestSupport for MySql {
25
25
fn test_context ( args : & TestArgs ) -> BoxFuture < ' _ , Result < TestContext < Self > , Error > > {
@@ -119,7 +119,7 @@ async fn test_context(args: &TestArgs) -> Result<TestContext<MySql>, Error> {
119
119
. after_release ( |_conn, _| Box :: pin ( async move { Ok ( false ) } ) )
120
120
. connect_lazy_with ( master_opts) ;
121
121
122
- let master_pool = match MASTER_POOL . try_insert ( pool) {
122
+ let master_pool = match once_lock_try_insert_polyfill ( & MASTER_POOL , pool) {
123
123
Ok ( inserted) => inserted,
124
124
Err ( ( existing, pool) ) => {
125
125
// Sanity checks.
@@ -195,3 +195,12 @@ async fn do_cleanup(conn: &mut MySqlConnection, db_name: &str) -> Result<(), Err
195
195
196
196
Ok ( ( ) )
197
197
}
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
+ }
0 commit comments