Skip to content

Commit

Permalink
Anki: Replace once_cell with stabilized LazyCell / LazyLock as far as…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
twwn committed Sep 27, 2024
1 parent e9b7268 commit abaa795
Show file tree
Hide file tree
Showing 32 changed files with 101 additions and 107 deletions.
5 changes: 0 additions & 5 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion build/ninja_gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ globset.workspace = true
itertools.workspace = true
maplit.workspace = true
num_cpus.workspace = true
once_cell.workspace = true
walkdir.workspace = true
which.workspace = true
4 changes: 2 additions & 2 deletions build/ninja_gen/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

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

use camino::Utf8PathBuf;
use once_cell::sync::Lazy;

#[derive(Debug, Clone, Hash, Default)]
pub enum BuildInput {
Expand Down Expand Up @@ -119,7 +119,7 @@ pub struct Glob {
pub exclude: Option<String>,
}

static CACHED_FILES: Lazy<Vec<Utf8PathBuf>> = Lazy::new(|| 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
once_cell.workspace = true
regex.workspace = true
serde_json.workspace = true
snafu.workspace = true
Expand Down
17 changes: 9 additions & 8 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 once_cell::sync::Lazy;
use regex::Regex;
use walkdir::DirEntry;
use walkdir::WalkDir;
Expand Down Expand Up @@ -144,7 +144,8 @@ fn extract_nested_messages_and_terms(
ftl_roots: &[impl AsRef<str>],
used_ftls: &mut HashSet<String>,
) {
static REFERENCE: Lazy<Regex> = Lazy::new(|| 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 @@ -196,12 +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) {
static SNAKECASE_TR: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\Wtr\s*\.([0-9a-z_]+)\W").unwrap());
static CAMELCASE_TR: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\Wtr2?\.([0-9A-Za-z_]+)\W").unwrap());
static DESIGNER_STYLE_TR: Lazy<Regex> =
Lazy::new(|| 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/linkchecker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ anki.workspace = true
futures.workspace = true
itertools.workspace = true
linkcheck.workspace = true
once_cell.workspace = true
regex.workspace = true
reqwest.workspace = true
strum.workspace = true
Expand Down
6 changes: 2 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 once_cell::sync::Lazy;
use linkcheck::validation::check_web;
use linkcheck::validation::Context;
use linkcheck::validation::Reason;
Expand Down Expand Up @@ -70,9 +70,7 @@ impl From<&'static str> for CheckableUrl {
}

fn ts_help_pages() -> impl Iterator<Item = &'static str> {
static QUOTED_URL: Lazy<Regex> = Lazy::new(|| {
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"))
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
8 changes: 4 additions & 4 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 once_cell::sync::Lazy;
use rusqlite::ToSql;
use serde::Deserialize;

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

type SequenceNumber = i32;

static HASHMAP: Lazy<Mutex<HashMap<CollectionId, HashMap<SequenceNumber, DbResponse>>>> =
Lazy::new(|| 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,7 +244,7 @@ pub(crate) fn next_sequence_number() -> i32 {

// same as we get from
// io.requery.android.database.CursorWindow.sCursorWindowSize
static DB_COMMAND_PAGE_SIZE: Lazy<Mutex<usize>> = Lazy::new(|| Mutex::new(1024 * 1024 * 2));
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
5 changes: 3 additions & 2 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 once_cell::sync::Lazy;
use std::sync::LazyLock;

use regex::Regex;
use reqwest::Client;
use serde;
Expand Down Expand Up @@ -31,7 +32,7 @@ pub async fn ankihub_login<S: Into<String>>(
client: Client,
) -> Result<LoginResponse> {
let client = HttpAnkiHubClient::new("", client);
static EMAIL_RE: Lazy<Regex> = Lazy::new(|| {
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 {
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
4 changes: 2 additions & 2 deletions rslib/src/cloze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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;
Expand All @@ -14,7 +15,6 @@ use nom::bytes::complete::tag;
use nom::bytes::complete::take_while;
use nom::combinator::map;
use nom::IResult;
use once_cell::sync::Lazy;
use regex::Captures;
use regex::Regex;

Expand All @@ -24,7 +24,7 @@ use crate::latex::contains_latex;
use crate::template::RenderContext;
use crate::text::strip_html_preserving_entities;

static MATHJAX: Lazy<Regex> = Lazy::new(|| {
static MATHJAX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"(?xsi)
(\\[(\[]) # 1 = mathjax opening tag
Expand Down
7 changes: 4 additions & 3 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 once_cell::sync::Lazy;
use regex::Regex;

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

fn strip_redundant_sections(text: &str) -> Cow<str> {
static RE: Lazy<Regex> = Lazy::new(|| {
static RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"(?isx)
<style>.*?</style> # style elements
Expand All @@ -170,7 +170,8 @@ fn strip_redundant_sections(text: &str) -> Cow<str> {
}

fn strip_answer_side_question(text: &str) -> Cow<str> {
static RE: Lazy<Regex> = Lazy::new(|| 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
6 changes: 3 additions & 3 deletions rslib/src/latex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html

use std::borrow::Cow;
use std::sync::LazyLock;

use once_cell::sync::Lazy;
use regex::Captures;
use regex::Regex;

use crate::cloze::expand_clozes_to_reveal_latex;
use crate::media::files::sha1_of_data;
use crate::text::strip_html;

pub(crate) static LATEX: Lazy<Regex> = Lazy::new(|| {
pub(crate) static LATEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"(?xsi)
\[latex\](.+?)\[/latex\] # 1 - standard latex
Expand All @@ -23,7 +23,7 @@ pub(crate) static LATEX: Lazy<Regex> = Lazy::new(|| {
)
.unwrap()
});
static LATEX_NEWLINES: Lazy<Regex> = Lazy::new(|| {
static LATEX_NEWLINES: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r#"(?xi)
<br( /)?>
Expand Down
6 changes: 3 additions & 3 deletions rslib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub mod undo;
pub mod version;

use std::env;
use std::sync::LazyLock;

use once_cell::sync::Lazy;

pub(crate) static PYTHON_UNIT_TESTS: Lazy<bool> = Lazy::new(|| env::var("ANKI_TEST_MODE").is_ok());
pub(crate) static PYTHON_UNIT_TESTS: LazyLock<bool> =
LazyLock::new(|| env::var("ANKI_TEST_MODE").is_ok());
4 changes: 2 additions & 2 deletions rslib/src/media/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use std::collections::HashMap;
use std::collections::HashSet;
use std::fs;
use std::io;
use std::sync::LazyLock;

use anki_i18n::without_unicode_isolation;
use anki_io::write_file;
use data_encoding::BASE64;
use once_cell::sync::Lazy;
use regex::Regex;
use tracing::debug;
use tracing::info;
Expand Down Expand Up @@ -459,7 +459,7 @@ impl MediaChecker<'_> {
}

fn maybe_extract_inline_image<'a>(&mut self, fname_decoded: &'a str) -> Result<Cow<'a, str>> {
static BASE64_IMG: Lazy<Regex> = Lazy::new(|| {
static BASE64_IMG: LazyLock<Regex> = LazyLock::new(|| {
Regex::new("(?i)^data:image/(jpg|jpeg|png|gif|webp|avif);base64,(.+)$").unwrap()
});

Expand Down
Loading

0 comments on commit abaa795

Please sign in to comment.