Skip to content

Commit

Permalink
Merge pull request #302 from serprex/remove-lazy-static
Browse files Browse the repository at this point in the history
Replace lazy_static & once_cell with std::sync::OnceLock
  • Loading branch information
blackbeam authored May 14, 2024
2 parents 08b7b70 + 11a86f1 commit a475a5f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 30 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ futures-core = "0.3"
futures-util = "0.3"
futures-sink = "0.3"
keyed_priority_queue = "0.4"
lazy_static = "1"
lru = "0.12.0"
mio = { version = "0.8.0", features = ["os-poll", "net"] }
mysql_common = { version = "0.32", default-features = false }
once_cell = "1.7.2"
pem = "3.0"
percent-encoding = "2.1.0"
pin-project = "1.0.2"
Expand Down
12 changes: 6 additions & 6 deletions src/conn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ impl Conn {
);

// Serialize here to satisfy borrow checker.
let mut buf = crate::BUFFER_POOL.get();
let mut buf = crate::buffer_pool().get();
handshake_response.serialize(buf.as_mut());

self.write_packet(buf).await?;
Expand Down Expand Up @@ -633,7 +633,7 @@ impl Conn {
if let Some(plugin_data) = plugin_data {
self.write_struct(&plugin_data.into_owned()).await?;
} else {
self.write_packet(crate::BUFFER_POOL.get()).await?;
self.write_packet(crate::buffer_pool().get()).await?;
}

self.continue_auth().await?;
Expand Down Expand Up @@ -701,7 +701,7 @@ impl Conn {
}
Some(0x04) => {
let pass = self.inner.opts.pass().unwrap_or_default();
let mut pass = crate::BUFFER_POOL.get_with(pass.as_bytes());
let mut pass = crate::buffer_pool().get_with(pass.as_bytes());
pass.as_mut().push(0);

if self.is_secure() || self.is_socket() {
Expand Down Expand Up @@ -838,13 +838,13 @@ impl Conn {

/// Writes bytes to a server.
pub(crate) async fn write_bytes(&mut self, bytes: &[u8]) -> Result<()> {
let buf = crate::BUFFER_POOL.get_with(bytes);
let buf = crate::buffer_pool().get_with(bytes);
self.write_packet(buf).await
}

/// Sends a serializable structure to a server.
pub(crate) async fn write_struct<T: MySerialize>(&mut self, x: &T) -> Result<()> {
let mut buf = crate::BUFFER_POOL.get();
let mut buf = crate::buffer_pool().get();
x.serialize(buf.as_mut());
self.write_packet(buf).await
}
Expand All @@ -870,7 +870,7 @@ impl Conn {
T: AsRef<[u8]>,
{
let cmd_data = cmd_data.as_ref();
let mut buf = crate::BUFFER_POOL.get();
let mut buf = crate::buffer_pool().get();
let body = buf.as_mut();
body.push(cmd as u8);
body.extend_from_slice(cmd_data);
Expand Down
4 changes: 2 additions & 2 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Default for PacketCodec {
fn default() -> Self {
Self {
inner: Default::default(),
decode_buf: crate::BUFFER_POOL.get(),
decode_buf: crate::buffer_pool().get(),
}
}
}
Expand All @@ -100,7 +100,7 @@ impl Decoder for PacketCodec {

fn decode(&mut self, src: &mut BytesMut) -> std::result::Result<Option<Self::Item>, IoError> {
if self.inner.decode(src, self.decode_buf.as_mut())? {
let new_buf = crate::BUFFER_POOL.get();
let new_buf = crate::buffer_pool().get();
Ok(Some(replace(&mut self.decode_buf, new_buf)))
} else {
Ok(None)
Expand Down
33 changes: 13 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,11 @@ mod queryable;

type BoxFuture<'a, T> = futures_core::future::BoxFuture<'a, Result<T>>;

static BUFFER_POOL: once_cell::sync::Lazy<Arc<crate::buffer_pool::BufferPool>> =
once_cell::sync::Lazy::new(Default::default);
fn buffer_pool() -> &'static Arc<crate::buffer_pool::BufferPool> {
static BUFFER_POOL: std::sync::OnceLock<Arc<crate::buffer_pool::BufferPool>> =
std::sync::OnceLock::new();
BUFFER_POOL.get_or_init(Default::default)
}

#[cfg(feature = "binlog")]
#[doc(inline)]
Expand Down Expand Up @@ -608,9 +611,8 @@ pub mod prelude {

#[doc(hidden)]
pub mod test_misc {
use lazy_static::lazy_static;

use std::env;
use std::sync::OnceLock;

use crate::opts::{Opts, OptsBuilder, SslOpts};

Expand All @@ -621,26 +623,17 @@ pub mod test_misc {
_dummy(err);
}

lazy_static! {
pub static ref DATABASE_URL: String = {
pub fn get_opts() -> OptsBuilder {
static DATABASE_OPTS: OnceLock<Opts> = OnceLock::new();
let database_opts = DATABASE_OPTS.get_or_init(|| {
if let Ok(url) = env::var("DATABASE_URL") {
let opts = Opts::from_url(&url).expect("DATABASE_URL invalid");
if opts
.db_name()
.expect("a database name is required")
.is_empty()
{
panic!("database name is empty");
}
url
Opts::from_url(&url).expect("DATABASE_URL invalid")
} else {
"mysql://root:password@localhost:3307/mysql".into()
Opts::from_url("mysql://root:password@localhost:3307/mysql").unwrap()
}
};
}
});

pub fn get_opts() -> OptsBuilder {
let mut builder = OptsBuilder::from_opts(Opts::from_url(&DATABASE_URL).unwrap());
let mut builder = OptsBuilder::from_opts(database_opts.clone());
if test_ssl() {
let ssl_opts = SslOpts::default()
.with_danger_skip_domain_validation(true)
Expand Down

0 comments on commit a475a5f

Please sign in to comment.