Skip to content

Commit

Permalink
lazy_static → once_cell → stabilized versions (#3447)
Browse files Browse the repository at this point in the history
* Anki: Replace lazy_static with once_cell

Unify to once_cell, lazy_static's replacement. The latter in unmaintained.

* Anki: Replace once_cell with stabilized LazyCell / LazyLock as far as possible

Since 1.80: rust-lang/rust#109736 and rust-lang/rust#98165

Non-Thread-Safe Lazy → std::cell::LazyCell https://doc.rust-lang.org/nightly/std/cell/struct.LazyCell.html

Thread-safe SyncLazy → std::sync::LazyLock https://doc.rust-lang.org/nightly/std/sync/struct.LazyLock.html

The compiler accepted LazyCell only in minilints.

The final use in rslib/src/log.rs couldn't be replaced since get_or_try_init has not yet been standardized: rust-lang/rust#109737

* Declare correct MSRV (dae)

Some of our deps require newer Rust versions, so this was misleading.

Updating the MSRV also allows us to use .inspect() on Option now
  • Loading branch information
twwn authored Sep 30, 2024
1 parent e2124cd commit d9969a9
Show file tree
Hide file tree
Showing 39 changed files with 202 additions and 222 deletions.
6 changes: 0 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version = "0.0.0"
authors = ["Ankitects Pty Ltd and contributors <https://help.ankiweb.net>"]
edition = "2021"
license = "AGPL-3.0-or-later"
rust-version = "1.65"
rust-version = "1.80"

[workspace]
members = [
Expand Down
1 change: 0 additions & 1 deletion build/ninja_gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ camino.workspace = true
dunce.workspace = true
globset.workspace = true
itertools.workspace = true
lazy_static.workspace = true
maplit.workspace = true
num_cpus.workspace = true
walkdir.workspace = true
Expand Down
5 changes: 2 additions & 3 deletions build/ninja_gen/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use std::collections::HashMap;
use std::fmt::Display;
use std::sync::LazyLock;

use camino::Utf8PathBuf;

Expand Down Expand Up @@ -118,9 +119,7 @@ pub struct Glob {
pub exclude: Option<String>,
}

lazy_static::lazy_static! {
static ref CACHED_FILES: Vec<Utf8PathBuf> = cache_files();
}
static CACHED_FILES: LazyLock<Vec<Utf8PathBuf>> = LazyLock::new(cache_files);

/// Walking the source tree once instead of for each glob yields ~4x speed
/// improvements.
Expand Down
1 change: 0 additions & 1 deletion ftl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ camino.workspace = true
clap.workspace = true
fluent-syntax.workspace = true
itertools.workspace = true
lazy_static.workspace = true
regex.workspace = true
serde_json.workspace = true
snafu.workspace = true
Expand Down
18 changes: 9 additions & 9 deletions ftl/src/garbage_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::fs;
use std::io::BufReader;
use std::iter::FromIterator;
use std::path::PathBuf;
use std::sync::LazyLock;

use anki_io::create_file;
use anyhow::Context;
Expand All @@ -14,7 +15,6 @@ use clap::Args;
use fluent_syntax::ast;
use fluent_syntax::ast::Resource;
use fluent_syntax::parser;
use lazy_static::lazy_static;
use regex::Regex;
use walkdir::DirEntry;
use walkdir::WalkDir;
Expand Down Expand Up @@ -144,9 +144,8 @@ fn extract_nested_messages_and_terms(
ftl_roots: &[impl AsRef<str>],
used_ftls: &mut HashSet<String>,
) {
lazy_static! {
static ref REFERENCE: Regex = Regex::new(r"\{\s*-?([-0-9a-z]+)\s*\}").unwrap();
}
static REFERENCE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\{\s*-?([-0-9a-z]+)\s*\}").unwrap());
for_files_with_ending(ftl_roots, ".ftl", |entry| {
let source = fs::read_to_string(entry.path()).expect("file not readable");
for caps in REFERENCE.captures_iter(&source) {
Expand Down Expand Up @@ -198,11 +197,12 @@ fn entry_use_check(used_ftls: &HashSet<String>) -> impl Fn(&ast::Entry<&str>) ->
}

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

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

Expand Down
1 change: 0 additions & 1 deletion rslib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ htmlescape.workspace = true
hyper.workspace = true
id_tree.workspace = true
itertools.workspace = true
lazy_static.workspace = true
nom.workspace = true
num_cpus.workspace = true
num_enum.workspace = true
Expand Down
1 change: 0 additions & 1 deletion rslib/linkchecker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ rust-version.workspace = true
anki.workspace = true
futures.workspace = true
itertools.workspace = true
lazy_static.workspace = true
linkcheck.workspace = true
regex.workspace = true
reqwest.workspace = true
Expand Down
7 changes: 3 additions & 4 deletions rslib/linkchecker/tests/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
use std::borrow::Cow;
use std::env;
use std::iter;
use std::sync::LazyLock;
use std::time::Duration;

use anki::links::help_page_link_suffix;
use anki::links::help_page_to_link;
use anki::links::HelpPage;
use futures::StreamExt;
use itertools::Itertools;
use lazy_static::lazy_static;
use linkcheck::validation::check_web;
use linkcheck::validation::Context;
use linkcheck::validation::Reason;
Expand Down Expand Up @@ -70,9 +70,8 @@ impl From<&'static str> for CheckableUrl {
}

fn ts_help_pages() -> impl Iterator<Item = &'static str> {
lazy_static! {
static ref QUOTED_URL: Regex = Regex::new("\"(http.+)\"").unwrap();
}
static QUOTED_URL: LazyLock<Regex> = LazyLock::new(|| Regex::new("\"(http.+)\"").unwrap());

QUOTED_URL
.captures_iter(include_str!("../../../ts/lib/tslib/help-page.ts"))
.map(|caps| caps.get(1).unwrap().as_str())
Expand Down
1 change: 0 additions & 1 deletion rslib/proto_gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ anyhow.workspace = true
camino.workspace = true
inflections.workspace = true
itertools.workspace = true
once_cell.workspace = true
prost-reflect.workspace = true
prost-types.workspace = true
regex.workspace = true
Expand Down
6 changes: 3 additions & 3 deletions rslib/proto_gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use std::collections::HashMap;
use std::env;
use std::path::PathBuf;
use std::sync::LazyLock;

use anki_io::read_to_string;
use anki_io::write_file_if_changed;
Expand All @@ -16,7 +17,6 @@ use camino::Utf8Path;
use inflections::Inflect;
use itertools::Either;
use itertools::Itertools;
use once_cell::sync::Lazy;
use prost_reflect::DescriptorPool;
use prost_reflect::MessageDescriptor;
use prost_reflect::MethodDescriptor;
Expand Down Expand Up @@ -238,8 +238,8 @@ pub fn add_must_use_annotations_to_file<E>(path: &Utf8Path, is_empty: E) -> Resu
where
E: Fn(&Utf8Path, &str) -> bool,
{
static MESSAGE_OR_ENUM_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"pub (struct|enum) ([[:alnum:]]+?)\s").unwrap());
static MESSAGE_OR_ENUM_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"pub (struct|enum) ([[:alnum:]]+?)\s").unwrap());
let contents = read_to_string(path)?;
let contents = MESSAGE_OR_ENUM_RE.replace_all(&contents, |caps: &Captures| {
let is_enum = caps.get(1).unwrap().as_str() == "enum";
Expand Down
15 changes: 6 additions & 9 deletions rslib/src/ankidroid/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::HashMap;
use std::mem::size_of;
use std::sync::atomic::AtomicI32;
use std::sync::atomic::Ordering;
use std::sync::LazyLock;
use std::sync::Mutex;

use anki_proto::ankidroid::sql_value::Data;
Expand All @@ -16,7 +17,6 @@ use itertools::FoldWhile;
use itertools::FoldWhile::Continue;
use itertools::FoldWhile::Done;
use itertools::Itertools;
use lazy_static::lazy_static;
use rusqlite::ToSql;
use serde::Deserialize;

Expand Down Expand Up @@ -110,10 +110,8 @@ fn select_slice_of_size<'a>(

type SequenceNumber = i32;

lazy_static! {
static ref HASHMAP: Mutex<HashMap<CollectionId, HashMap<SequenceNumber, DbResponse>>> =
Mutex::new(HashMap::new());
}
static HASHMAP: LazyLock<Mutex<HashMap<CollectionId, HashMap<SequenceNumber, DbResponse>>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));

pub(crate) fn flush_single_result(col: &Collection, sequence_number: i32) {
HASHMAP
Expand Down Expand Up @@ -244,10 +242,9 @@ pub(crate) fn next_sequence_number() -> i32 {
SEQUENCE_NUMBER.fetch_add(1, Ordering::SeqCst)
}

lazy_static! {
// same as we get from io.requery.android.database.CursorWindow.sCursorWindowSize
static ref DB_COMMAND_PAGE_SIZE: Mutex<usize> = Mutex::new(1024 * 1024 * 2);
}
// same as we get from
// io.requery.android.database.CursorWindow.sCursorWindowSize
static DB_COMMAND_PAGE_SIZE: LazyLock<Mutex<usize>> = LazyLock::new(|| Mutex::new(1024 * 1024 * 2));

pub(crate) fn set_max_page_size(size: usize) {
let mut state = DB_COMMAND_PAGE_SIZE.lock().expect("Could not lock mutex");
Expand Down
11 changes: 5 additions & 6 deletions rslib/src/ankihub/login.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html

use lazy_static::lazy_static;
use std::sync::LazyLock;

use regex::Regex;
use reqwest::Client;
use serde;
Expand Down Expand Up @@ -31,11 +32,9 @@ pub async fn ankihub_login<S: Into<String>>(
client: Client,
) -> Result<LoginResponse> {
let client = HttpAnkiHubClient::new("", client);
lazy_static! {
static ref EMAIL_RE: Regex =
Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
.unwrap();
}
static EMAIL_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap()
});
let mut request = LoginRequest {
username: None,
email: None,
Expand Down
6 changes: 3 additions & 3 deletions rslib/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ use std::ops::Deref;
use std::result;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::OnceLock;
use std::thread::JoinHandle;

use futures::future::AbortHandle;
use once_cell::sync::OnceCell;
use prost::Message;
use reqwest::Client;
use tokio::runtime;
Expand Down Expand Up @@ -53,7 +53,7 @@ pub struct BackendInner {
server: bool,
sync_abort: Mutex<Option<AbortHandle>>,
progress_state: Arc<Mutex<ProgressState>>,
runtime: OnceCell<Runtime>,
runtime: OnceLock<Runtime>,
state: Mutex<BackendState>,
backup_task: Mutex<Option<JoinHandle<Result<()>>>>,
media_sync_task: Mutex<Option<JoinHandle<Result<()>>>>,
Expand Down Expand Up @@ -88,7 +88,7 @@ impl Backend {
want_abort: false,
last_progress: None,
})),
runtime: OnceCell::new(),
runtime: OnceLock::new(),
state: Mutex::new(BackendState::default()),
backup_task: Mutex::new(None),
media_sync_task: Mutex::new(None),
Expand Down
12 changes: 6 additions & 6 deletions rslib/src/cloze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use std::borrow::Cow;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt::Write;
use std::sync::LazyLock;

use anki_proto::image_occlusion::get_image_occlusion_note_response::ImageOcclusion;
use anki_proto::image_occlusion::get_image_occlusion_note_response::ImageOcclusionShape;
use htmlescape::encode_attribute;
use lazy_static::lazy_static;
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::bytes::complete::take_while;
Expand All @@ -24,16 +24,16 @@ use crate::latex::contains_latex;
use crate::template::RenderContext;
use crate::text::strip_html_preserving_entities;

lazy_static! {
static ref MATHJAX: Regex = Regex::new(
static MATHJAX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"(?xsi)
(\\[(\[]) # 1 = mathjax opening tag
(.*?) # 2 = inner content
(\\[])]) # 3 = mathjax closing tag
"
",
)
.unwrap();
}
.unwrap()
});

mod mathjax_caps {
pub const OPENING_TAG: usize = 1;
Expand Down
17 changes: 8 additions & 9 deletions rslib/src/import_export/text/csv/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::sync::Arc;
use std::sync::LazyLock;

use anki_proto::import_export::ExportNoteCsvRequest;
use itertools::Itertools;
use lazy_static::lazy_static;
use regex::Regex;

use super::metadata::Delimiter;
Expand Down Expand Up @@ -156,23 +156,22 @@ fn field_to_record_field(field: &str, with_html: bool) -> Cow<str> {
}

fn strip_redundant_sections(text: &str) -> Cow<str> {
lazy_static! {
static ref RE: Regex = Regex::new(
static RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"(?isx)
<style>.*?</style> # style elements
|
\[\[type:[^]]+\]\] # type replacements
"
",
)
.unwrap();
}
.unwrap()
});
RE.replace_all(text.as_ref(), "")
}

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

Expand Down
Loading

0 comments on commit d9969a9

Please sign in to comment.