Skip to content

Commit e5b79bf

Browse files
committed
refactor: replace once_cell::sync::Lazy with std::sync::LazyLock
1 parent cfaa8ce commit e5b79bf

20 files changed

+137
-140
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ mime = "0.3.17"
7272
num_cpus = "1.16"
7373
num-derive = "0.4"
7474
num-traits = { workspace = true }
75-
once_cell = { workspace = true }
7675
parking_lot = "0.12"
7776
percent-encoding = "2.3"
7877
pgp = { version = "0.15.0", default-features = false }
@@ -185,7 +184,6 @@ log = "0.4"
185184
mailparse = "0.16.1"
186185
nu-ansi-term = "0.46"
187186
num-traits = "0.2"
188-
once_cell = "1.21.3"
189187
rand = "0.8"
190188
regex = "1.10"
191189
rusqlite = "0.32"

deltachat-contact-tools/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ license = "MPL-2.0"
99

1010
[dependencies]
1111
anyhow = { workspace = true }
12-
once_cell = { workspace = true }
1312
regex = { workspace = true }
1413
rusqlite = { workspace = true } # Needed in order to `impl rusqlite::types::ToSql for EmailAddress`. Could easily be put behind a feature.
1514
chrono = { workspace = true, features = ["alloc", "clock", "std"] }

deltachat-contact-tools/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929

3030
use std::fmt;
3131
use std::ops::Deref;
32+
use std::sync::LazyLock;
3233

3334
use anyhow::bail;
3435
use anyhow::Context as _;
3536
use anyhow::Result;
3637
use chrono::{DateTime, NaiveDateTime};
37-
use once_cell::sync::Lazy;
3838
use regex::Regex;
3939

4040
#[derive(Debug)]
@@ -155,7 +155,8 @@ pub fn parse_vcard(vcard: &str) -> Vec<VcardContact> {
155155
}
156156

157157
// Remove line folding, see https://datatracker.ietf.org/doc/html/rfc6350#section-3.2
158-
static NEWLINE_AND_SPACE_OR_TAB: Lazy<Regex> = Lazy::new(|| Regex::new("\r?\n[\t ]").unwrap());
158+
static NEWLINE_AND_SPACE_OR_TAB: LazyLock<Regex> =
159+
LazyLock::new(|| Regex::new("\r?\n[\t ]").unwrap());
159160
let unfolded_lines = NEWLINE_AND_SPACE_OR_TAB.replace_all(vcard, "");
160161

