diff --git a/rust/Cargo.lock b/rust/Cargo.lock index d5c457f5..3b11604a 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -13,7 +13,6 @@ dependencies = [ "getset", "indexmap 2.9.0", "itertools 0.14.0", - "lazy_static", "parameterized", "pyo3", "rayon", @@ -274,12 +273,6 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" -[[package]] -name = "lazy_static" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" - [[package]] name = "libc" version = "0.2.172" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 1b346c8a..c9ca82aa 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -13,7 +13,6 @@ bimap = "0.6.3" slotmap = "1.0.7" getset = "0.1.3" derive-new = "0.7.0" -lazy_static = "1.5.0" string-interner = "0.18.0" thiserror = "2.0.11" itertools = "0.14.0" diff --git a/rust/src/filesystem.rs b/rust/src/filesystem.rs index 844c6ad3..1ae4b987 100644 --- a/rust/src/filesystem.rs +++ b/rust/src/filesystem.rs @@ -1,3 +1,4 @@ +use itertools::Itertools; use pyo3::exceptions::{PyFileNotFoundError, PyUnicodeDecodeError}; use pyo3::prelude::*; use regex::Regex; @@ -5,16 +6,16 @@ use std::collections::HashMap; use std::ffi::OsStr; use std::fs; use std::path::{Path, PathBuf}; +use std::sync::LazyLock; use unindent::unindent; -use lazy_static::lazy_static; -lazy_static! { - static ref ENCODING_RE: Regex = Regex::new(r"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)").unwrap(); -} +static ENCODING_RE: LazyLock = LazyLock::new(|| { + Regex::new(r"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)").unwrap() +}); pub trait FileSystem: Send + Sync { - fn sep(&self) -> String; + fn sep(&self) -> &str; fn join(&self, components: Vec) -> String; @@ -37,8 +38,8 @@ pub struct PyRealBasicFileSystem { } impl FileSystem for RealBasicFileSystem { - fn sep(&self) -> String { - std::path::MAIN_SEPARATOR.to_string() + fn sep(&self) -> &str { + std::path::MAIN_SEPARATOR_STR } fn join(&self, components: Vec) -> String { @@ -46,7 +47,7 @@ impl FileSystem for RealBasicFileSystem { for component in components { path.push(component); } - path.to_str().unwrap().to_string() + path.to_str().expect("Path components should be valid unicode").to_string() } fn split(&self, file_name: &str) -> (String, String) { @@ -65,8 +66,8 @@ impl FileSystem for RealBasicFileSystem { }; ( - head.to_str().unwrap().to_string(), - tail.to_str().unwrap().to_string(), + head.to_str().expect("Path components should be valid unicode").to_string(), + tail.to_str().expect("Path components should be valid unicode").to_string(), ) } @@ -136,7 +137,7 @@ impl PyRealBasicFileSystem { } #[getter] - fn sep(&self) -> String { + fn sep(&self) -> &str { self.inner.sep() } @@ -191,17 +192,16 @@ impl FakeBasicFileSystem { } impl FileSystem for FakeBasicFileSystem { - fn sep(&self) -> String { - "/".to_string() + fn sep(&self) -> &str { + "/" } fn join(&self, components: Vec) -> String { let sep = self.sep(); components .into_iter() - .map(|c| c.trim_end_matches(&sep).to_string()) - .collect::>() - .join(&sep) + .map(|c| c.trim_end_matches(sep).to_string()) + .join(sep) } fn split(&self, file_name: &str) -> (String, String) { @@ -217,8 +217,8 @@ impl FileSystem for FakeBasicFileSystem { tail = path.file_name().unwrap_or(OsStr::new("")); } ( - head.to_str().unwrap().to_string(), - tail.to_str().unwrap().to_string(), + head.to_str().expect("Path components should be valid unicode").to_string(), + tail.to_str().expect("Path components should be valid unicode").to_string(), ) } @@ -230,7 +230,7 @@ impl FileSystem for FakeBasicFileSystem { fn read(&self, file_name: &str) -> PyResult { match self.contents.get(file_name) { Some(file_name) => Ok(file_name.clone()), - None => Err(PyFileNotFoundError::new_err("")), + None => Err(PyFileNotFoundError::new_err(format!("No such file: {file_name}"))), } } } @@ -246,7 +246,7 @@ impl PyFakeBasicFileSystem { } #[getter] - fn sep(&self) -> String { + fn sep(&self) -> &str { self.inner.sep() } @@ -288,7 +288,8 @@ pub fn parse_indented_file_system_string(file_system_string: &str) -> HashMap = buffer.split('\n').collect(); - for line_raw in lines.clone() { + let first_line_starts_with_slash = lines[0].trim().starts_with('/'); + for line_raw in lines { let line = line_raw.trim_end(); // Remove trailing whitespace if line.is_empty() { continue; // Skip empty lines @@ -331,7 +332,7 @@ pub fn parse_indented_file_system_string(file_system_string: &str) -> HashMap HashMap> = - RwLock::new(StringInterner::default()); - static ref IMPORT_LINE_CONTENTS: RwLock> = - RwLock::new(StringInterner::default()); - static ref EMPTY_MODULE_TOKENS: FxHashSet = FxHashSet::default(); - static ref EMPTY_IMPORT_DETAILS: FxHashSet = FxHashSet::default(); - static ref EMPTY_IMPORTS: FxHashSet<(ModuleToken, ModuleToken)> = FxHashSet::default(); -} +static MODULE_NAMES: LazyLock>> = LazyLock::new(|| { + RwLock::new(StringInterner::default()) +}); +static IMPORT_LINE_CONTENTS: LazyLock>> = LazyLock::new(|| { + RwLock::new(StringInterner::default()) +}); +static EMPTY_MODULE_TOKENS: LazyLock> = LazyLock::new(|| { + FxHashSet::default() +}); +static EMPTY_IMPORT_DETAILS: LazyLock> = LazyLock::new(|| { + FxHashSet::default() +}); new_key_type! { pub struct ModuleToken; } diff --git a/rust/src/module_expressions.rs b/rust/src/module_expressions.rs index df1b02b0..6e2a1687 100644 --- a/rust/src/module_expressions.rs +++ b/rust/src/module_expressions.rs @@ -1,15 +1,14 @@ use crate::errors::{GrimpError, GrimpResult}; use const_format::formatcp; use itertools::Itertools; -use lazy_static::lazy_static; use regex::Regex; use std::fmt::Display; use std::str::FromStr; +use std::sync::LazyLock; -lazy_static! { - static ref MODULE_EXPRESSION_PATTERN: Regex = - Regex::new(r"^(\w+|\*{1,2})(\.(\w+|\*{1,2}))*$").unwrap(); -} +static MODULE_EXPRESSION_PATTERN: LazyLock = LazyLock::new(|| { + Regex::new(r"^(\w+|\*{1,2})(\.(\w+|\*{1,2}))*$").unwrap() +}); /// A module expression is used to refer to sets of modules. ///