Skip to content

Commit 6d9b3bd

Browse files
committed
Anki: Replace lazy_static with once_cell
Unify to once_cell, lazy_static's replacement. The latter in unmaintained, anyway.
1 parent f3b0afc commit 6d9b3bd

File tree

24 files changed

+175
-184
lines changed

24 files changed

+175
-184
lines changed

Cargo.lock

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/ninja_gen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ camino.workspace = true
1414
dunce.workspace = true
1515
globset.workspace = true
1616
itertools.workspace = true
17-
lazy_static.workspace = true
1817
maplit.workspace = true
1918
num_cpus.workspace = true
19+
once_cell.workspace = true
2020
walkdir.workspace = true
2121
which.workspace = true

build/ninja_gen/src/input.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::collections::HashMap;
55
use std::fmt::Display;
66

77
use camino::Utf8PathBuf;
8+
use once_cell::sync::Lazy;
89

910
#[derive(Debug, Clone, Hash, Default)]
1011
pub enum BuildInput {
@@ -118,9 +119,7 @@ pub struct Glob {
118119
pub exclude: Option<String>,
119120
}
120121

121-
lazy_static::lazy_static! {
122-
static ref CACHED_FILES: Vec<Utf8PathBuf> = cache_files();
123-
}
122+
static CACHED_FILES: Lazy<Vec<Utf8PathBuf>> = Lazy::new(|| cache_files());
124123

125124
/// Walking the source tree once instead of for each glob yields ~4x speed
126125
/// improvements.

ftl/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ camino.workspace = true
1616
clap.workspace = true
1717
fluent-syntax.workspace = true
1818
itertools.workspace = true
19-
lazy_static.workspace = true
19+
once_cell.workspace = true
2020
regex.workspace = true
2121
serde_json.workspace = true
2222
snafu.workspace = true

ftl/src/garbage_collection.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use clap::Args;
1414
use fluent_syntax::ast;
1515
use fluent_syntax::ast::Resource;
1616
use fluent_syntax::parser;
17-
use lazy_static::lazy_static;
17+
use once_cell::sync::Lazy;
1818
use regex::Regex;
1919
use walkdir::DirEntry;
2020
use walkdir::WalkDir;
@@ -144,9 +144,7 @@ fn extract_nested_messages_and_terms(
144144
ftl_roots: &[impl AsRef<str>],
145145
used_ftls: &mut HashSet<String>,
146146
) {
147-
lazy_static! {
148-
static ref REFERENCE: Regex = Regex::new(r"\{\s*-?([-0-9a-z]+)\s*\}").unwrap();
149-
}
147+
static REFERENCE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\{\s*-?([-0-9a-z]+)\s*\}").unwrap());
150148
for_files_with_ending(ftl_roots, ".ftl", |entry| {
151149
let source = fs::read_to_string(entry.path()).expect("file not readable");
152150
for caps in REFERENCE.captures_iter(&source) {
@@ -198,11 +196,12 @@ fn entry_use_check(used_ftls: &HashSet<String>) -> impl Fn(&ast::Entry<&str>) ->
198196
}
199197

200198
fn extract_references_from_file(refs: &mut HashSet<String>, entry: &DirEntry) {
201-
lazy_static! {
202-
static ref SNAKECASE_TR: Regex = Regex::new(r"\Wtr\s*\.([0-9a-z_]+)\W").unwrap();
203-
static ref CAMELCASE_TR: Regex = Regex::new(r"\Wtr2?\.([0-9A-Za-z_]+)\W").unwrap();
204-
static ref DESIGNER_STYLE_TR: Regex = Regex::new(r"<string>([0-9a-z_]+)</string>").unwrap();
205-
}
199+
static SNAKECASE_TR: Lazy<Regex> =
200+
Lazy::new(|| Regex::new(r"\Wtr\s*\.([0-9a-z_]+)\W").unwrap());
201+
static CAMELCASE_TR: Lazy<Regex> =
202+
Lazy::new(|| Regex::new(r"\Wtr2?\.([0-9A-Za-z_]+)\W").unwrap());
203+
static DESIGNER_STYLE_TR: Lazy<Regex> =
204+
Lazy::new(|| Regex::new(r"<string>([0-9a-z_]+)</string>").unwrap());
206205

207206
let file_name = entry.file_name().to_str().expect("non-unicode filename");
208207

rslib/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ htmlescape.workspace = true
6969
hyper.workspace = true
7070
id_tree.workspace = true
7171
itertools.workspace = true
72-
lazy_static.workspace = true
7372
nom.workspace = true
7473
num_cpus.workspace = true
7574
num_enum.workspace = true

rslib/linkchecker/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ rust-version.workspace = true
1111
anki.workspace = true
1212
futures.workspace = true
1313
itertools.workspace = true
14-
lazy_static.workspace = true
1514
linkcheck.workspace = true
15+
once_cell.workspace = true
1616
regex.workspace = true
1717
reqwest.workspace = true
1818
strum.workspace = true

rslib/linkchecker/tests/links.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use anki::links::help_page_to_link;
1313
use anki::links::HelpPage;
1414
use futures::StreamExt;
1515
use itertools::Itertools;
16-
use lazy_static::lazy_static;
16+
use once_cell::sync::Lazy;
1717
use linkcheck::validation::check_web;
1818
use linkcheck::validation::Context;
1919
use linkcheck::validation::Reason;
@@ -70,9 +70,10 @@ impl From<&'static str> for CheckableUrl {
7070
}
7171

7272
fn ts_help_pages() -> impl Iterator<Item = &'static str> {
73-
lazy_static! {
74-
static ref QUOTED_URL: Regex = Regex::new("\"(http.+)\"").unwrap();
75-
}
73+
static QUOTED_URL: Lazy<Regex> = Lazy::new(|| {
74+
Regex::new("\"(http.+)\"").unwrap()
75+
});
76+
7677
QUOTED_URL
7778
.captures_iter(include_str!("../../../ts/lib/tslib/help-page.ts"))
7879
.map(|caps| caps.get(1).unwrap().as_str())

rslib/src/ankidroid/db.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use itertools::FoldWhile;
1616
use itertools::FoldWhile::Continue;
1717
use itertools::FoldWhile::Done;
1818
use itertools::Itertools;
19-
use lazy_static::lazy_static;
19+
use once_cell::sync::Lazy;
2020
use rusqlite::ToSql;
2121
use serde::Deserialize;
2222

@@ -110,10 +110,8 @@ fn select_slice_of_size<'a>(
110110

111111
type SequenceNumber = i32;
112112

113-
lazy_static! {
114-
static ref HASHMAP: Mutex<HashMap<CollectionId, HashMap<SequenceNumber, DbResponse>>> =
115-
Mutex::new(HashMap::new());
116-
}
113+
static HASHMAP: Lazy<Mutex<HashMap<CollectionId, HashMap<SequenceNumber, DbResponse>>>> =
114+
Lazy::new(|| Mutex::new(HashMap::new()));
117115

118116
pub(crate) fn flush_single_result(col: &Collection, sequence_number: i32) {
119117
HASHMAP
@@ -244,10 +242,9 @@ pub(crate) fn next_sequence_number() -> i32 {
244242
SEQUENCE_NUMBER.fetch_add(1, Ordering::SeqCst)
245243
}
246244

247-
lazy_static! {
248-
// same as we get from io.requery.android.database.CursorWindow.sCursorWindowSize
249-
static ref DB_COMMAND_PAGE_SIZE: Mutex<usize> = Mutex::new(1024 * 1024 * 2);
250-
}
245+
// same as we get from
246+
// io.requery.android.database.CursorWindow.sCursorWindowSize
247+
static DB_COMMAND_PAGE_SIZE: Lazy<Mutex<usize>> = Lazy::new(|| Mutex::new(1024 * 1024 * 2));
251248

252249
pub(crate) fn set_max_page_size(size: usize) {
253250
let mut state = DB_COMMAND_PAGE_SIZE.lock().expect("Could not lock mutex");

rslib/src/ankihub/login.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright: Ankitects Pty Ltd and contributors
22
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
33

4-
use lazy_static::lazy_static;
4+
use once_cell::sync::Lazy;
55
use regex::Regex;
66
use reqwest::Client;
77
use serde;
@@ -31,11 +31,9 @@ pub async fn ankihub_login<S: Into<String>>(
3131
client: Client,
3232
) -> Result<LoginResponse> {
3333
let client = HttpAnkiHubClient::new("", client);
34-
lazy_static! {
35-
static ref EMAIL_RE: Regex =
36-
Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
37-
.unwrap();
38-
}
34+
static EMAIL_RE: Lazy<Regex> = Lazy::new(|| {
35+
Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap()
36+
});
3937
let mut request = LoginRequest {
4038
username: None,
4139
email: None,

rslib/src/cloze.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ use std::fmt::Write;
99
use anki_proto::image_occlusion::get_image_occlusion_note_response::ImageOcclusion;
1010
use anki_proto::image_occlusion::get_image_occlusion_note_response::ImageOcclusionShape;
1111
use htmlescape::encode_attribute;
12-
use lazy_static::lazy_static;
1312
use nom::branch::alt;
1413
use nom::bytes::complete::tag;
1514
use nom::bytes::complete::take_while;
1615
use nom::combinator::map;
1716
use nom::IResult;
17+
use once_cell::sync::Lazy;
1818
use regex::Captures;
1919
use regex::Regex;
2020

@@ -24,16 +24,16 @@ use crate::latex::contains_latex;
2424
use crate::template::RenderContext;
2525
use crate::text::strip_html_preserving_entities;
2626

27-
lazy_static! {
28-
static ref MATHJAX: Regex = Regex::new(
27+
static MATHJAX: Lazy<Regex> = Lazy::new(|| {
28+
Regex::new(
2929
r"(?xsi)
3030
(\\[(\[]) # 1 = mathjax opening tag
3131
(.*?) # 2 = inner content
3232
(\\[])]) # 3 = mathjax closing tag
33-
"
33+
",
3434
)
35-
.unwrap();
36-
}
35+
.unwrap()
36+
});
3737

3838
mod mathjax_caps {
3939
pub const OPENING_TAG: usize = 1;

rslib/src/import_export/text/csv/export.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::sync::Arc;
99

1010
use anki_proto::import_export::ExportNoteCsvRequest;
1111
use itertools::Itertools;
12-
use lazy_static::lazy_static;
12+
use once_cell::sync::Lazy;
1313
use regex::Regex;
1414

1515
use super::metadata::Delimiter;
@@ -156,23 +156,21 @@ fn field_to_record_field(field: &str, with_html: bool) -> Cow<str> {
156156
}
157157

158158
fn strip_redundant_sections(text: &str) -> Cow<str> {
159-
lazy_static! {
160-
static ref RE: Regex = Regex::new(
159+
static RE: Lazy<Regex> = Lazy::new(|| {
160+
Regex::new(
161161
r"(?isx)
162162
<style>.*?</style> # style elements
163163
|
164164
\[\[type:[^]]+\]\] # type replacements
165-
"
165+
",
166166
)
167-
.unwrap();
168-
}
167+
.unwrap()
168+
});
169169
RE.replace_all(text.as_ref(), "")
170170
}
171171

172172
fn strip_answer_side_question(text: &str) -> Cow<str> {
173-
lazy_static! {
174-
static ref RE: Regex = Regex::new(r"(?is)^.*<hr id=answer>\n*").unwrap();
175-
}
173+
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?is)^.*<hr id=answer>\n*").unwrap());
176174
RE.replace_all(text.as_ref(), "")
177175
}
178176

rslib/src/latex.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,36 @@
33

44
use std::borrow::Cow;
55

6-
use lazy_static::lazy_static;
6+
use once_cell::sync::Lazy;
77
use regex::Captures;
88
use regex::Regex;
99

1010
use crate::cloze::expand_clozes_to_reveal_latex;
1111
use crate::media::files::sha1_of_data;
1212
use crate::text::strip_html;
1313

14-
lazy_static! {
15-
pub(crate) static ref LATEX: Regex = Regex::new(
14+
pub(crate) static LATEX: Lazy<Regex> = Lazy::new(|| {
15+
Regex::new(
1616
r"(?xsi)
1717
\[latex\](.+?)\[/latex\] # 1 - standard latex
1818
|
1919
\[\$\](.+?)\[/\$\] # 2 - inline math
2020
|
2121
\[\$\$\](.+?)\[/\$\$\] # 3 - math environment
22-
"
22+
",
2323
)
24-
.unwrap();
25-
static ref LATEX_NEWLINES: Regex = Regex::new(
24+
.unwrap()
25+
});
26+
static LATEX_NEWLINES: Lazy<Regex> = Lazy::new(|| {
27+
Regex::new(
2628
r#"(?xi)
2729
<br( /)?>
2830
|
2931
<div>
30-
"#
32+
"#,
3133
)
32-
.unwrap();
33-
}
34+
.unwrap()
35+
});
3436

3537
pub(crate) fn contains_latex(text: &str) -> bool {
3638
LATEX.is_match(text)

rslib/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ pub mod version;
5353

5454
use std::env;
5555

56-
use lazy_static::lazy_static;
56+
use once_cell::sync::Lazy;
5757

58-
lazy_static! {
59-
pub(crate) static ref PYTHON_UNIT_TESTS: bool = env::var("ANKI_TEST_MODE").is_ok();
60-
}
58+
pub(crate) static PYTHON_UNIT_TESTS: Lazy<bool> = Lazy::new(|| env::var("ANKI_TEST_MODE").is_ok());

0 commit comments

Comments
 (0)