Skip to content

Commit a0a8442

Browse files
committed
Remove direct dependencies on lazy_static, once_cell and byteorder
The functionality of all three crates is now available in the standard library.
1 parent 6c90ac8 commit a0a8442

24 files changed

+91
-130
lines changed

Cargo.lock

-8
Original file line numberDiff line numberDiff line change
@@ -2113,7 +2113,6 @@ dependencies = [
21132113
"fs-err",
21142114
"getopts",
21152115
"jsonpath_lib",
2116-
"once_cell",
21172116
"regex",
21182117
"serde_json",
21192118
"shlex",
@@ -2232,7 +2231,6 @@ name = "linkchecker"
22322231
version = "0.1.0"
22332232
dependencies = [
22342233
"html5ever",
2235-
"once_cell",
22362234
"regex",
22372235
]
22382236

@@ -2491,7 +2489,6 @@ dependencies = [
24912489
"directories",
24922490
"getrandom",
24932491
"jemalloc-sys",
2494-
"lazy_static",
24952492
"libc",
24962493
"libffi",
24972494
"libloading",
@@ -4791,12 +4788,10 @@ dependencies = [
47914788
"arrayvec",
47924789
"askama",
47934790
"base64",
4794-
"byteorder",
47954791
"expect-test",
47964792
"indexmap",
47974793
"itertools 0.12.1",
47984794
"minifier",
4799-
"once_cell",
48004795
"regex",
48014796
"rustdoc-json-types",
48024797
"serde",
@@ -4889,7 +4884,6 @@ dependencies = [
48894884
"getopts",
48904885
"ignore",
48914886
"itertools 0.11.0",
4892-
"lazy_static",
48934887
"regex",
48944888
"rustfmt-config_proc_macro",
48954889
"serde",
@@ -5351,7 +5345,6 @@ version = "0.1.0"
53515345
dependencies = [
53525346
"build_helper",
53535347
"glob",
5354-
"once_cell",
53555348
]
53565349

53575350
[[package]]
@@ -5596,7 +5589,6 @@ version = "0.1.0"
55965589
dependencies = [
55975590
"cargo_metadata 0.15.4",
55985591
"ignore",
5599-
"lazy_static",
56005592
"miropt-test-tools",
56015593
"regex",
56025594
"rustc-hash",

src/librustdoc/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ path = "lib.rs"
1010
arrayvec = { version = "0.7", default-features = false }
1111
askama = { version = "0.12", default-features = false, features = ["config"] }
1212
base64 = "0.21.7"
13-
byteorder = "1.5"
1413
itertools = "0.12"
1514
indexmap = "2"
1615
minifier = "0.3.0"
17-
once_cell = "1.10.0"
1816
regex = "1"
1917
rustdoc-json-types = { path = "../rustdoc-json-types" }
2018
serde_json = "1.0"

src/librustdoc/html/markdown.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ use rustc_resolve::rustdoc::may_be_doc_link;
3535
use rustc_span::edition::Edition;
3636
use rustc_span::{Span, Symbol};
3737

38-
use once_cell::sync::Lazy;
3938
use std::borrow::Cow;
4039
use std::collections::VecDeque;
4140
use std::fmt::Write;
4241
use std::iter::Peekable;
4342
use std::ops::{ControlFlow, Range};
4443
use std::str::{self, CharIndices};
44+
use std::sync::OnceLock;
4545

4646
use crate::clean::RenderedLink;
4747
use crate::doctest;
@@ -1994,7 +1994,7 @@ pub struct IdMap {
19941994
}
19951995

19961996
// The map is pre-initialized and cloned each time to avoid reinitializing it repeatedly.
1997-
static DEFAULT_ID_MAP: Lazy<FxHashMap<Cow<'static, str>, usize>> = Lazy::new(|| init_id_map());
1997+
static DEFAULT_ID_MAP: OnceLock<FxHashMap<Cow<'static, str>, usize>> = OnceLock::new();
19981998

19991999
fn init_id_map() -> FxHashMap<Cow<'static, str>, usize> {
20002000
let mut map = FxHashMap::default();
@@ -2051,7 +2051,7 @@ fn init_id_map() -> FxHashMap<Cow<'static, str>, usize> {
20512051

20522052
impl IdMap {
20532053
pub fn new() -> Self {
2054-
IdMap { map: DEFAULT_ID_MAP.clone() }
2054+
IdMap { map: DEFAULT_ID_MAP.get_or_init(init_id_map).clone() }
20552055
}
20562056

20572057
pub(crate) fn derive<S: AsRef<str> + ToString>(&mut self, candidate: S) -> String {

src/librustdoc/html/render/search_index/encode.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -166,43 +166,42 @@ pub(crate) fn write_bitmap_to_bytes(
166166
containers.push(container);
167167
}
168168
// https://github.com/RoaringBitmap/RoaringFormatSpec
169-
use byteorder::{WriteBytesExt, LE};
170169
const SERIAL_COOKIE_NO_RUNCONTAINER: u32 = 12346;
171170
const SERIAL_COOKIE: u32 = 12347;
172171
const NO_OFFSET_THRESHOLD: u32 = 4;
173172
let size: u32 = containers.len().try_into().unwrap();
174173
let start_offset = if has_run {
175-
out.write_u32::<LE>(SERIAL_COOKIE | ((size - 1) << 16))?;
174+
out.write_all(&u32::to_le_bytes(SERIAL_COOKIE | ((size - 1) << 16)))?;
176175
for set in containers.chunks(8) {
177176
let mut b = 0;
178177
for (i, container) in set.iter().enumerate() {
179178
if matches!(container, &Container::Run(..)) {
180179
b |= 1 << i;
181180
}
182181
}
183-
out.write_u8(b)?;
182+
out.write_all(&[b])?;
184183
}
185184
if size < NO_OFFSET_THRESHOLD {
186185
4 + 4 * size + ((size + 7) / 8)
187186
} else {
188187
4 + 8 * size + ((size + 7) / 8)
189188
}
190189
} else {
191-
out.write_u32::<LE>(SERIAL_COOKIE_NO_RUNCONTAINER)?;
192-
out.write_u32::<LE>(containers.len().try_into().unwrap())?;
190+
out.write_all(&u32::to_le_bytes(SERIAL_COOKIE_NO_RUNCONTAINER))?;
191+
out.write_all(&u32::to_le_bytes(containers.len().try_into().unwrap()))?;
193192
4 + 4 + 4 * size + 4 * size
194193
};
195194
for (&key, container) in keys.iter().zip(&containers) {
196195
// descriptive header
197196
let key: u32 = key.into();
198197
let count: u32 = container.popcount() - 1;
199-
out.write_u32::<LE>((count << 16) | key)?;
198+
out.write_all(&u32::to_le_bytes((count << 16) | key))?;
200199
}
201200
if !has_run || size >= NO_OFFSET_THRESHOLD {
202201
// offset header
203202
let mut starting_offset = start_offset;
204203
for container in &containers {
205-
out.write_u32::<LE>(starting_offset)?;
204+
out.write_all(&u32::to_le_bytes(starting_offset))?;
206205
starting_offset += match container {
207206
Container::Bits(_) => 8192u32,
208207
Container::Array(array) => u32::try_from(array.len()).unwrap() * 2,
@@ -214,19 +213,19 @@ pub(crate) fn write_bitmap_to_bytes(
214213
match container {
215214
Container::Bits(bits) => {
216215
for chunk in bits.iter() {
217-
out.write_u64::<LE>(*chunk)?;
216+
out.write_all(&u64::to_le_bytes(*chunk))?;
218217
}
219218
}
220219
Container::Array(array) => {
221220
for value in array.iter() {
222-
out.write_u16::<LE>(*value)?;
221+
out.write_all(&u16::to_le_bytes(*value))?;
223222
}
224223
}
225224
Container::Run(runs) => {
226-
out.write_u16::<LE>((runs.len()).try_into().unwrap())?;
225+
out.write_all(&u16::to_le_bytes(runs.len().try_into().unwrap()))?;
227226
for (start, lenm1) in runs.iter().copied() {
228-
out.write_u16::<LE>(start)?;
229-
out.write_u16::<LE>(lenm1)?;
227+
out.write_all(&u16::to_le_bytes(start))?;
228+
out.write_all(&u16::to_le_bytes(lenm1))?;
230229
}
231230
}
232231
}

src/tools/jsondocck/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ regex = "1.4"
1010
shlex = "1.0"
1111
serde_json = "1.0"
1212
fs-err = "2.5.0"
13-
once_cell = "1.0"

src/tools/jsondocck/src/main.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use jsonpath_lib::select;
2-
use once_cell::sync::Lazy;
32
use regex::{Regex, RegexBuilder};
43
use serde_json::Value;
54
use std::borrow::Cow;
5+
use std::sync::OnceLock;
66
use std::{env, fmt, fs};
77

88
mod cache;
@@ -95,7 +95,8 @@ impl fmt::Display for CommandKind {
9595
}
9696
}
9797

98-
static LINE_PATTERN: Lazy<Regex> = Lazy::new(|| {
98+
static LINE_PATTERN: OnceLock<Regex> = OnceLock::new();
99+
fn line_pattern() -> Regex {
99100
RegexBuilder::new(
100101
r#"
101102
\s(?P<invalid>!?)@(?P<negated>!?)
@@ -107,7 +108,7 @@ static LINE_PATTERN: Lazy<Regex> = Lazy::new(|| {
107108
.unicode(true)
108109
.build()
109110
.unwrap()
110-
});
111+
}
111112

112113
fn print_err(msg: &str, lineno: usize) {
113114
eprintln!("Invalid command: {} on line {}", msg, lineno)
@@ -123,7 +124,7 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
123124
for (lineno, line) in file.split('\n').enumerate() {
124125
let lineno = lineno + 1;
125126

126-
let cap = match LINE_PATTERN.captures(line) {
127+
let cap = match LINE_PATTERN.get_or_init(line_pattern).captures(line) {
127128
Some(c) => c,
128129
None => continue,
129130
};

src/tools/linkchecker/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ path = "main.rs"
99

1010
[dependencies]
1111
regex = "1"
12-
once_cell = "1"
1312
html5ever = "0.26.0"

src/tools/linkchecker/main.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ use html5ever::tendril::ByteTendril;
1818
use html5ever::tokenizer::{
1919
BufferQueue, TagToken, Token, TokenSink, TokenSinkResult, Tokenizer, TokenizerOpts,
2020
};
21-
use once_cell::sync::Lazy;
22-
use regex::Regex;
2321
use std::cell::RefCell;
2422
use std::collections::{HashMap, HashSet};
2523
use std::env;
@@ -69,8 +67,12 @@ const INTRA_DOC_LINK_EXCEPTIONS: &[(&str, &[&str])] = &[
6967

7068
];
7169

72-
static BROKEN_INTRA_DOC_LINK: Lazy<Regex> =
73-
Lazy::new(|| Regex::new(r#"\[<code>(.*)</code>\]"#).unwrap());
70+
macro_rules! static_regex {
71+
($re:literal) => {{
72+
static RE: ::std::sync::OnceLock<::regex::Regex> = ::std::sync::OnceLock::new();
73+
RE.get_or_init(|| ::regex::Regex::new($re).unwrap())
74+
}};
75+
}
7476

7577
macro_rules! t {
7678
($e:expr) => {
@@ -373,7 +375,7 @@ impl Checker {
373375
// Search for intra-doc links that rustdoc didn't warn about
374376
// NOTE: only looks at one line at a time; in practice this should find most links
375377
for (i, line) in source.lines().enumerate() {
376-
for broken_link in BROKEN_INTRA_DOC_LINK.captures_iter(line) {
378+
for broken_link in static_regex!(r#"\[<code>(.*)</code>\]"#).captures_iter(line) {
377379
if is_intra_doc_exception(file, &broken_link[1]) {
378380
report.intra_doc_exceptions += 1;
379381
} else {

src/tools/miri/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ colored = "2"
4646
ui_test = "0.21.1"
4747
rustc_version = "0.4"
4848
regex = "1.5.5"
49-
lazy_static = "1.4.0"
5049
tempfile = "3"
5150

5251
[package.metadata.rust-analyzer]

src/tools/miri/tests/ui.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::ffi::OsString;
22
use std::num::NonZeroUsize;
33
use std::path::{Path, PathBuf};
4+
use std::sync::OnceLock;
45
use std::{env, process::Command};
56

67
use colored::*;
@@ -67,8 +68,8 @@ fn miri_config(target: &str, path: &str, mode: Mode, with_dependencies: bool) ->
6768

6869
let mut config = Config {
6970
target: Some(target.to_owned()),
70-
stderr_filters: STDERR.clone(),
71-
stdout_filters: STDOUT.clone(),
71+
stderr_filters: stderr_filters().into(),
72+
stdout_filters: stdout_filters().into(),
7273
mode,
7374
program,
7475
out_dir: PathBuf::from(std::env::var_os("CARGO_TARGET_DIR").unwrap()).join("ui"),
@@ -174,15 +175,18 @@ fn run_tests(
174175
}
175176

176177
macro_rules! regexes {
177-
($name:ident: $($regex:expr => $replacement:expr,)*) => {lazy_static::lazy_static! {
178-
static ref $name: Vec<(Match, &'static [u8])> = vec![
179-
$((Regex::new($regex).unwrap().into(), $replacement.as_bytes()),)*
180-
];
181-
}};
178+
($name:ident: $($regex:expr => $replacement:expr,)*) => {
179+
fn $name() -> &'static [(Match, &'static [u8])] {
180+
static S: OnceLock<Vec<(Match, &'static [u8])>> = OnceLock::new();
181+
S.get_or_init(|| vec![
182+
$((Regex::new($regex).unwrap().into(), $replacement.as_bytes()),)*
183+
])
184+
}
185+
};
182186
}
183187

184188
regexes! {
185-
STDOUT:
189+
stdout_filters:
186190
// Windows file paths
187191
r"\\" => "/",
188192
// erase borrow tags
@@ -191,7 +195,7 @@ regexes! {
191195
}
192196

193197
regexes! {
194-
STDERR:
198+
stderr_filters:
195199
// erase line and column info
196200
r"\.rs:[0-9]+:[0-9]+(: [0-9]+:[0-9]+)?" => ".rs:LL:CC",
197201
// erase alloc ids

src/tools/rustfmt/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ dirs = "4.0"
4444
getopts = "0.2"
4545
ignore = "0.4"
4646
itertools = "0.11"
47-
lazy_static = "1.4"
4847
regex = "1.7"
4948
serde = { version = "1.0.160", features = ["derive"] }
5049
serde_json = "1.0"

src/tools/rustfmt/src/comment.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
use std::{borrow::Cow, iter};
44

55
use itertools::{multipeek, MultiPeek};
6-
use lazy_static::lazy_static;
7-
use regex::Regex;
86
use rustc_span::Span;
97

108
use crate::config::Config;
@@ -17,17 +15,6 @@ use crate::utils::{
1715
};
1816
use crate::{ErrorKind, FormattingError};
1917

20-
lazy_static! {
21-
/// A regex matching reference doc links.
22-
///
23-
/// ```markdown
24-
/// /// An [example].
25-
/// ///
26-
/// /// [example]: this::is::a::link
27-
/// ```
28-
static ref REFERENCE_LINK_URL: Regex = Regex::new(r"^\[.+\]\s?:").unwrap();
29-
}
30-
3118
fn is_custom_comment(comment: &str) -> bool {
3219
if !comment.starts_with("//") {
3320
false
@@ -980,11 +967,16 @@ fn trim_custom_comment_prefix(s: &str) -> String {
980967
/// Returns `true` if the given string MAY include URLs or alike.
981968
fn has_url(s: &str) -> bool {
982969
// This function may return false positive, but should get its job done in most cases.
970+
// The regex is indended to capture text such as the below.
971+
//
972+
// /// An [example].
973+
// ///
974+
// /// [example]: this::is::a::link
983975
s.contains("https://")
984976
|| s.contains("http://")
985977
|| s.contains("ftp://")
986978
|| s.contains("file://")
987-
|| REFERENCE_LINK_URL.is_match(s)
979+
|| static_regex!(r"^\[.+\]\s?:").is_match(s)
988980
}
989981

990982
/// Returns true if the given string may be part of a Markdown table.

0 commit comments

Comments
 (0)