Skip to content

Commit 109acbc

Browse files
committed
Use the once_cell crate instead of lazy_static.
1 parent 2cf588d commit 109acbc

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ features = ["chrono", "aq_unstable"]
2020
aq_unstable = []
2121

2222
[dependencies]
23-
lazy_static = "1.3.0"
23+
once_cell = "1.19.0"
2424
paste = "1.0.5"
2525
chrono = { version = "0.4.20", optional = true, default-features = false, features = ["clock"] }
2626
oracle_procmacro = { version = "0.1.2", path = "./oracle_procmacro" }

src/context.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ use crate::binding::*;
1717
use crate::DbError;
1818
use crate::Error;
1919
use crate::Result;
20-
use lazy_static::lazy_static;
21-
use std::mem::MaybeUninit;
20+
use once_cell::sync::OnceCell;
21+
use std::mem::{self, MaybeUninit};
2222
use std::os::raw::c_char;
2323
use std::ptr;
24-
use std::result;
2524
use std::sync::{Arc, Mutex};
2625

2726
//
@@ -41,15 +40,23 @@ pub(crate) struct Context {
4140
unsafe impl Sync for Context {}
4241
unsafe impl Send for Context {}
4342

44-
lazy_static! {
45-
static ref DPI_CONTEXT: result::Result<Context, DbError> = {
43+
static GLOBAL_CONTEXT: OnceCell<Context> = OnceCell::new();
44+
45+
impl Context {
46+
// Use this only inside of GLOBAL_CONTEXT.get_or_try_init().
47+
fn from_params(params: &mut dpiContextCreateParams) -> Result<Context> {
48+
if params.defaultDriverName.is_null() {
49+
let driver_name: &'static str =
50+
concat!("rust-oracle : ", env!("CARGO_PKG_VERSION"), "\0");
51+
params.defaultDriverName = driver_name.as_ptr() as *const c_char;
52+
}
4653
let mut ctxt = ptr::null_mut();
4754
let mut err = MaybeUninit::uninit();
4855
if unsafe {
4956
dpiContext_createWithParams(
5057
DPI_MAJOR_VERSION,
5158
DPI_MINOR_VERSION,
52-
ptr::null_mut(),
59+
params,
5360
&mut ctxt,
5461
err.as_mut_ptr(),
5562
)
@@ -60,17 +67,17 @@ lazy_static! {
6067
last_warning: None,
6168
})
6269
} else {
63-
Err(DbError::from_dpi_error(&unsafe { err.assume_init() }))
70+
Err(Error::from_dpi_error(&unsafe { err.assume_init() }))
6471
}
65-
};
66-
}
72+
}
6773

68-
impl Context {
6974
pub fn new0() -> Result<Context> {
70-
match *DPI_CONTEXT {
71-
Ok(ref ctxt) => Ok(ctxt.clone()),
72-
Err(ref err) => Err(Error::from_db_error(err.clone())),
73-
}
75+
Ok(GLOBAL_CONTEXT
76+
.get_or_try_init(|| {
77+
let mut params = unsafe { mem::zeroed() };
78+
Context::from_params(&mut params)
79+
})?
80+
.clone())
7481
}
7582

7683
pub fn new() -> Result<Context> {
@@ -101,10 +108,7 @@ impl Context {
101108
unsafe {
102109
dpiContext_initCommonCreateParams(self.context, params.as_mut_ptr());
103110
let mut params = params.assume_init();
104-
let driver_name: &'static str = concat!("rust-oracle : ", env!("CARGO_PKG_VERSION"));
105111
params.createMode |= DPI_MODE_CREATE_THREADED;
106-
params.driverName = driver_name.as_ptr() as *const c_char;
107-
params.driverNameLength = driver_name.len() as u32;
108112
params
109113
}
110114
}

src/sql_type/lob.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ impl_traits!(FromSql, ToSqlNull, ToSql, Read, Write, SeekInChars, Lob for Nclob
751751
mod tests {
752752
use super::*;
753753
use crate::test_util;
754-
use lazy_static::lazy_static;
754+
use once_cell::sync::Lazy;
755755
use std::io::Read;
756756
use std::io::Seek;
757757
use std::io::Write;
@@ -795,15 +795,13 @@ mod tests {
795795
}
796796
}
797797

798-
lazy_static! {
799-
static ref TEST_DATA: String = {
800-
Rand::new()
801-
.take(100)
802-
.map(|n| CRAB_CHARS[(n as usize) % CRAB_CHARS.len()])
803-
.collect::<Vec<_>>()
804-
.join("")
805-
};
806-
}
798+
static TEST_DATA: Lazy<String> = Lazy::new(|| {
799+
Rand::new()
800+
.take(100)
801+
.map(|n| CRAB_CHARS[(n as usize) % CRAB_CHARS.len()])
802+
.collect::<Vec<_>>()
803+
.join("")
804+
});
807805

808806
#[test]
809807
fn read_write_blob() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {

0 commit comments

Comments
 (0)