Skip to content

Commit

Permalink
Replace lazy_static with LazyLock (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
prehner authored Sep 5, 2024
1 parent 90680a9 commit 293eb35
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "quantity"
version = "0.8.0"
authors = ["Philipp Rehner <[email protected]>",
"Gernot Bauer <[email protected]>"]
rust-version = "1.80"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Representation of quantites, i.e. of unit valued scalars and arrays."
Expand All @@ -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"
Expand Down
143 changes: 69 additions & 74 deletions src/si_fmt.rs
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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<f64>)> = {
static DERIVED_UNIT_SYMBOLS: LazyLock<HashMap<&'static str, (SINumber, Option<f64>)>> =
LazyLock::new(|| {
let mut m = HashMap::new();
m.insert("m", (METER, Some(MEGA)));
m.insert("g", (GRAM, Some(MEGA)));
Expand All @@ -241,8 +241,7 @@ lazy_static! {
m.insert("lm", (CANDELA, None));
m.insert("s²", (SECOND.powi(2), None));
m
};
}
});

type SIUnitSymbol = (SINumber, String, Option<f64>, Vec<&'static str>, Vec<i8>);

Expand Down Expand Up @@ -286,76 +285,72 @@ fn insert_derived_unit(map: &mut HashMap<SIUnit, SIUnitSymbol>, s: &'static str)
);
}

lazy_static! {
static ref DERIVED_UNITS: HashMap<SIUnit, SIUnitSymbol> = {
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<HashMap<SIUnit, SIUnitSymbol>> = 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<i8, &'static str> = {
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<HashMap<i8, &'static str>> = 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 {
Expand Down

0 comments on commit 293eb35

Please sign in to comment.