Switch from lazy_static to once_cell

This commit is contained in:
Alexander Krotov
2020-10-17 15:25:47 +03:00
committed by link2xt
parent bf72ae4ccc
commit 67cddedf7e
11 changed files with 955 additions and 682 deletions

2
Cargo.lock generated
View File

@@ -1033,7 +1033,6 @@ dependencies = [
"indexmap", "indexmap",
"itertools", "itertools",
"kamadak-exif", "kamadak-exif",
"lazy_static",
"lettre_email", "lettre_email",
"libc", "libc",
"log", "log",
@@ -1041,6 +1040,7 @@ dependencies = [
"native-tls", "native-tls",
"num-derive", "num-derive",
"num-traits", "num-traits",
"once_cell",
"percent-encoding", "percent-encoding",
"pgp", "pgp",
"pretty_assertions", "pretty_assertions",

View File

@@ -34,7 +34,7 @@ serde_json = "1.0"
chrono = "0.4.6" chrono = "0.4.6"
indexmap = "1.3.0" indexmap = "1.3.0"
kamadak-exif = "0.5" kamadak-exif = "0.5"
lazy_static = "1.4.0" once_cell = "1.4.1"
regex = "1.1.6" regex = "1.1.6"
rusqlite = { version = "0.24", features = ["bundled"] } rusqlite = { version = "0.24", features = ["bundled"] }
r2d2_sqlite = "0.17.0" r2d2_sqlite = "0.17.0"

View File

@@ -1,11 +1,9 @@
//! # Constants //! # Constants
use deltachat_derive::*; use deltachat_derive::*;
use lazy_static::lazy_static; use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
lazy_static! { pub static DC_VERSION_STR: Lazy<String> = Lazy::new(|| env!("CARGO_PKG_VERSION").to_string());
pub static ref DC_VERSION_STR: String = env!("CARGO_PKG_VERSION").to_string();
}
#[derive( #[derive(
Debug, Debug,

View File

@@ -3,7 +3,7 @@
use async_std::path::PathBuf; use async_std::path::PathBuf;
use deltachat_derive::*; use deltachat_derive::*;
use itertools::Itertools; use itertools::Itertools;
use lazy_static::lazy_static; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
use crate::aheader::EncryptPreference; use crate::aheader::EncryptPreference;
@@ -1053,9 +1053,7 @@ pub fn addr_normalize(addr: &str) -> &str {
} }
fn sanitize_name_and_addr(name: impl AsRef<str>, addr: impl AsRef<str>) -> (String, String) { fn sanitize_name_and_addr(name: impl AsRef<str>, addr: impl AsRef<str>) -> (String, String) {
lazy_static! { static ADDR_WITH_NAME_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new("(.*)<(.*)>").unwrap());
static ref ADDR_WITH_NAME_REGEX: Regex = Regex::new("(.*)<(.*)>").unwrap();
}
if let Some(captures) = ADDR_WITH_NAME_REGEX.captures(addr.as_ref()) { if let Some(captures) = ADDR_WITH_NAME_REGEX.captures(addr.as_ref()) {
( (
if name.as_ref().is_empty() { if name.as_ref().is_empty() {

View File

@@ -2,12 +2,10 @@
//! //!
//! A module to remove HTML tags from the email text //! A module to remove HTML tags from the email text
use lazy_static::lazy_static; use once_cell::sync::Lazy;
use quick_xml::events::{BytesEnd, BytesStart, BytesText}; use quick_xml::events::{BytesEnd, BytesStart, BytesText};
lazy_static! { static LINE_RE: Lazy<regex::Regex> = Lazy::new(|| regex::Regex::new(r"(\r?\n)+").unwrap());
static ref LINE_RE: regex::Regex = regex::Regex::new(r"(\r?\n)+").unwrap();
}
struct Dehtml { struct Dehtml {
strbuilder: String, strbuilder: String,

View File

@@ -431,11 +431,9 @@ mod tests {
use std::error::Error; use std::error::Error;
use async_std::sync::Arc; use async_std::sync::Arc;
use lazy_static::lazy_static; use once_cell::sync::Lazy;
lazy_static! { static KEYPAIR: Lazy<KeyPair> = Lazy::new(alice_keypair);
static ref KEYPAIR: KeyPair = alice_keypair();
}
#[test] #[test]
fn test_from_armored_string() { fn test_from_armored_string() {

View File

@@ -3,9 +3,9 @@ use std::future::Future;
use std::pin::Pin; use std::pin::Pin;
use deltachat_derive::{FromSql, ToSql}; use deltachat_derive::{FromSql, ToSql};
use lazy_static::lazy_static;
use lettre_email::mime::{self, Mime}; use lettre_email::mime::{self, Mime};
use mailparse::{addrparse_header, DispositionType, MailHeader, MailHeaderMap, SingleInfo}; use mailparse::{addrparse_header, DispositionType, MailHeader, MailHeaderMap, SingleInfo};
use once_cell::sync::Lazy;
use crate::aheader::Aheader; use crate::aheader::Aheader;
use crate::blob::BlobObject; use crate::blob::BlobObject;
@@ -1004,9 +1004,8 @@ impl MimeMessage {
false false
}; };
if maybe_ndn && self.failure_report.is_none() { if maybe_ndn && self.failure_report.is_none() {
lazy_static! { static RE: Lazy<regex::Regex> =
static ref RE: regex::Regex = regex::Regex::new(r"Message-ID:(.*)").unwrap(); Lazy::new(|| regex::Regex::new(r"Message-ID:(.*)").unwrap());
}
for captures in self for captures in self
.parts .parts
.iter() .iter()

View File

@@ -381,7 +381,7 @@ pub async fn symm_decrypt<T: std::io::Read + std::io::Seek>(
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::*; use crate::test_utils::*;
use lazy_static::lazy_static; use once_cell::sync::Lazy;
#[test] #[test]
fn test_split_armored_data_1() { fn test_split_armored_data_1() {
@@ -449,26 +449,29 @@ mod tests {
/// The original text of [CTEXT_SIGNED] /// The original text of [CTEXT_SIGNED]
static CLEARTEXT: &[u8] = b"This is a test"; static CLEARTEXT: &[u8] = b"This is a test";
lazy_static! {
/// Initialised [TestKeys] for tests. /// Initialised [TestKeys] for tests.
static ref KEYS: TestKeys = TestKeys::new(); static KEYS: Lazy<TestKeys> = Lazy::new(TestKeys::new);
/// A cyphertext encrypted to Alice & Bob, signed by Alice. /// A cyphertext encrypted to Alice & Bob, signed by Alice.
static ref CTEXT_SIGNED: String = { static CTEXT_SIGNED: Lazy<String> = Lazy::new(|| {
let mut keyring = Keyring::new(); let mut keyring = Keyring::new();
keyring.add(KEYS.alice_public.clone()); keyring.add(KEYS.alice_public.clone());
keyring.add(KEYS.bob_public.clone()); keyring.add(KEYS.bob_public.clone());
futures_lite::future::block_on(pk_encrypt(CLEARTEXT, keyring, Some(KEYS.alice_secret.clone()))).unwrap() futures_lite::future::block_on(pk_encrypt(
}; CLEARTEXT,
keyring,
Some(KEYS.alice_secret.clone()),
))
.unwrap()
});
/// A cyphertext encrypted to Alice & Bob, not signed. /// A cyphertext encrypted to Alice & Bob, not signed.
static ref CTEXT_UNSIGNED: String = { static CTEXT_UNSIGNED: Lazy<String> = Lazy::new(|| {
let mut keyring = Keyring::new(); let mut keyring = Keyring::new();
keyring.add(KEYS.alice_public.clone()); keyring.add(KEYS.alice_public.clone());
keyring.add(KEYS.bob_public.clone()); keyring.add(KEYS.bob_public.clone());
futures_lite::future::block_on(pk_encrypt(CLEARTEXT, keyring, None)).unwrap() futures_lite::future::block_on(pk_encrypt(CLEARTEXT, keyring, None)).unwrap()
}; });
}
#[test] #[test]
fn test_encrypt_signed() { fn test_encrypt_signed() {

File diff suppressed because it is too large Load Diff

View File

@@ -111,7 +111,7 @@ def process_data(data, file):
before_login_hint = cleanstr(data.get("before_login_hint", "")) before_login_hint = cleanstr(data.get("before_login_hint", ""))
after_login_hint = cleanstr(data.get("after_login_hint", "")) after_login_hint = cleanstr(data.get("after_login_hint", ""))
if (not has_imap and not has_smtp) or (has_imap and has_smtp): if (not has_imap and not has_smtp) or (has_imap and has_smtp):
provider += " static ref " + file2varname(file) + ": Provider = Provider {\n" provider += "static " + file2varname(file) + ": Lazy<Provider> = Lazy::new(|| Provider {\n"
provider += " status: Status::" + status + ",\n" provider += " status: Status::" + status + ",\n"
provider += " before_login_hint: \"" + before_login_hint + "\",\n" provider += " before_login_hint: \"" + before_login_hint + "\",\n"
provider += " after_login_hint: \"" + after_login_hint + "\",\n" provider += " after_login_hint: \"" + after_login_hint + "\",\n"
@@ -120,7 +120,7 @@ def process_data(data, file):
provider += " config_defaults: " + config_defaults + ",\n" provider += " config_defaults: " + config_defaults + ",\n"
provider += " strict_tls: " + strict_tls + ",\n" provider += " strict_tls: " + strict_tls + ",\n"
provider += " oauth2_authorizer: " + oauth2 + ",\n" provider += " oauth2_authorizer: " + oauth2 + ",\n"
provider += " };\n\n" provider += "});\n\n"
else: else:
raise TypeError("SMTP and IMAP must be specified together or left out both") raise TypeError("SMTP and IMAP must be specified together or left out both")
@@ -164,18 +164,16 @@ if __name__ == "__main__":
"use crate::provider::UsernamePattern::*;\n" "use crate::provider::UsernamePattern::*;\n"
"use crate::provider::*;\n" "use crate::provider::*;\n"
"use std::collections::HashMap;\n\n" "use std::collections::HashMap;\n\n"
"lazy_static::lazy_static! {\n\n") "use once_cell::sync::Lazy;\n\n")
process_dir(sys.argv[1]) process_dir(sys.argv[1])
out_all += " pub static ref PROVIDER_DATA: HashMap<&'static str, &'static Provider> = [\n" out_all += "pub static PROVIDER_DATA: Lazy<HashMap<&'static str, &'static Provider>> = Lazy::new(|| [\n"
out_all += out_domains; out_all += out_domains;
out_all += " ].iter().copied().collect();\n\n" out_all += "].iter().copied().collect());\n\n"
now = datetime.datetime.utcnow() now = datetime.datetime.utcnow()
out_all += " pub static ref PROVIDER_UPDATED: chrono::NaiveDate = "\ out_all += "pub static PROVIDER_UPDATED: Lazy<chrono::NaiveDate> = "\
"chrono::NaiveDate::from_ymd("+str(now.year)+", "+str(now.month)+", "+str(now.day)+");\n" "Lazy::new(|| chrono::NaiveDate::from_ymd("+str(now.year)+", "+str(now.month)+", "+str(now.day)+"));\n"
out_all += "}"
print(out_all) print(out_all)

View File

@@ -1,6 +1,6 @@
//! # QR code module //! # QR code module
use lazy_static::lazy_static; use once_cell::sync::Lazy;
use percent_encoding::percent_decode_str; use percent_encoding::percent_decode_str;
use serde::Deserialize; use serde::Deserialize;
@@ -358,12 +358,10 @@ async fn decode_matmsg(context: &Context, qr: &str) -> Lot {
Lot::from_address(context, name, addr).await Lot::from_address(context, name, addr).await
} }
lazy_static! { static VCARD_NAME_RE: Lazy<regex::Regex> =
static ref VCARD_NAME_RE: regex::Regex = Lazy::new(|| regex::Regex::new(r"(?m)^N:([^;]*);([^;\n]*)").unwrap());
regex::Regex::new(r"(?m)^N:([^;]*);([^;\n]*)").unwrap(); static VCARD_EMAIL_RE: Lazy<regex::Regex> =
static ref VCARD_EMAIL_RE: regex::Regex = Lazy::new(|| regex::Regex::new(r"(?m)^EMAIL([^:\n]*):([^;\n]*)").unwrap());
regex::Regex::new(r"(?m)^EMAIL([^:\n]*):([^;\n]*)").unwrap();
}
/// Extract address for the matmsg scheme. /// Extract address for the matmsg scheme.
/// ///