161162
let mut lines = unfolded_lines.lines().peekable();
@@ -276,7 +277,8 @@ impl rusqlite::types::ToSql for ContactAddress {
276277
/// - Removes special characters from the name, see [`sanitize_name()`]
277278
/// - Removes the name if it is equal to the address by setting it to ""
278279
pub fn sanitize_name_and_addr(name: &str, addr: &str) -> (String, String) {
279-
static ADDR_WITH_NAME_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new("(.*)<(.*)>").unwrap());
280+
static ADDR_WITH_NAME_REGEX: LazyLock<Regex> =
281+
LazyLock::new(|| Regex::new("(.*)<(.*)>").unwrap());
280282
let (name, addr) = if let Some(captures) = ADDR_WITH_NAME_REGEX.captures(addr.as_ref()) {
281283
(
282284
if name.is_empty() {

deltachat-ffi/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ tokio = { workspace = true, features = ["rt-multi-thread"] }
2424
anyhow = { workspace = true }
2525
thiserror = { workspace = true }
2626
rand = { workspace = true }
27-
once_cell = { workspace = true }
2827
yerpc = { workspace = true, features = ["anyhow_expose"] }
2928

3029
[features]

deltachat-ffi/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::future::Future;
1818
use std::ops::Deref;
1919
use std::ptr;
2020
use std::str::FromStr;
21-
use std::sync::Arc;
21+
use std::sync::{Arc, LazyLock};
2222
use std::time::{Duration, SystemTime};
2323

2424
use anyhow::Context as _;
@@ -38,7 +38,6 @@ use deltachat::{accounts::Accounts, log::LogExt};
3838
use deltachat_jsonrpc::api::CommandApi;
3939
use deltachat_jsonrpc::yerpc::{OutReceiver, RpcClient, RpcSession};
4040
use num_traits::{FromPrimitive, ToPrimitive};
41-
use once_cell::sync::Lazy;
4241
use rand::Rng;
4342
use tokio::runtime::Runtime;
4443
use tokio::sync::RwLock;
@@ -68,7 +67,8 @@ const DC_GCM_INFO_ONLY: u32 = 0x02;
6867
/// Struct representing the deltachat context.
6968
pub type dc_context_t = Context;
7069

71-
static RT: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("unable to create tokio runtime"));
70+
static RT: LazyLock<Runtime> =
71+
LazyLock::new(|| Runtime::new().expect("unable to create tokio runtime"));
7272

7373
fn block_on<T>(fut: T) -> T::Output
7474
where

scripts/create-provider-data-rs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def process_dir(dir):
215215
" Config, ConfigDefault, Oauth2Authorizer, Provider, ProviderOptions, Server, Status,\n"
216216
"};\n"
217217
"use std::collections::HashMap;\n\n"
218-
"use once_cell::sync::Lazy;\n\n"
218+
"use std::sync::LazyLock;\n\n"
219219
)
220220

221221
process_dir(Path(sys.argv[1]))
@@ -224,7 +224,7 @@ def process_dir(dir):
224224
out_all += out_domains
225225
out_all += "];\n\n"
226226

227-
out_all += "pub(crate) static PROVIDER_IDS: Lazy<HashMap<&'static str, &'static Provider>> = Lazy::new(|| HashMap::from([\n"
227+
out_all += "pub(crate) static PROVIDER_IDS: LazyLock<HashMap<&'static str, &'static Provider>> = LazyLock::new(|| HashMap::from([\n"
228228
out_all += out_ids
229229
out_all += "]));\n\n"
230230

@@ -233,8 +233,8 @@ def process_dir(dir):
233233
else:
234234
now = datetime.datetime.fromisoformat(sys.argv[2])
235235
out_all += (
236-
"pub static _PROVIDER_UPDATED: Lazy<chrono::NaiveDate> = "
237-
"Lazy::new(|| chrono::NaiveDate::from_ymd_opt("
236+
"pub static _PROVIDER_UPDATED: LazyLock<chrono::NaiveDate> = "
237+
"LazyLock::new(|| chrono::NaiveDate::from_ymd_opt("
238238
+ str(now.year)
239239
+ ", "
240240
+ str(now.month)

src/authres.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
use std::borrow::Cow;
55
use std::collections::BTreeSet;
66
use std::fmt;
7+
use std::sync::LazyLock;
78

89
use anyhow::Result;
910
use deltachat_contact_tools::EmailAddress;
1011
use mailparse::MailHeaderMap;
1112
use mailparse::ParsedMail;
12-
use once_cell::sync::Lazy;
1313

1414
use crate::config::Config;
1515
use crate::context::Context;
@@ -107,7 +107,8 @@ fn remove_comments(header: &str) -> Cow<'_, str> {
107107
// In Pomsky, this is:
108108
// "(" Codepoint* lazy ")"
109109
// See https://playground.pomsky-lang.org/?text=%22(%22%20Codepoint*%20lazy%20%22)%22
110-
static RE: Lazy<regex::Regex> = Lazy::new(|| regex::Regex::new(r"\([\s\S]*?\)").unwrap());
110+
static RE: LazyLock<regex::Regex> =
111+
LazyLock::new(|| regex::Regex::new(r"\([\s\S]*?\)").unwrap());
111112

112113
RE.replace_all(header, " ")
113114
}

src/chatlist.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! # Chat list module.
22
33
use anyhow::{ensure, Context as _, Result};
4-
use once_cell::sync::Lazy;
4+
use std::sync::LazyLock;
55

66
use crate::chat::{update_special_chat_names, Chat, ChatId, ChatVisibility};
77
use crate::constants::{
@@ -17,8 +17,8 @@ use crate::summary::Summary;
1717
use crate::tools::IsNoneOrEmpty;
1818

1919
/// Regex to find out if a query should filter by unread messages.
20-
pub static IS_UNREAD_FILTER: Lazy<regex::Regex> =
21-
Lazy::new(|| regex::Regex::new(r"\bis:unread\b").unwrap());
20+
pub static IS_UNREAD_FILTER: LazyLock<regex::Regex> =
21+
LazyLock::new(|| regex::Regex::new(r"\bis:unread\b").unwrap());
2222

2323
/// An object representing a single chatlist in memory.
2424
///

src/constants.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
33
#![allow(missing_docs)]
44

5+
use std::sync::LazyLock;
6+
57
use deltachat_derive::{FromSql, ToSql};
6-
use once_cell::sync::Lazy;
78
use percent_encoding::{AsciiSet, NON_ALPHANUMERIC};
89
use serde::{Deserialize, Serialize};
910

1011
use crate::chat::ChatId;
1112

12-
pub static DC_VERSION_STR: Lazy<String> = Lazy::new(|| env!("CARGO_PKG_VERSION").to_string());
13+
pub static DC_VERSION_STR: LazyLock<String> =
14+
LazyLock::new(|| env!("CARGO_PKG_VERSION").to_string());
1315

1416
/// Set of characters to percent-encode in email addresses and names.
1517
pub(crate) const NON_ALPHANUMERIC_WITHOUT_DOT: &AsciiSet = &NON_ALPHANUMERIC.remove(b'.');

src/dehtml.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
//! A module to remove HTML tags from the email text
44
55
use std::io::BufRead;
6+
use std::sync::LazyLock;
67

7-
use once_cell::sync::Lazy;
88
use quick_xml::{
99
events::{BytesEnd, BytesStart, BytesText},
1010
Reader,
@@ -176,7 +176,8 @@ fn dehtml_quick_xml(buf: &str) -> (String, String) {
176176
}
177177

178178
fn dehtml_text_cb(event: &BytesText, dehtml: &mut Dehtml) {
179-
static LINE_RE: Lazy<regex::Regex> = Lazy::new(|| regex::Regex::new(r"(\r?\n)+").unwrap());
179+
static LINE_RE: LazyLock<regex::Regex> =
180+
LazyLock::new(|| regex::Regex::new(r"(\r?\n)+").unwrap());
180181

181182
if dehtml.get_add_text() == AddText::YesPreserveLineEnds
182183
|| dehtml.get_add_text() == AddText::YesRemoveLineEnds

src/key.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,15 +456,13 @@ impl std::str::FromStr for Fingerprint {
456456

457457
#[cfg(test)]
458458
mod tests {
459-
use std::sync::Arc;
460-
461-
use once_cell::sync::Lazy;
459+
use std::sync::{Arc, LazyLock};
462460

463461
use super::*;
464462
use crate::config::Config;
465463
use crate::test_utils::{alice_keypair, TestContext};
466464

467-
static KEYPAIR: Lazy<KeyPair> = Lazy::new(alice_keypair);
465+
static KEYPAIR: LazyLock<KeyPair> = LazyLock::new(alice_keypair);
468466

469467
#[test]
470468
fn test_from_armored_string() {

src/net/dns.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ use anyhow::{Context as _, Result};
4444
use std::collections::HashMap;
4545
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
4646
use std::str::FromStr;
47+
use std::sync::LazyLock;
4748
use tokio::net::lookup_host;
4849
use tokio::time::timeout;
4950

5051
use super::load_connection_timestamp;
5152
use crate::context::Context;
5253
use crate::tools::time;
53-
use once_cell::sync::Lazy;
5454

5555
/// Inserts entry into DNS cache
5656
/// or updates existing one with a new timestamp.
@@ -90,8 +90,8 @@ pub(crate) async fn prune_dns_cache(context: &Context) -> Result<()> {
9090
/// <https://docs.rs/tokio/1.40.0/tokio/sync/struct.Mutex.html#which-kind-of-mutex-should-you-use>
9191
/// and
9292
/// <https://stackoverflow.com/questions/63712823/why-do-i-get-a-deadlock-when-using-tokio-with-a-stdsyncmutex>.
93-
static LOOKUP_HOST_CACHE: Lazy<parking_lot::RwLock<HashMap<String, Vec<IpAddr>>>> =
94-
Lazy::new(Default::default);
93+
static LOOKUP_HOST_CACHE: LazyLock<parking_lot::RwLock<HashMap<String, Vec<IpAddr>>>> =
94+
LazyLock::new(Default::default);
9595

9696
/// Wrapper for `lookup_host` that returns IP addresses.
9797
async fn lookup_ips(host: impl tokio::net::ToSocketAddrs) -> Result<impl Iterator<Item = IpAddr>> {
@@ -229,7 +229,7 @@ pub(crate) async fn update_connect_timestamp(
229229
///
230230
/// See <https://support.delta.chat/t/no-dns-resolution-result/2778> and
231231
/// <https://github.com/deltachat/deltachat-core-rust/issues/4920> for reasons.
232-
static DNS_PRELOAD: Lazy<HashMap<&'static str, Vec<IpAddr>>> = Lazy::new(|| {
232+
static DNS_PRELOAD: LazyLock<HashMap<&'static str, Vec<IpAddr>>> = LazyLock::new(|| {
233233
HashMap::from([
234234
(
235235
"mail.sangham.net",

src/pgp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ pub async fn symm_decrypt<T: std::io::Read + std::io::Seek>(
424424

425425
#[cfg(test)]
426426
mod tests {
427-
use once_cell::sync::Lazy;
427+
use std::sync::LazyLock;
428428
use tokio::sync::OnceCell;
429429

430430
use super::*;
@@ -502,7 +502,7 @@ mod tests {
502502
static CLEARTEXT: &[u8] = b"This is a test";
503503

504504
/// Initialised [TestKeys] for tests.
505-
static KEYS: Lazy<TestKeys> = Lazy::new(TestKeys::new);
505+
static KEYS: LazyLock<TestKeys> = LazyLock::new(TestKeys::new);
506506

507507
static CTEXT_SIGNED: OnceCell<String> = OnceCell::const_new();
508508
static CTEXT_UNSIGNED: OnceCell<String> = OnceCell::const_new();

src/plaintext.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Handle plain text together with some attributes.
22
3-
use once_cell::sync::Lazy;
3+
use std::sync::LazyLock;
44

55
use crate::simplify::remove_message_footer;
66

@@ -25,10 +25,10 @@ impl PlainText {
2525
/// Convert plain text to HTML.
2626
/// The function handles quotes, links, fixed and floating text paragraphs.
2727
pub fn to_html(&self) -> String {
28-
static LINKIFY_MAIL_RE: Lazy<regex::Regex> =
29-
Lazy::new(|| regex::Regex::new(r"\b([\w.\-+]+@[\w.\-]+)\b").unwrap());
28+
static LINKIFY_MAIL_RE: LazyLock<regex::Regex> =
29+
LazyLock::new(|| regex::Regex::new(r"\b([\w.\-+]+@[\w.\-]+)\b").unwrap());
3030

31-
static LINKIFY_URL_RE: Lazy<regex::Regex> = Lazy::new(|| {
31+
static LINKIFY_URL_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
3232
regex::Regex::new(r"\b((http|https|ftp|ftps):[\w.,:;$/@!?&%\-~=#+]+)").unwrap()
3333
});
3434

0 commit comments

Comments
 (0)