Skip to content

Commit d24c0ca

Browse files
authored
Merge pull request #2696 from paolobarbolini/once-cell-to-std
Replace `once_cell::sync::Lazy` with `std::sync::LazyLock`
2 parents a8aee21 + 623fc60 commit d24c0ca

File tree

9 files changed

+35
-34
lines changed

9 files changed

+35
-34
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ anyhow = "1.0.71"
3030
chrono = { version = "0.4.24", default-features = false, features = ["clock"] }
3131
clap = { version = "4.3.12", features = ["cargo", "wrap_help"] }
3232
clap_complete = "4.3.2"
33-
once_cell = "1.17.1"
3433
env_logger = "0.11.1"
3534
handlebars = "6.0"
3635
hex = "0.4.3"

src/preprocess/index.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use regex::Regex;
2-
use std::path::Path;
2+
use std::{path::Path, sync::LazyLock};
33

44
use super::{Preprocessor, PreprocessorContext};
55
use crate::book::{Book, BookItem};
66
use crate::errors::*;
77
use log::warn;
8-
use once_cell::sync::Lazy;
98

109
/// A preprocessor for converting file name `README.md` to `index.md` since
1110
/// `README.md` is the de facto index file in markdown-based documentation.
@@ -68,7 +67,7 @@ fn warn_readme_name_conflict<P: AsRef<Path>>(readme_path: P, index_path: P) {
6867
}
6968

7069
fn is_readme_file<P: AsRef<Path>>(path: P) -> bool {
71-
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)^readme$").unwrap());
70+
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)^readme$").unwrap());
7271

