From e3517c5d478d4bf7be6a8339db3ff7913d17d248 Mon Sep 17 00:00:00 2001 From: Casper Meijn Date: Thu, 29 Feb 2024 11:34:05 +0100 Subject: [PATCH] WIP: chore: Replace once_cell dependency by std lib Open question: - Do we want to wait for LazyLock? https://github.com/rust-lang/rust/issues/109736 --- prost-build/Cargo.toml | 1 - prost-build/src/ast.rs | 14 ++++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/prost-build/Cargo.toml b/prost-build/Cargo.toml index 3cd4334f9..b0a6aef2a 100644 --- a/prost-build/Cargo.toml +++ b/prost-build/Cargo.toml @@ -30,7 +30,6 @@ petgraph = { version = "0.6", default-features = false } prost = { version = "0.12.6", path = "../prost", default-features = false } prost-types = { version = "0.12.6", path = "../prost-types", default-features = false } tempfile = "3" -once_cell = "1.17.1" regex = { version = "1.8.1", default-features = false, features = ["std", "unicode-bool"] } # feature: format diff --git a/prost-build/src/ast.rs b/prost-build/src/ast.rs index 9a6a0de99..bf882b6e7 100644 --- a/prost-build/src/ast.rs +++ b/prost-build/src/ast.rs @@ -1,8 +1,8 @@ -use once_cell::sync::Lazy; use prost_types::source_code_info::Location; #[cfg(feature = "cleanup-markdown")] use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag}; use regex::Regex; +use std::sync::OnceLock; /// Comments on a Protobuf item. #[derive(Debug, Default, Clone)] @@ -110,12 +110,14 @@ impl Comments { /// - escape urls as /// - escape `[` & `]` if not already escaped and not followed by a parenthesis or bracket fn sanitize_line(line: &str) -> String { - static RULE_URL: Lazy = Lazy::new(|| Regex::new(r"https?://[^\s)]+").unwrap()); - static RULE_BRACKETS: Lazy = - Lazy::new(|| Regex::new(r"(^|[^\]\\])\[(([^\]]*[^\\])?)\]([^(\[]|$)").unwrap()); + static RULE_URL: OnceLock = OnceLock::new(); + static RULE_BRACKETS: OnceLock = OnceLock::new(); + let rule_url = RULE_URL.get_or_init(|| Regex::new(r"https?://[^\s)]+").unwrap()); + let rule_brackets = RULE_BRACKETS + .get_or_init(|| Regex::new(r"(^|[^\]\\])\[(([^\]]*[^\\])?)\]([^(\[]|$)").unwrap()); - let mut s = RULE_URL.replace_all(line, r"<$0>").to_string(); - s = RULE_BRACKETS.replace_all(&s, r"$1\[$2\]$4").to_string(); + let mut s = rule_url.replace_all(line, r"<$0>").to_string(); + s = rule_brackets.replace_all(&s, r"$1\[$2\]$4").to_string(); if Self::should_indent(&s) { s.insert(0, ' '); }