From 293eb356d755733744498c5751402f31241036d5 Mon Sep 17 00:00:00 2001 From: Philipp Rehner <69816385+prehner@users.noreply.github.com> Date: Thu, 5 Sep 2024 19:58:48 +0200 Subject: [PATCH] Replace lazy_static with LazyLock (#58) --- Cargo.toml | 2 +- src/si_fmt.rs | 143 ++++++++++++++++++++++++-------------------------- 2 files changed, 70 insertions(+), 75 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 10a6008..21db5fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ name = "quantity" version = "0.8.0" authors = ["Philipp Rehner ", "Gernot Bauer "] +rust-version = "1.80" edition = "2021" license = "MIT OR Apache-2.0" description = "Representation of quantites, i.e. of unit valued scalars and arrays." @@ -19,7 +20,6 @@ rustdoc-args = [ "--html-in-header", "./src/docs-header.html" ] [dependencies] ndarray = { version = "0.15", features = ["serde", "approx-0_5"] } approx = "0.5" -lazy_static = "1.4" thiserror = "1.0" serde = { version = "1.0", features = ["derive"] } bincode = "1.3" diff --git a/src/si_fmt.rs b/src/si_fmt.rs index 291ce09..4320fc9 100644 --- a/src/si_fmt.rs +++ b/src/si_fmt.rs @@ -1,10 +1,10 @@ use super::si::*; -use lazy_static::lazy_static; use ndarray::prelude::*; use regex::Regex; use std::cmp::Ordering; use std::collections::HashMap; use std::fmt; +use std::sync::LazyLock; macro_rules! impl_fmt { ($trt:path) => { @@ -214,8 +214,8 @@ impl Debye { const UNIT_SYMBOLS: [&str; 7] = ["m", "kg", "s", "A", "mol", "K", "cd"]; -lazy_static! { - static ref DERIVED_UNIT_SYMBOLS: HashMap<&'static str, (SINumber, Option)> = { +static DERIVED_UNIT_SYMBOLS: LazyLock)>> = + LazyLock::new(|| { let mut m = HashMap::new(); m.insert("m", (METER, Some(MEGA))); m.insert("g", (GRAM, Some(MEGA))); @@ -241,8 +241,7 @@ lazy_static! { m.insert("lm", (CANDELA, None)); m.insert("s²", (SECOND.powi(2), None)); m - }; -} + }); type SIUnitSymbol = (SINumber, String, Option, Vec<&'static str>, Vec); @@ -286,76 +285,72 @@ fn insert_derived_unit(map: &mut HashMap, s: &'static str) ); } -lazy_static! { - static ref DERIVED_UNITS: HashMap = { - let mut m = HashMap::new(); - insert_derived_unit(&mut m, "m"); - insert_derived_unit(&mut m, "g"); - insert_derived_unit(&mut m, "s"); - insert_derived_unit(&mut m, "mol"); - insert_derived_unit(&mut m, "K"); - insert_derived_unit(&mut m, "Hz"); - insert_derived_unit(&mut m, "N"); - insert_derived_unit(&mut m, "Pa"); - insert_derived_unit(&mut m, "J"); - insert_derived_unit(&mut m, "W"); - insert_derived_unit(&mut m, "C"); - insert_derived_unit(&mut m, "V"); - insert_derived_unit(&mut m, "F"); - insert_derived_unit(&mut m, "Ω"); - insert_derived_unit(&mut m, "S"); - insert_derived_unit(&mut m, "Wb"); - insert_derived_unit(&mut m, "T"); - insert_derived_unit(&mut m, "H"); - insert_derived_unit(&mut m, "mol/m³"); - insert_derived_unit(&mut m, "mol/m²"); - insert_derived_unit(&mut m, "mol/m"); - insert_derived_unit(&mut m, "m³/mol"); - insert_derived_unit(&mut m, "m³/mol/K"); - insert_derived_unit(&mut m, "g/m³"); - insert_derived_unit(&mut m, "N/m"); - insert_derived_unit(&mut m, "J*s"); - insert_derived_unit(&mut m, "J/mol"); - insert_derived_unit(&mut m, "J/K"); - insert_derived_unit(&mut m, "J/mol/K"); - insert_derived_unit(&mut m, "J/kg"); - insert_derived_unit(&mut m, "J/kg/K"); - insert_derived_unit(&mut m, "Pa*s"); - insert_derived_unit(&mut m, "m/s"); - insert_derived_unit(&mut m, "m²/s"); - insert_derived_unit(&mut m, "W/m/K"); - insert_derived_unit(&mut m, "g/mol"); - insert_derived_unit(&mut m, "m²"); - insert_derived_unit(&mut m, "m³"); - insert_derived_unit(&mut m, "lm/W"); - insert_derived_unit(&mut m, "m³/kg/s²"); - m - }; -} +static DERIVED_UNITS: LazyLock> = LazyLock::new(|| { + let mut m = HashMap::new(); + insert_derived_unit(&mut m, "m"); + insert_derived_unit(&mut m, "g"); + insert_derived_unit(&mut m, "s"); + insert_derived_unit(&mut m, "mol"); + insert_derived_unit(&mut m, "K"); + insert_derived_unit(&mut m, "Hz"); + insert_derived_unit(&mut m, "N"); + insert_derived_unit(&mut m, "Pa"); + insert_derived_unit(&mut m, "J"); + insert_derived_unit(&mut m, "W"); + insert_derived_unit(&mut m, "C"); + insert_derived_unit(&mut m, "V"); + insert_derived_unit(&mut m, "F"); + insert_derived_unit(&mut m, "Ω"); + insert_derived_unit(&mut m, "S"); + insert_derived_unit(&mut m, "Wb"); + insert_derived_unit(&mut m, "T"); + insert_derived_unit(&mut m, "H"); + insert_derived_unit(&mut m, "mol/m³"); + insert_derived_unit(&mut m, "mol/m²"); + insert_derived_unit(&mut m, "mol/m"); + insert_derived_unit(&mut m, "m³/mol"); + insert_derived_unit(&mut m, "m³/mol/K"); + insert_derived_unit(&mut m, "g/m³"); + insert_derived_unit(&mut m, "N/m"); + insert_derived_unit(&mut m, "J*s"); + insert_derived_unit(&mut m, "J/mol"); + insert_derived_unit(&mut m, "J/K"); + insert_derived_unit(&mut m, "J/mol/K"); + insert_derived_unit(&mut m, "J/kg"); + insert_derived_unit(&mut m, "J/kg/K"); + insert_derived_unit(&mut m, "Pa*s"); + insert_derived_unit(&mut m, "m/s"); + insert_derived_unit(&mut m, "m²/s"); + insert_derived_unit(&mut m, "W/m/K"); + insert_derived_unit(&mut m, "g/mol"); + insert_derived_unit(&mut m, "m²"); + insert_derived_unit(&mut m, "m³"); + insert_derived_unit(&mut m, "lm/W"); + insert_derived_unit(&mut m, "m³/kg/s²"); + m +}); -lazy_static! { - static ref PREFIX_SYMBOLS: HashMap = { - let mut m = HashMap::new(); - m.insert(0, " "); - m.insert(-24, "y"); - m.insert(-21, "z"); - m.insert(-18, "a"); - m.insert(-15, "f"); - m.insert(-12, "p"); - m.insert(-9, "n"); - m.insert(-6, "µ"); - m.insert(-3, "m"); - m.insert(3, "k"); - m.insert(6, "M"); - m.insert(9, "G"); - m.insert(12, "T"); - m.insert(15, "P"); - m.insert(18, "E"); - m.insert(21, "Z"); - m.insert(24, "Y"); - m - }; -} +static PREFIX_SYMBOLS: LazyLock> = LazyLock::new(|| { + let mut m = HashMap::new(); + m.insert(0, " "); + m.insert(-24, "y"); + m.insert(-21, "z"); + m.insert(-18, "a"); + m.insert(-15, "f"); + m.insert(-12, "p"); + m.insert(-9, "n"); + m.insert(-6, "µ"); + m.insert(-3, "m"); + m.insert(3, "k"); + m.insert(6, "M"); + m.insert(9, "G"); + m.insert(12, "T"); + m.insert(15, "P"); + m.insert(18, "E"); + m.insert(21, "Z"); + m.insert(24, "Y"); + m +}); #[cfg(test)] mod tests {