7372
RE.is_match(
7473
path.as_ref()

src/preprocess/links.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use regex::{CaptureMatches, Captures, Regex};
77
use std::fs;
88
use std::ops::{Bound, Range, RangeBounds, RangeFrom, RangeFull, RangeTo};
99
use std::path::{Path, PathBuf};
10+
use std::sync::LazyLock;
1011

1112
use super::{Preprocessor, PreprocessorContext};
1213
use crate::book::{Book, BookItem};
1314
use log::{error, warn};
14-
use once_cell::sync::Lazy;
1515

1616
const ESCAPE_CHAR: char = '\\';
1717
const MAX_LINK_NESTED_DEPTH: usize = 10;
@@ -409,7 +409,7 @@ impl<'a> Iterator for LinkIter<'a> {
409409
fn find_links(contents: &str) -> LinkIter<'_> {
410410
// lazily compute following regex
411411
// r"\\\{\{#.*\}\}|\{\{#([a-zA-Z0-9]+)\s*([^}]+)\}\}")?;
412-
static RE: Lazy<Regex> = Lazy::new(|| {
412+
static RE: LazyLock<Regex> = LazyLock::new(|| {
413413
Regex::new(
414414
r"(?x) # insignificant whitespace mode
415415
\\\{\{\#.*\}\} # match escaped link

src/renderer/html_handlebars/hbs_renderer.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ use std::collections::BTreeMap;
1212
use std::collections::HashMap;
1313
use std::fs::{self, File};
1414
use std::path::{Path, PathBuf};
15+
use std::sync::LazyLock;
1516

1617
use crate::utils::fs::get_404_output_file;
1718
use handlebars::Handlebars;
1819
use log::{debug, trace, warn};
19-
use once_cell::sync::Lazy;
2020
use regex::{Captures, Regex};
2121
use serde_json::json;
2222

@@ -664,7 +664,7 @@ fn make_data(
664664
/// Goes through the rendered HTML, making sure all header tags have
665665
/// an anchor respectively so people can link to sections directly.
666666
fn build_header_links(html: &str) -> String {
667-
static BUILD_HEADER_LINKS: Lazy<Regex> = Lazy::new(|| {
667+
static BUILD_HEADER_LINKS: LazyLock<Regex> = LazyLock::new(|| {
668668
Regex::new(r#"<h(\d)(?: id="([^"]+)")?(?: class="([^"]+)")?>(.*?)</h\d>"#).unwrap()
669669
});
670670
static IGNORE_CLASS: &[&str] = &["menu-title"];
@@ -725,8 +725,8 @@ fn insert_link_into_header(
725725
// ```
726726
// This function replaces all commas by spaces in the code block classes
727727
fn fix_code_blocks(html: &str) -> String {
728-
static FIX_CODE_BLOCKS: Lazy<Regex> =
729-
Lazy::new(|| Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap());
728+
static FIX_CODE_BLOCKS: LazyLock<Regex> =
729+
LazyLock::new(|| Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap());
730730

731731
FIX_CODE_BLOCKS
732732
.replace_all(html, |caps: &Captures<'_>| {
@@ -739,8 +739,8 @@ fn fix_code_blocks(html: &str) -> String {
739739
.into_owned()
740740
}
741741

742-
static CODE_BLOCK_RE: Lazy<Regex> =
743-
Lazy::new(|| Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap());
742+
static CODE_BLOCK_RE: LazyLock<Regex> =
743+
LazyLock::new(|| Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap());
744744

745745
fn add_playground_pre(
746746
html: &str,
@@ -808,8 +808,10 @@ fn add_playground_pre(
808808
/// Modifies all `<code>` blocks to convert "hidden" lines and to wrap them in
809809
/// a `<span class="boring">`.
810810
fn hide_lines(html: &str, code_config: &Code) -> String {
811-
static LANGUAGE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\blanguage-(\w+)\b").unwrap());
812-
static HIDELINES_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\bhidelines=(\S+)").unwrap());
811+
static LANGUAGE_REGEX: LazyLock<Regex> =
812+
LazyLock::new(|| Regex::new(r"\blanguage-(\w+)\b").unwrap());
813+
static HIDELINES_REGEX: LazyLock<Regex> =
814+
LazyLock::new(|| Regex::new(r"\bhidelines=(\S+)").unwrap());
813815

814816
CODE_BLOCK_RE
815817
.replace_all(html, |caps: &Captures<'_>| {
@@ -850,7 +852,8 @@ fn hide_lines(html: &str, code_config: &Code) -> String {
850852
}
851853

852854
fn hide_lines_rust(content: &str) -> String {
853-
static BORING_LINES_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\s*)#(.?)(.*)$").unwrap());
855+
static BORING_LINES_REGEX: LazyLock<Regex> =
856+
LazyLock::new(|| Regex::new(r"^(\s*)#(.?)(.*)$").unwrap());
854857

855858
let mut result = String::with_capacity(content.len());
856859
let mut lines = content.lines().peekable();

src/renderer/html_handlebars/search.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::borrow::Cow;
22
use std::collections::{HashMap, HashSet};
33
use std::path::{Path, PathBuf};
4+
use std::sync::LazyLock;
45

56
use elasticlunr::{Index, IndexBuilder};
6-
use once_cell::sync::Lazy;
77
use pulldown_cmark::*;
88

99
use crate::book::{Book, BookItem, Chapter};
@@ -314,7 +314,7 @@ fn write_to_json(index: Index, search_config: &Search, doc_urls: Vec<String>) ->
314314
}
315315

316316
fn clean_html(html: &str) -> String {
317-
static AMMONIA: Lazy<ammonia::Builder<'static>> = Lazy::new(|| {
317+
static AMMONIA: LazyLock<ammonia::Builder<'static>> = LazyLock::new(|| {
318318
let mut clean_content = HashSet::new();
319319
clean_content.insert("script");
320320
clean_content.insert("style");

src/renderer/html_handlebars/static_files.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Support for writing static files.
22
33
use log::{debug, warn};
4-
use once_cell::sync::Lazy;
54

65
use crate::config::HtmlConfig;
76
use crate::errors::*;
@@ -13,6 +12,7 @@ use std::borrow::Cow;
1312
use std::collections::HashMap;
1413
use std::fs::{self, File};
1514
use std::path::{Path, PathBuf};
15+
use std::sync::LazyLock;
1616

1717
/// Map static files to their final names and contents.
1818
///
@@ -231,8 +231,8 @@ impl StaticFiles {
231231
use regex::bytes::{Captures, Regex};
232232
// The `{{ resource "name" }}` directive in static resources look like
233233
// handlebars syntax, even if they technically aren't.
234-
static RESOURCE: Lazy<Regex> =
235-
Lazy::new(|| Regex::new(r#"\{\{ resource "([^"]+)" \}\}"#).unwrap());
234+
static RESOURCE: LazyLock<Regex> =
235+
LazyLock::new(|| Regex::new(r#"\{\{ resource "([^"]+)" \}\}"#).unwrap());
236236
fn replace_all<'a>(
237237
hash_map: &HashMap<String, String>,
238238
data: &'a [u8],

src/utils/mod.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ mod string;
55
pub(crate) mod toml_ext;
66
use crate::errors::Error;
77
use log::error;
8-
use once_cell::sync::Lazy;
98
use pulldown_cmark::{html, CodeBlockKind, CowStr, Event, Options, Parser, Tag, TagEnd};
109
use regex::Regex;
1110

1211
use std::borrow::Cow;
1312
use std::collections::HashMap;
1413
use std::fmt::Write;
1514
use std::path::Path;
15+
use std::sync::LazyLock;
1616

1717
pub use self::string::{
1818
take_anchored_lines, take_lines, take_rustdoc_include_anchored_lines,
@@ -21,7 +21,7 @@ pub use self::string::{
2121

2222
/// Replaces multiple consecutive whitespace characters with a single space character.
2323
pub fn collapse_whitespace(text: &str) -> Cow<'_, str> {
24-
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\s\s+").unwrap());
24+
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s\s+").unwrap());
2525
RE.replace_all(text, " ")
2626
}
2727

@@ -50,7 +50,7 @@ pub fn id_from_content(content: &str) -> String {
5050
let mut content = content.to_string();
5151

5252
// Skip any tags or html-encoded stuff
53-
static HTML: Lazy<Regex> = Lazy::new(|| Regex::new(r"(<.*?>)").unwrap());
53+
static HTML: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(<.*?>)").unwrap());
5454
content = HTML.replace_all(&content, "").into();
5555
const REPL_SUB: &[&str] = &["&lt;", "&gt;", "&amp;", "&#39;", "&quot;"];
5656
for sub in REPL_SUB {
@@ -93,9 +93,10 @@ pub fn unique_id_from_content(content: &str, id_counter: &mut HashMap<String, us
9393
/// None. Ideally, print page links would link to anchors on the print page,
9494
/// but that is very difficult.
9595
fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>) -> Event<'a> {
96-
static SCHEME_LINK: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap());
97-
static MD_LINK: Lazy<Regex> =
98-
Lazy::new(|| Regex::new(r"(?P<link>.*)\.md(?P<anchor>#.*)?").unwrap());
96+
static SCHEME_LINK: LazyLock<Regex> =
97+
LazyLock::new(|| Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap());
98+
static MD_LINK: LazyLock<Regex> =
99+
LazyLock::new(|| Regex::new(r"(?P<link>.*)\.md(?P<anchor>#.*)?").unwrap());
99100

100101
fn fix<'a>(dest: CowStr<'a>, path: Option<&Path>) -> CowStr<'a> {
101102
if dest.starts_with('#') {
@@ -148,8 +149,8 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>) -> Event<'a> {
148149
// There are dozens of HTML tags/attributes that contain paths, so
149150
// feel free to add more tags if desired; these are the only ones I
150151
// care about right now.
151-
static HTML_LINK: Lazy<Regex> =
152-
Lazy::new(|| Regex::new(r#"(<(?:a|img) [^>]*?(?:src|href)=")([^"]+?)""#).unwrap());
152+
static HTML_LINK: LazyLock<Regex> =
153+
LazyLock::new(|| Regex::new(r#"(<(?:a|img) [^>]*?(?:src|href)=")([^"]+?)""#).unwrap());
153154

154155
HTML_LINK
155156
.replace_all(&html, |caps: &regex::Captures<'_>| {

src/utils/string.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use once_cell::sync::Lazy;
21
use regex::Regex;
32
use std::ops::Bound::{Excluded, Included, Unbounded};
43
use std::ops::RangeBounds;
4+
use std::sync::LazyLock;
55

66
/// Take a range of lines from a string.
77
pub fn take_lines<R: RangeBounds<usize>>(s: &str, range: R) -> String {
@@ -24,10 +24,10 @@ pub fn take_lines<R: RangeBounds<usize>>(s: &str, range: R) -> String {
2424
}
2525
}
2626

27-
static ANCHOR_START: Lazy<Regex> =
28-
Lazy::new(|| Regex::new(r"ANCHOR:\s*(?P<anchor_name>[\w_-]+)").unwrap());
29-
static ANCHOR_END: Lazy<Regex> =
30-
Lazy::new(|| Regex::new(r"ANCHOR_END:\s*(?P<anchor_name>[\w_-]+)").unwrap());
27+
static ANCHOR_START: LazyLock<Regex> =
28+
LazyLock::new(|| Regex::new(r"ANCHOR:\s*(?P<anchor_name>[\w_-]+)").unwrap());
29+
static ANCHOR_END: LazyLock<Regex> =
30+
LazyLock::new(|| Regex::new(r"ANCHOR_END:\s*(?P<anchor_name>[\w_-]+)").unwrap());
3131

3232
/// Take anchored lines from a string.
3333
/// Lines containing anchor are ignored.

0 commit comments

Comments
 (0)