Skip to content

Commit

Permalink
Remove lazy_static
Browse files Browse the repository at this point in the history
Rust 1.70 includes OnceLock in std, which can replace our use of
`lazy_static`.

---------

Co-authored-by: Vikram Bhaskaran <[email protected]>
  • Loading branch information
vbasky and Vikram Bhaskaran authored Jan 19, 2025
1 parent ba26917 commit 66a9315
Show file tree
Hide file tree
Showing 23 changed files with 262 additions and 227 deletions.
5 changes: 0 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ vidyut-prakriya = { path = "./vidyut-prakriya" }
vidyut-sandhi = { path = "./vidyut-sandhi" }
csv = "1.1.6"
multimap = "0.8.3"
lazy_static = "1.4.0"
glob = "0.3.1"

[workspace.dependencies]
Expand Down
2 changes: 0 additions & 2 deletions bindings-python/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion vidyut-chandas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ edition = "2021"

[dependencies]
console_error_panic_hook = "0.1.7"
lazy_static = "1.4.0"
serde = { version = "1.0.150", features = ["derive"] }
serde-wasm-bindgen = "0.4"
serde_derive = "1.0.193"
Expand Down
19 changes: 9 additions & 10 deletions vidyut-chandas/src/sounds.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use lazy_static::lazy_static;
use std::sync::OnceLock;

lazy_static! {
static ref HRASVA: Set = Set::from("aiufx");
static ref AC: Set = Set::from("aAiIuUfFxXeEoO");
static ref HAL: Set = Set::from("kKgGNcCjJYwWqQRtTdDnpPbBmyrlvSzshL");
}
static HRASVA: OnceLock<Set> = OnceLock::new();
static AC: OnceLock<Set> = OnceLock::new();
static HAL: OnceLock<Set> = OnceLock::new();

type Sound = char;

