Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop lazy_static in favour of std::sync::OnceLock #609

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.56.1"
msrv = "1.70"
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ versions.
### Fixed
### Changed

- Drop usage of `lazy_static` in favour of `std::sync::OnceLock`. This change
raises the minimal supported Rust version to 1.70.

### Removed

- The old macros `rustler_export_nifs!`, `rustler::rustler_atoms!` and
Expand Down
2 changes: 1 addition & 1 deletion rustler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ authors = ["Hansihe <[email protected]>"]
license = "MIT/Apache-2.0"
readme = "../README.md"
edition = "2021"
rust-version = "1.70"

[features]
big_integer = ["dep:num-bigint"]
Expand All @@ -20,7 +21,6 @@ nif_version_2_17 = ["nif_version_2_16", "rustler_sys/nif_version_2_17"]
serde = ["dep:serde"]

[dependencies]
lazy_static = "1.4"
rustler_codegen = { path = "../rustler_codegen", version = "0.32.1", optional = true}
rustler_sys = { path = "../rustler_sys", version = "~2.4.0" }
num-bigint = { version = "0.4", optional = true }
Expand Down
2 changes: 0 additions & 2 deletions rustler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ pub mod wrapper;
#[doc(hidden)]
pub mod codegen_runtime;

pub use lazy_static;

#[macro_use]
pub mod types;

Expand Down
8 changes: 2 additions & 6 deletions rustler/src/serde/atoms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
use crate::serde::Error;
use crate::{types::atom::Atom, Encoder, Env, Term};

use lazy_static::lazy_static;

lazy_static! {
pub static ref OK: String = String::from("Ok");
pub static ref ERROR: String = String::from("Err");
}
pub static OK: &str = "Ok";
pub static ERROR: &str = "Err";

atoms! {
nil,
Expand Down
20 changes: 13 additions & 7 deletions rustler/src/types/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,23 @@ macro_rules! atoms {
struct RustlerAtoms {
$( $name : $crate::types::atom::Atom ),*
}
$crate::lazy_static::lazy_static! {
static ref RUSTLER_ATOMS: RustlerAtoms = $crate::env::OwnedEnv::new().run(|env| {
RustlerAtoms {
$( $name: $crate::atoms!(@internal_make_atom(env, $name $( = $str)? )) ),*
}
});
impl RustlerAtoms {
fn get() -> &'static Self {
use std::sync::OnceLock;
static RUSTLER_ATOMS: OnceLock<RustlerAtoms> = OnceLock::new();
RUSTLER_ATOMS.get_or_init(||
$crate::env::OwnedEnv::new().run(|env| {
RustlerAtoms {
$( $name: $crate::atoms!(@internal_make_atom(env, $name $( = $str)? )) ),*
}
})
)
}
}
$(
$( #[$attr] )*
pub fn $name() -> $crate::types::atom::Atom {
RUSTLER_ATOMS.$name
RustlerAtoms::get().$name
}
)*
};
Expand Down
1 change: 0 additions & 1 deletion rustler_tests/native/rustler_compile_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]
lazy_static = "1.4"
rustler = { path = "../../../rustler" }
1 change: 0 additions & 1 deletion rustler_tests/native/rustler_serde_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ name = "rustler_serde_test"
crate-type = ["cdylib"]

[dependencies]
lazy_static = "1"
rustler = {path = "../../../rustler", features = ["serde"]}
serde = { version = "1.0", features = ["derive"] }
serde_bytes = "0.11"
Expand Down
1 change: 0 additions & 1 deletion rustler_tests/native/rustler_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ nif_version_2_16 = ["nif_version_2_15", "rustler/nif_version_2_16"]
nif_version_2_17 = ["nif_version_2_16", "rustler/nif_version_2_17"]

[dependencies]
lazy_static = "1.4"
rustler = { path = "../../../rustler" }
21 changes: 13 additions & 8 deletions rustler_tests/native/rustler_test/src/test_resource.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustler::{Binary, Env, ResourceArc};
use std::sync::RwLock;
use std::sync::{OnceLock, RwLock};

pub struct TestResource {
test_field: RwLock<i32>,
Expand Down Expand Up @@ -46,14 +46,19 @@ pub fn resource_get_integer_field(resource: ResourceArc<TestResource>) -> i32 {

use std::sync::atomic::{AtomicUsize, Ordering};

lazy_static::lazy_static! {
static ref COUNT: AtomicUsize = AtomicUsize::new(0);
static ref STATIC_BIN: [u8; 10] = [0,1,2,3,4,5,6,7,8,9];
fn get_count() -> &'static AtomicUsize {
static COUNT: OnceLock<AtomicUsize> = OnceLock::new();
COUNT.get_or_init(|| AtomicUsize::new(0))
}

fn get_static_bin() -> &'static [u8; 10] {
static STATIC_BIN: OnceLock<[u8; 10]> = OnceLock::new();
STATIC_BIN.get_or_init(|| [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
}

impl ImmutableResource {
fn new(u: u32) -> ImmutableResource {
COUNT.fetch_add(1, Ordering::SeqCst);
get_count().fetch_add(1, Ordering::SeqCst);
ImmutableResource { a: u, b: !u }
}
}
Expand All @@ -62,7 +67,7 @@ impl Drop for ImmutableResource {
fn drop(&mut self) {
assert_eq!(self.a, !self.b);
self.b = self.a;
COUNT.fetch_sub(1, Ordering::SeqCst);
get_count().fetch_sub(1, Ordering::SeqCst);
}
}

Expand All @@ -74,7 +79,7 @@ pub fn resource_make_immutable(u: u32) -> ResourceArc<ImmutableResource> {
// Count how many instances of `ImmutableResource` are currently alive globally.
#[rustler::nif]
pub fn resource_immutable_count() -> u32 {
COUNT.load(Ordering::SeqCst) as u32
get_count().load(Ordering::SeqCst) as u32
}

#[rustler::nif]
Expand All @@ -100,7 +105,7 @@ pub fn resource_make_binaries(
// From vec (on the heap)
resource.make_binary(env, |w| &w.b),
// From static
resource.make_binary(env, |_| &*STATIC_BIN),
resource.make_binary(env, |_| get_static_bin()),
)
}

Expand Down
Loading