From 623fc606a4e56cbe3794aca3b77406cffc1a8589 Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Sun, 11 May 2025 11:46:01 +0200 Subject: [PATCH] Replace `once_cell::sync::Lazy` with `std::sync::LazyLock` --- Cargo.lock | 1 - Cargo.toml | 1 - src/preprocess/index.rs | 5 ++--- src/preprocess/links.rs | 4 ++-- src/renderer/html_handlebars/hbs_renderer.rs | 21 +++++++++++--------- src/renderer/html_handlebars/search.rs | 4 ++-- src/renderer/html_handlebars/static_files.rs | 6 +++--- src/utils/mod.rs | 17 ++++++++-------- src/utils/string.rs | 10 +++++----- 9 files changed, 35 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index efcd85cd..591cc840 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1306,7 +1306,6 @@ dependencies = [ "memchr", "notify", "notify-debouncer-mini", - "once_cell", "opener", "pathdiff", "pretty_assertions", diff --git a/Cargo.toml b/Cargo.toml index 20458829..2907ad14 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,6 @@ anyhow = "1.0.71" chrono = { version = "0.4.24", default-features = false, features = ["clock"] } clap = { version = "4.3.12", features = ["cargo", "wrap_help"] } clap_complete = "4.3.2" -once_cell = "1.17.1" env_logger = "0.11.1" handlebars = "6.0" hex = "0.4.3" diff --git a/src/preprocess/index.rs b/src/preprocess/index.rs index 004b7eda..1e58e294 100644 --- a/src/preprocess/index.rs +++ b/src/preprocess/index.rs @@ -1,11 +1,10 @@ use regex::Regex; -use std::path::Path; +use std::{path::Path, sync::LazyLock}; use super::{Preprocessor, PreprocessorContext}; use crate::book::{Book, BookItem}; use crate::errors::*; use log::warn; -use once_cell::sync::Lazy; /// A preprocessor for converting file name `README.md` to `index.md` since /// `README.md` is the de facto index file in markdown-based documentation. @@ -68,7 +67,7 @@ fn warn_readme_name_conflict>(readme_path: P, index_path: P) { } fn is_readme_file>(path: P) -> bool { - static RE: Lazy = Lazy::new(|| Regex::new(r"(?i)^readme$").unwrap()); + static RE: LazyLock = LazyLock::new(|| Regex::new(r"(?i)^readme$").unwrap()); RE.is_match( path.as_ref() diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 1f55425d..951a3436 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -7,11 +7,11 @@ use regex::{CaptureMatches, Captures, Regex}; use std::fs; use std::ops::{Bound, Range, RangeBounds, RangeFrom, RangeFull, RangeTo}; use std::path::{Path, PathBuf}; +use std::sync::LazyLock; use super::{Preprocessor, PreprocessorContext}; use crate::book::{Book, BookItem}; use log::{error, warn}; -use once_cell::sync::Lazy; const ESCAPE_CHAR: char = '\\'; const MAX_LINK_NESTED_DEPTH: usize = 10; @@ -409,7 +409,7 @@ impl<'a> Iterator for LinkIter<'a> { fn find_links(contents: &str) -> LinkIter<'_> { // lazily compute following regex // r"\\\{\{#.*\}\}|\{\{#([a-zA-Z0-9]+)\s*([^}]+)\}\}")?; - static RE: Lazy = Lazy::new(|| { + static RE: LazyLock = LazyLock::new(|| { Regex::new( r"(?x) # insignificant whitespace mode \\\{\{\#.*\}\} # match escaped link diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index ffe083ad..155b0ffd 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -12,11 +12,11 @@ use std::collections::BTreeMap; use std::collections::HashMap; use std::fs::{self, File}; use std::path::{Path, PathBuf}; +use std::sync::LazyLock; use crate::utils::fs::get_404_output_file; use handlebars::Handlebars; use log::{debug, trace, warn}; -use once_cell::sync::Lazy; use regex::{Captures, Regex}; use serde_json::json; @@ -664,7 +664,7 @@ fn make_data( /// Goes through the rendered HTML, making sure all header tags have /// an anchor respectively so people can link to sections directly. fn build_header_links(html: &str) -> String { - static BUILD_HEADER_LINKS: Lazy = Lazy::new(|| { + static BUILD_HEADER_LINKS: LazyLock = LazyLock::new(|| { Regex::new(r#"(.*?)"#).unwrap() }); static IGNORE_CLASS: &[&str] = &["menu-title"]; @@ -725,8 +725,8 @@ fn insert_link_into_header( // ``` // This function replaces all commas by spaces in the code block classes fn fix_code_blocks(html: &str) -> String { - static FIX_CODE_BLOCKS: Lazy = - Lazy::new(|| Regex::new(r##"]+)class="([^"]+)"([^>]*)>"##).unwrap()); + static FIX_CODE_BLOCKS: LazyLock = + LazyLock::new(|| Regex::new(r##"]+)class="([^"]+)"([^>]*)>"##).unwrap()); FIX_CODE_BLOCKS .replace_all(html, |caps: &Captures<'_>| { @@ -739,8 +739,8 @@ fn fix_code_blocks(html: &str) -> String { .into_owned() } -static CODE_BLOCK_RE: Lazy = - Lazy::new(|| Regex::new(r##"((?s)]?class="([^"]+)".*?>(.*?))"##).unwrap()); +static CODE_BLOCK_RE: LazyLock = + LazyLock::new(|| Regex::new(r##"((?s)]?class="([^"]+)".*?>(.*?))"##).unwrap()); fn add_playground_pre( html: &str, @@ -808,8 +808,10 @@ fn add_playground_pre( /// Modifies all `` blocks to convert "hidden" lines and to wrap them in /// a ``. fn hide_lines(html: &str, code_config: &Code) -> String { - static LANGUAGE_REGEX: Lazy = Lazy::new(|| Regex::new(r"\blanguage-(\w+)\b").unwrap()); - static HIDELINES_REGEX: Lazy = Lazy::new(|| Regex::new(r"\bhidelines=(\S+)").unwrap()); + static LANGUAGE_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"\blanguage-(\w+)\b").unwrap()); + static HIDELINES_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"\bhidelines=(\S+)").unwrap()); CODE_BLOCK_RE .replace_all(html, |caps: &Captures<'_>| { @@ -850,7 +852,8 @@ fn hide_lines(html: &str, code_config: &Code) -> String { } fn hide_lines_rust(content: &str) -> String { - static BORING_LINES_REGEX: Lazy = Lazy::new(|| Regex::new(r"^(\s*)#(.?)(.*)$").unwrap()); + static BORING_LINES_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r"^(\s*)#(.?)(.*)$").unwrap()); let mut result = String::with_capacity(content.len()); let mut lines = content.lines().peekable(); diff --git a/src/renderer/html_handlebars/search.rs b/src/renderer/html_handlebars/search.rs index fe2edd5b..01ef44ac 100644 --- a/src/renderer/html_handlebars/search.rs +++ b/src/renderer/html_handlebars/search.rs @@ -1,9 +1,9 @@ use std::borrow::Cow; use std::collections::{HashMap, HashSet}; use std::path::{Path, PathBuf}; +use std::sync::LazyLock; use elasticlunr::{Index, IndexBuilder}; -use once_cell::sync::Lazy; use pulldown_cmark::*; use crate::book::{Book, BookItem, Chapter}; @@ -314,7 +314,7 @@ fn write_to_json(index: Index, search_config: &Search, doc_urls: Vec) -> } fn clean_html(html: &str) -> String { - static AMMONIA: Lazy> = Lazy::new(|| { + static AMMONIA: LazyLock> = LazyLock::new(|| { let mut clean_content = HashSet::new(); clean_content.insert("script"); clean_content.insert("style"); diff --git a/src/renderer/html_handlebars/static_files.rs b/src/renderer/html_handlebars/static_files.rs index 1ecf2d5e..e1531f42 100644 --- a/src/renderer/html_handlebars/static_files.rs +++ b/src/renderer/html_handlebars/static_files.rs @@ -1,7 +1,6 @@ //! Support for writing static files. use log::{debug, warn}; -use once_cell::sync::Lazy; use crate::config::HtmlConfig; use crate::errors::*; @@ -13,6 +12,7 @@ use std::borrow::Cow; use std::collections::HashMap; use std::fs::{self, File}; use std::path::{Path, PathBuf}; +use std::sync::LazyLock; /// Map static files to their final names and contents. /// @@ -231,8 +231,8 @@ impl StaticFiles { use regex::bytes::{Captures, Regex}; // The `{{ resource "name" }}` directive in static resources look like // handlebars syntax, even if they technically aren't. - static RESOURCE: Lazy = - Lazy::new(|| Regex::new(r#"\{\{ resource "([^"]+)" \}\}"#).unwrap()); + static RESOURCE: LazyLock = + LazyLock::new(|| Regex::new(r#"\{\{ resource "([^"]+)" \}\}"#).unwrap()); fn replace_all<'a>( hash_map: &HashMap, data: &'a [u8], diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 0ca0482f..597f0ea4 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -5,7 +5,6 @@ mod string; pub(crate) mod toml_ext; use crate::errors::Error; use log::error; -use once_cell::sync::Lazy; use pulldown_cmark::{html, CodeBlockKind, CowStr, Event, Options, Parser, Tag, TagEnd}; use regex::Regex; @@ -13,6 +12,7 @@ use std::borrow::Cow; use std::collections::HashMap; use std::fmt::Write; use std::path::Path; +use std::sync::LazyLock; pub use self::string::{ take_anchored_lines, take_lines, take_rustdoc_include_anchored_lines, @@ -21,7 +21,7 @@ pub use self::string::{ /// Replaces multiple consecutive whitespace characters with a single space character. pub fn collapse_whitespace(text: &str) -> Cow<'_, str> { - static RE: Lazy = Lazy::new(|| Regex::new(r"\s\s+").unwrap()); + static RE: LazyLock = LazyLock::new(|| Regex::new(r"\s\s+").unwrap()); RE.replace_all(text, " ") } @@ -50,7 +50,7 @@ pub fn id_from_content(content: &str) -> String { let mut content = content.to_string(); // Skip any tags or html-encoded stuff - static HTML: Lazy = Lazy::new(|| Regex::new(r"(<.*?>)").unwrap()); + static HTML: LazyLock = LazyLock::new(|| Regex::new(r"(<.*?>)").unwrap()); content = HTML.replace_all(&content, "").into(); const REPL_SUB: &[&str] = &["<", ">", "&", "'", """]; for sub in REPL_SUB { @@ -93,9 +93,10 @@ pub fn unique_id_from_content(content: &str, id_counter: &mut HashMap(event: Event<'a>, path: Option<&Path>) -> Event<'a> { - static SCHEME_LINK: Lazy = Lazy::new(|| Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap()); - static MD_LINK: Lazy = - Lazy::new(|| Regex::new(r"(?P.*)\.md(?P#.*)?").unwrap()); + static SCHEME_LINK: LazyLock = + LazyLock::new(|| Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap()); + static MD_LINK: LazyLock = + LazyLock::new(|| Regex::new(r"(?P.*)\.md(?P#.*)?").unwrap()); fn fix<'a>(dest: CowStr<'a>, path: Option<&Path>) -> CowStr<'a> { if dest.starts_with('#') { @@ -148,8 +149,8 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>) -> Event<'a> { // There are dozens of HTML tags/attributes that contain paths, so // feel free to add more tags if desired; these are the only ones I // care about right now. - static HTML_LINK: Lazy = - Lazy::new(|| Regex::new(r#"(<(?:a|img) [^>]*?(?:src|href)=")([^"]+?)""#).unwrap()); + static HTML_LINK: LazyLock = + LazyLock::new(|| Regex::new(r#"(<(?:a|img) [^>]*?(?:src|href)=")([^"]+?)""#).unwrap()); HTML_LINK .replace_all(&html, |caps: ®ex::Captures<'_>| { diff --git a/src/utils/string.rs b/src/utils/string.rs index 6dafe260..105bd104 100644 --- a/src/utils/string.rs +++ b/src/utils/string.rs @@ -1,7 +1,7 @@ -use once_cell::sync::Lazy; use regex::Regex; use std::ops::Bound::{Excluded, Included, Unbounded}; use std::ops::RangeBounds; +use std::sync::LazyLock; /// Take a range of lines from a string. pub fn take_lines>(s: &str, range: R) -> String { @@ -24,10 +24,10 @@ pub fn take_lines>(s: &str, range: R) -> String { } } -static ANCHOR_START: Lazy = - Lazy::new(|| Regex::new(r"ANCHOR:\s*(?P[\w_-]+)").unwrap()); -static ANCHOR_END: Lazy = - Lazy::new(|| Regex::new(r"ANCHOR_END:\s*(?P[\w_-]+)").unwrap()); +static ANCHOR_START: LazyLock = + LazyLock::new(|| Regex::new(r"ANCHOR:\s*(?P[\w_-]+)").unwrap()); +static ANCHOR_END: LazyLock = + LazyLock::new(|| Regex::new(r"ANCHOR_END:\s*(?P[\w_-]+)").unwrap()); /// Take anchored lines from a string. /// Lines containing anchor are ignored.