diff --git a/.clippy.toml b/.clippy.toml index 56ce04e4..5d37e5d8 100644 --- a/.clippy.toml +++ b/.clippy.toml @@ -1 +1 @@ -msrv = "1.56.1" +msrv = "1.70" diff --git a/CHANGELOG.md b/CHANGELOG.md index d2912685..f33339d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/rustler/Cargo.toml b/rustler/Cargo.toml index 68b1e7cd..15567975 100644 --- a/rustler/Cargo.toml +++ b/rustler/Cargo.toml @@ -7,6 +7,7 @@ authors = ["Hansihe "] license = "MIT/Apache-2.0" readme = "../README.md" edition = "2021" +rust-version = "1.70" [features] big_integer = ["dep:num-bigint"] @@ -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 } diff --git a/rustler/src/lib.rs b/rustler/src/lib.rs index 2efc7159..fb0e1c91 100644 --- a/rustler/src/lib.rs +++ b/rustler/src/lib.rs @@ -29,8 +29,6 @@ pub mod wrapper; #[doc(hidden)] pub mod codegen_runtime; -pub use lazy_static; - #[macro_use] pub mod types; diff --git a/rustler/src/serde/atoms.rs b/rustler/src/serde/atoms.rs index fc162b2a..27ac7346 100644 --- a/rustler/src/serde/atoms.rs +++ b/rustler/src/serde/atoms.rs @@ -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, diff --git a/rustler/src/types/atom.rs b/rustler/src/types/atom.rs index 43628abb..7c8371e6 100644 --- a/rustler/src/types/atom.rs +++ b/rustler/src/types/atom.rs @@ -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 = 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 } )* }; diff --git a/rustler_tests/native/rustler_compile_tests/Cargo.toml b/rustler_tests/native/rustler_compile_tests/Cargo.toml index 7db15519..f4b70a6a 100644 --- a/rustler_tests/native/rustler_compile_tests/Cargo.toml +++ b/rustler_tests/native/rustler_compile_tests/Cargo.toml @@ -10,5 +10,4 @@ path = "src/lib.rs" crate-type = ["cdylib"] [dependencies] -lazy_static = "1.4" rustler = { path = "../../../rustler" } diff --git a/rustler_tests/native/rustler_serde_test/Cargo.toml b/rustler_tests/native/rustler_serde_test/Cargo.toml index 58f8ea6d..c2cc1388 100644 --- a/rustler_tests/native/rustler_serde_test/Cargo.toml +++ b/rustler_tests/native/rustler_serde_test/Cargo.toml @@ -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" diff --git a/rustler_tests/native/rustler_test/Cargo.toml b/rustler_tests/native/rustler_test/Cargo.toml index dc341923..c2418189 100644 --- a/rustler_tests/native/rustler_test/Cargo.toml +++ b/rustler_tests/native/rustler_test/Cargo.toml @@ -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" } diff --git a/rustler_tests/native/rustler_test/src/test_resource.rs b/rustler_tests/native/rustler_test/src/test_resource.rs index 5aa58d28..8739bcaa 100644 --- a/rustler_tests/native/rustler_test/src/test_resource.rs +++ b/rustler_tests/native/rustler_test/src/test_resource.rs @@ -1,5 +1,5 @@ use rustler::{Binary, Env, ResourceArc}; -use std::sync::RwLock; +use std::sync::{OnceLock, RwLock}; pub struct TestResource { test_field: RwLock, @@ -46,14 +46,19 @@ pub fn resource_get_integer_field(resource: ResourceArc) -> 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 = 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 } } } @@ -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); } } @@ -74,7 +79,7 @@ pub fn resource_make_immutable(u: u32) -> ResourceArc { // 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] @@ -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()), ) }