Expand Down Expand Up @@ -37,22 +35,23 @@ impl Set {

/// Returns whether `c` is a vowel.
pub(crate) fn is_ac(c: Sound) -> bool {
AC.contains(c)
AC.get_or_init(|| Set::from("aAiIuUfFxXeEoO")).contains(c)
}

/// Returns whether `c` is a short vowel.
pub(crate) fn is_hrasva(c: Sound) -> bool {
HRASVA.contains(c)
HRASVA.get_or_init(|| Set::from("aiufx")).contains(c)
}

/// Returns whether `c` is a consonant.
pub(crate) fn is_hal(c: Sound) -> bool {
HAL.contains(c)
HAL.get_or_init(|| Set::from("kKgGNcCjJYwWqQRtTdDnpPbBmyrlvSzshL"))
.contains(c)
}

/// Returns whether `c` is a Sanskrit sound.
pub(crate) fn is_sanskrit(c: Sound) -> bool {
AC.contains(c) || HAL.contains(c) || matches!(c, 'M' | 'H')
is_ac(c) || is_hal(c) || matches!(c, 'M' | 'H')
}

/// Returns whether `s` starts with a consonant cluster.
Expand Down
1 change: 0 additions & 1 deletion vidyut-cheda/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ compact_str = "0.8.1"
clap = { workspace = true }
log = { workspace = true }
env_logger = { workspace = true }
lazy_static = "1.5.0"
regex = "1.11.1"
rmp-serde = { workspace = true }
serde = {workspace = true }
13 changes: 7 additions & 6 deletions vidyut-cheda/src/normalize_text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use lazy_static::lazy_static;
use std::sync::OnceLock;

use regex::Regex;

/// Creates a normalized version of `text` that is easier to process.
Expand All @@ -8,12 +9,12 @@ use regex::Regex;
/// 2. Delete all whitespace spans.
/// 3. Separate all remaining spans with a single " ".
pub fn normalize(text: &str) -> String {
lazy_static! {
static ref RE: Regex =
Regex::new(r"([a-zA-Z']+)|(\s+)|([^a-zA-Z']+)").expect("always defined");
}
static RE: OnceLock<Regex> = OnceLock::new();

let re =
RE.get_or_init(|| Regex::new(r"([a-zA-Z']+)|(\s+)|([^a-zA-Z']+)").expect("always defined"));

let mut ret = RE
let mut ret = re
.find_iter(text)
.map(|m| m.as_str())
.filter(|s| !s.trim().is_empty())
Expand Down
34 changes: 16 additions & 18 deletions vidyut-cheda/src/sounds.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Utility functions for checking Sanskrit sounds.
use lazy_static::lazy_static;
use std::sync::OnceLock;

/// A set of Sanskrit sounds.
///
Expand Down Expand Up @@ -42,41 +42,39 @@ impl Default for SoundSet {
/// - other punctuation characters (|, ||, numbers)
/// - characters or symbols from non-SLP1 encodings
pub fn is_sanskrit(c: char) -> bool {
lazy_static! {
static ref CHARS: SoundSet =
SoundSet::from("aAiIuUfFxXeEoOMHkKgGNcCjJYwWqQRtTdDnpPbBmyrlvSzshL'");
}
CHARS.contains(c)
static CHARS: OnceLock<SoundSet> = OnceLock::new();
let chars =
CHARS.get_or_init(|| SoundSet::from("aAiIuUfFxXeEoOMHkKgGNcCjJYwWqQRtTdDnpPbBmyrlvSzshL'"));
chars.contains(c)
}

/// Returns whether the given sound is a vowel.
///
/// `ac` is the Paninian name for the Sanskrit vowels.
pub fn is_ac(c: char) -> bool {
lazy_static! {
static ref AC: SoundSet = SoundSet::from("aAiIuUfFxXeEoO");
}
AC.contains(c)
static AC: OnceLock<SoundSet> = OnceLock::new();
let ac = AC.get_or_init(|| SoundSet::from("aAiIuUfFxXeEoO"));
ac.contains(c)
}

/// Returns whether the given sound is a consonant.
///
/// `hal` is the Paninian name for the Sanskrit consonants.
#[allow(unused)]
pub fn is_hal(c: char) -> bool {
lazy_static! {
static ref HAL: SoundSet = SoundSet::from("kKgGNcCjJYwWqQRtTdDnpPbBmyrlvSzshL");
}
HAL.contains(c)
static HAL: OnceLock<SoundSet> = OnceLock::new();

let hal = HAL.get_or_init(|| SoundSet::from("kKgGNcCjJYwWqQRtTdDnpPbBmyrlvSzshL"));
hal.contains(c)
}

/// Returns whether the given sound is voiced.
#[allow(unused)]
pub fn is_ghosha(c: char) -> bool {
lazy_static! {
static ref GHOSHA: SoundSet = SoundSet::from("aAiIuUfFxXeEoOgGNjJYqQRdDnbBmyrlvh");
}
GHOSHA.contains(c)
static GHOSHA: OnceLock<SoundSet> = OnceLock::new();

let ghosha = GHOSHA.get_or_init(|| SoundSet::from("aAiIuUfFxXeEoOgGNjJYqQRdDnbBmyrlvh"));
ghosha.contains(c)
}

#[cfg(test)]
Expand Down
1 change: 0 additions & 1 deletion vidyut-prakriya/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions vidyut-prakriya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ rust-version = "1.68"

[dependencies]
enumset = { version = "1.1.3", features = ["serde"] }
lazy_static = "1.4.0"
rustc-hash = { workspace = true }
serde = { version = "1.0.150", features = ["derive"] }
wasm-bindgen = { version = "0.2", features = ["serde-serialize"]}
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = "0.1.7"

Expand Down
12 changes: 7 additions & 5 deletions vidyut-prakriya/src/angasya/abhyasasya.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ abhyasasya
Runs rules that modify the abhyāsa.
*/

use std::sync::OnceLock;

use crate::args::Agama as A;
use crate::args::Agama;
use crate::args::Aupadeshika;
Expand All @@ -23,7 +25,6 @@ use crate::dhatu_gana as gana;
use crate::it_samjna;
use crate::sounds as al;
use crate::sounds::{map, s, Map, Set, AC, HAL};
use lazy_static::lazy_static;

const AA: Set = s(&["a"]);
const ANUNASIKA: Set = s(&["Yam"]);
Expand All @@ -33,9 +34,7 @@ const KHAY: Set = s(&["Kay"]);
const F_HAL: Set = s(&["f hal"]);
const PU_YAN_J: Set = s(&["pu~", "yaR", "j"]);

lazy_static! {
static ref KUH_CU: Map = map("ku~ h", "cu~");
}
static KUH_CU: OnceLock<Map> = OnceLock::new();

/// Simplifies the abhyasa per 7.4.60.
fn try_haladi(text: &str) -> TermString {
Expand Down Expand Up @@ -269,7 +268,10 @@ fn try_general_rules(p: &mut Prakriya, i: usize) -> Option<()> {

let abhyasa = p.get(i)?;
let dhatu = p.get(i_dhatu)?;
if let Some(val) = KUH_CU.get(abhyasa.adi()?) {
if let Some(val) = KUH_CU
.get_or_init(|| map("ku~ h", "cu~"))
.get(abhyasa.adi()?)
{
let n = p.get(i_dhatu + 1)?;
if dhatu.has_u("ku\\N") && dhatu.has_gana(Gana::Bhvadi) && n.is(S::yaN) {
p.step("7.4.63");
Expand Down
8 changes: 3 additions & 5 deletions vidyut-prakriya/src/krt/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,13 @@ use crate::krt::utils::KrtPrakriya;
use crate::sounds::{s, Set, AC, HAL, IK};
use crate::stem_gana::TYAD_ADI;
use crate::Rule::Varttika;
use lazy_static::lazy_static;
use std::sync::OnceLock;

const II: Set = s(&["i"]);
const UU: Set = s(&["u"]);
const PU: Set = s(&["pu~"]);

lazy_static! {
static ref EMPTY_TERM: Term = Term::make_text("");
}
static EMPTY_TERM: OnceLock<Term> = OnceLock::new();

/// Tries to add various pratyayas that are just "a."
fn try_add_various_pratyayas(kp: &mut KrtPrakriya) {
Expand Down Expand Up @@ -563,7 +561,7 @@ fn try_add_upapada_krt(kp: &mut KrtPrakriya) -> Option<bool> {

let upapada = match kp.p.get_if(0, |t| t.has_tag(T::Pratipadika)) {
Some(t) => t,
None => &EMPTY_TERM,
None => &EMPTY_TERM.get_or_init(|| Term::make_text("")),
};
let upapade = kp.p.has(0, |t| t.has_tag(T::Pratipadika));

Expand Down
12 changes: 5 additions & 7 deletions vidyut-prakriya/src/sounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ We chose SLP1 over something like [WX][wx] merely because we have more familiari
[slp1]: https://en.wikipedia.org/wiki/SLP1
[wx]: https://en.wikipedia.org/wiki/WX_notation
*/
use lazy_static::lazy_static;
use rustc_hash::FxHashMap;
use std::fmt;
use std::{fmt, sync::OnceLock};

type Sound = char;

Expand All @@ -49,9 +48,7 @@ pub const HAL: Set = s(&["hal"]);
pub const YAN: Set = s(&["yaR"]);
pub const VAL: Set = s(&["val"]);

lazy_static! {
static ref SOUND_PROPS: FxHashMap<Sound, Uccarana> = create_sound_props();
}
static SOUND_PROPS: OnceLock<FxHashMap<Sound, Uccarana>> = OnceLock::new();

/// A set of Sanskrit sounds.
///
Expand Down Expand Up @@ -636,17 +633,18 @@ pub const fn savarna(c: Sound) -> Set {
pub(crate) fn map(keys: &str, values: &str) -> Map {
let keys = s_old(keys);
let values = s_old(values);
let sound_props = SOUND_PROPS.get_or_init(|| create_sound_props());

let mut map = Map::new();
for key in keys.to_string().chars() {
let key_props = SOUND_PROPS.get(&key).expect("called statically");
let key_props = sound_props.get(&key).expect("called statically");

// The best sound has the minimal distance.
let best_value = values
.to_string()
.chars()
.min_by_key(|v| {
SOUND_PROPS
sound_props
.get(v)
.expect("called statically")
.distance(key_props)
Expand Down
Loading

0 comments on commit 66a9315

Please sign in to comment.