]> Untitled Git - lemmy.git/commitdiff
Merge pull request #1938 from LemmyNet/once_cell
authorDessalines <dessalines@users.noreply.github.com>
Mon, 22 Nov 2021 19:27:08 +0000 (14:27 -0500)
committerGitHub <noreply@github.com>
Mon, 22 Nov 2021 19:27:08 +0000 (14:27 -0500)
Use once_cell instead of lazy_static

19 files changed:
Cargo.lock
crates/api/Cargo.toml
crates/api_crud/Cargo.toml
crates/apub/Cargo.toml
crates/apub/src/context.rs
crates/apub/src/lib.rs
crates/apub_lib/Cargo.toml
crates/apub_lib/src/lib.rs
crates/apub_lib/src/object_id.rs
crates/apub_lib/src/signatures.rs
crates/db_schema/Cargo.toml
crates/db_schema/src/lib.rs
crates/routes/Cargo.toml
crates/routes/src/feeds.rs
crates/routes/src/lib.rs
crates/utils/Cargo.toml
crates/utils/src/lib.rs
crates/utils/src/settings/mod.rs
crates/utils/src/utils.rs

index abb5013592e86c7970e15055713e9a1e2bec0cdc..aa8d170b8acc99e8192bf41411e453980aa59925 100644 (file)
@@ -1760,7 +1760,6 @@ dependencies = [
  "http",
  "http-signature-normalization-actix",
  "itertools",
- "lazy_static",
  "lemmy_api_common",
  "lemmy_apub",
  "lemmy_apub_lib",
@@ -1822,7 +1821,6 @@ dependencies = [
  "http",
  "http-signature-normalization-actix",
  "itertools",
- "lazy_static",
  "lemmy_api_common",
  "lemmy_apub",
  "lemmy_apub_lib",
@@ -1869,7 +1867,6 @@ dependencies = [
  "http",
  "http-signature-normalization-actix",
  "itertools",
- "lazy_static",
  "lemmy_api_common",
  "lemmy_apub_lib",
  "lemmy_db_schema",
@@ -1878,6 +1875,7 @@ dependencies = [
  "lemmy_utils",
  "lemmy_websocket",
  "log",
+ "once_cell",
  "percent-encoding",
  "rand 0.8.4",
  "reqwest",
@@ -1908,10 +1906,10 @@ dependencies = [
  "http",
  "http-signature-normalization-actix",
  "http-signature-normalization-reqwest",
- "lazy_static",
  "lemmy_apub_lib_derive",
  "lemmy_utils",
  "log",
+ "once_cell",
  "openssl",
  "reqwest",
  "serde",
@@ -1939,10 +1937,10 @@ dependencies = [
  "diesel",
  "diesel-derive-newtype",
  "diesel_migrations",
- "lazy_static",
  "lemmy_apub_lib",
  "lemmy_utils",
  "log",
+ "once_cell",
  "regex",
  "serde",
  "serde_json",
@@ -1995,7 +1993,6 @@ dependencies = [
  "awc",
  "chrono",
  "diesel",
- "lazy_static",
  "lemmy_api_common",
  "lemmy_apub",
  "lemmy_db_schema",
@@ -2004,6 +2001,7 @@ dependencies = [
  "lemmy_utils",
  "lemmy_websocket",
  "log",
+ "once_cell",
  "rss",
  "serde",
  "sha2",
@@ -2066,9 +2064,9 @@ dependencies = [
  "http",
  "itertools",
  "jsonwebtoken",
- "lazy_static",
  "lettre",
  "log",
+ "once_cell",
  "openssl",
  "percent-encoding",
  "rand 0.8.4",
index 8d88f8db17387e9cad99d9e7192d9a1b0bae959e..7d53cb33008bed4031a96bc518cdc5fcc0f8ef92 100644 (file)
@@ -35,7 +35,6 @@ log = "0.4.14"
 rand = "0.8.4"
 strum = "0.21.0"
 strum_macros = "0.21.1"
-lazy_static = "1.4.0"
 url = { version = "2.2.2", features = ["serde"] }
 openssl = "0.10.36"
 http = "0.2.5"
index a5f1216fec61630549b31c3a4b55e86d40c2f3bf..6372ebf4fdc99ac4cbb06d16203944229cb657db 100644 (file)
@@ -30,7 +30,6 @@ log = "0.4.14"
 rand = "0.8.4"
 strum = "0.21.0"
 strum_macros = "0.21.1"
-lazy_static = "1.4.0"
 url = { version = "2.2.2", features = ["serde"] }
 openssl = "0.10.36"
 http = "0.2.5"
index b4ddff281456c49b52e818479096ae7aafe8096e..8e5c312df254348219b9241b0e3067618999f92d 100644 (file)
@@ -50,7 +50,7 @@ thiserror = "1.0.29"
 background-jobs = "0.9.1"
 reqwest = { version = "0.11.4", features = ["json"] }
 html2md = "0.2.13"
-lazy_static = "1.4.0"
+once_cell = "1.8.0"
 
 [dev-dependencies]
 serial_test = "0.5.1"
index 0a6e220a73ef6a8058014b17bd59c9f22f373ec2..3ec444e3392a8ce3bd8f9cf4645443fa8d4d3928 100644 (file)
@@ -1,9 +1,9 @@
+use once_cell::sync::Lazy;
 use serde::{Deserialize, Serialize};
 
-lazy_static! {
-  static ref CONTEXT: Vec<serde_json::Value> =
-    serde_json::from_str(include_str!("../assets/lemmy/context.json")).expect("parse context");
-}
+static CONTEXT: Lazy<Vec<serde_json::Value>> = Lazy::new(|| {
+  serde_json::from_str(include_str!("../assets/lemmy/context.json")).expect("parse context")
+});
 
 #[derive(Serialize, Deserialize, Debug)]
 pub(crate) struct WithContext<T> {
@@ -16,7 +16,7 @@ pub(crate) struct WithContext<T> {
 impl<T> WithContext<T> {
   pub(crate) fn new(inner: T) -> WithContext<T> {
     WithContext {
-      context: CONTEXT.clone(),
+      context: (*CONTEXT).clone(),
       inner,
     }
   }
index e400f81e8f014162bed0a396cb4d82dbe91241ce..984d722e8568066efc0a06b9f3aa7bb47cff8dd7 100644 (file)
@@ -17,9 +17,6 @@ pub mod migrations;
 pub mod objects;
 pub mod protocol;
 
-#[macro_use]
-extern crate lazy_static;
-
 /// Checks if the ID is allowed for sending or receiving.
 ///
 /// In particular, it checks for:
index ef9d0b2d7e9b1472f6586e6622677e0b66564794..1e30cd8d6d31be774d2191f372efb5c53e47e26d 100644 (file)
@@ -20,7 +20,7 @@ reqwest = { version = "0.11.4", features = ["json"] }
 log = "0.4.14"
 base64 = "0.13.0"
 openssl = "0.10.36"
-lazy_static = "1.4.0"
+once_cell = "1.8.0"
 http = "0.2.5"
 sha2 = "0.9.8"
 actix-web = { version = "4.0.0-beta.9", default-features = false }
index 82c190055cfbb317a19910c24157b4f6089ad20a..3c11fcea1e6120d1a9e591b667250204e4b70cc1 100644 (file)
@@ -1,6 +1,3 @@
-#[macro_use]
-extern crate lazy_static;
-
 pub mod activity_queue;
 pub mod data;
 pub mod object_id;
index cf5dfaa1e31b366b0136d8a40923bcf5c196b3fd..7d3be9e5e40cb8b63ce82ec6132a3dfe1ebf7d9b 100644 (file)
@@ -8,6 +8,7 @@ use lemmy_utils::{
   LemmyError,
 };
 use log::info;
+use once_cell::sync::Lazy;
 use reqwest::{Client, StatusCode};
 use serde::{Deserialize, Serialize};
 use std::{
@@ -21,12 +22,12 @@ use url::Url;
 /// fetch through the search). This should be configurable.
 static REQUEST_LIMIT: i32 = 25;
 
-lazy_static! {
-  static ref CLIENT: Client = Client::builder()
+static CLIENT: Lazy<Client> = Lazy::new(|| {
+  Client::builder()
     .user_agent(build_user_agent(&Settings::get()))
     .build()
-    .expect("Couldn't build client");
-}
+    .expect("Couldn't build client")
+});
 
 /// We store Url on the heap because it is quite large (88 bytes).
 #[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
index d05f997bcec711d1a83c2f611b094923db0ae1ca..2ba7edd47f7e9528c4d5f2eeda23270b3b09b9df 100644 (file)
@@ -6,6 +6,7 @@ use http_signature_normalization_actix::Config as ConfigActix;
 use http_signature_normalization_reqwest::prelude::{Config, SignExt};
 use lemmy_utils::LemmyError;
 use log::debug;
+use once_cell::sync::Lazy;
 use openssl::{
   hash::MessageDigest,
   pkey::PKey,
@@ -17,10 +18,8 @@ use sha2::{Digest, Sha256};
 use std::str::FromStr;
 use url::Url;
 
-lazy_static! {
-  static ref CONFIG2: ConfigActix = ConfigActix::new();
-  static ref HTTP_SIG_CONFIG: Config = Config::new();
-}
+static CONFIG2: Lazy<ConfigActix> = Lazy::new(ConfigActix::new);
+static HTTP_SIG_CONFIG: Lazy<Config> = Lazy::new(Config::new);
 
 /// Creates an HTTP post request to `inbox_url`, with the given `client` and `headers`, and
 /// `activity` as request body. The request is signed with `private_key` and then sent.
index 45655d6a0ae64c3ae72bc0c7b38815e788e2e8ac..b7e48691130a32a99955b9ce997f43eaee3c34b1 100644 (file)
@@ -22,7 +22,7 @@ log = "0.4.14"
 url = { version = "2.2.2", features = ["serde"] }
 diesel-derive-newtype = "0.1.2"
 regex = "1.5.4"
-lazy_static = "1.4.0"
+once_cell = "1.8.0"
 strum = "0.21.0"
 strum_macros = "0.21.1"
 sha2 = "0.9.8"
index a2a19290a1800a89b510aa8a2f64d2ba85b6172c..ddff1375e0b7cd46cf324098e16442dcd9b4a6f9 100644 (file)
@@ -2,8 +2,6 @@
 extern crate diesel;
 #[macro_use]
 extern crate diesel_derive_newtype;
-#[macro_use]
-extern crate lazy_static;
 // this is used in tests
 #[allow(unused_imports)]
 #[macro_use]
@@ -24,6 +22,7 @@ use crate::newtypes::DbUrl;
 use chrono::NaiveDateTime;
 use diesel::{Connection, PgConnection};
 use lemmy_utils::ApiError;
+use once_cell::sync::Lazy;
 use regex::Regex;
 use serde::{Deserialize, Serialize};
 use std::{env, env::VarError};
@@ -133,11 +132,10 @@ pub fn naive_now() -> NaiveDateTime {
   chrono::prelude::Utc::now().naive_utc()
 }
 
-lazy_static! {
-  static ref EMAIL_REGEX: Regex =
-    Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
-      .expect("compile email regex");
-}
+static EMAIL_REGEX: Lazy<Regex> = Lazy::new(|| {
+  Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
+    .expect("compile email regex")
+});
 
 pub mod functions {
   use diesel::sql_types::*;
index 8c459afa0c0b4c836d050967795dca4f7cd013fd..00b17133eb1dd2b809fd2885ef3098208c38c42f 100644 (file)
@@ -32,4 +32,4 @@ serde = { version = "1.0.130", features = ["derive"] }
 awc = { version = "3.0.0-beta.8", default-features = false }
 url = { version = "2.2.2", features = ["serde"] }
 strum = "0.21.0"
-lazy_static = "1.4.0"
+once_cell = "1.8.0"
index 1a85a10ac693806fc0cdf5ff155bff3449644ea6..534ec9fb43e5ffcc5787c117e9b03bf048f9038d 100644 (file)
@@ -18,6 +18,7 @@ use lemmy_db_views::{
 use lemmy_db_views_actor::person_mention_view::{PersonMentionQueryBuilder, PersonMentionView};
 use lemmy_utils::{claims::Claims, utils::markdown_to_html, LemmyError};
 use lemmy_websocket::LemmyContext;
+use once_cell::sync::Lazy;
 use rss::{
   extension::dublincore::DublinCoreExtensionBuilder,
   ChannelBuilder,
@@ -48,16 +49,14 @@ pub fn config(cfg: &mut web::ServiceConfig) {
     .route("/feeds/local.xml", web::get().to(get_local_feed));
 }
 
-lazy_static! {
-  static ref RSS_NAMESPACE: HashMap<String, String> = {
-    let mut h = HashMap::new();
-    h.insert(
-      "dc".to_string(),
-      rss::extension::dublincore::NAMESPACE.to_string(),
-    );
-    h
-  };
-}
+static RSS_NAMESPACE: Lazy<HashMap<String, String>> = Lazy::new(|| {
+  let mut h = HashMap::new();
+  h.insert(
+    "dc".to_string(),
+    rss::extension::dublincore::NAMESPACE.to_string(),
+  );
+  h
+});
 
 async fn get_all_feed(
   info: web::Query<Params>,
index b966b766acb3026bc711d60b2147803c4b702253..e158fa550129f37a61dd81b62ec25222866a7e06 100644 (file)
@@ -1,6 +1,3 @@
-#[macro_use]
-extern crate lazy_static;
-
 pub mod feeds;
 pub mod images;
 pub mod nodeinfo;
index 2c2482b5aae456407044cb3f359de612f79fb947..fa75be6919f0f2e88e75d46609662636d126d6c6 100644 (file)
@@ -24,7 +24,7 @@ serde = { version = "1.0.130", features = ["derive"] }
 serde_json = { version = "1.0.68", features = ["preserve_order"] }
 thiserror = "1.0.29"
 comrak = { version = "0.12.1", default-features = false }
-lazy_static = "1.4.0"
+once_cell = "1.8.0"
 openssl = "0.10.36"
 url = { version = "2.2.2", features = ["serde"] }
 actix-web = { version = "4.0.0-beta.9", default-features = false, features = ["rustls"] }
index 057e067e62a1d6db7a84e9ac578052fa2a737e2f..e30150e30b058521986ffece8130fa2313719f1c 100644 (file)
@@ -1,6 +1,4 @@
 #[macro_use]
-extern crate lazy_static;
-#[macro_use]
 extern crate strum_macros;
 #[macro_use]
 extern crate smart_default;
index 260acbd9776f286457b31f9ffea9d9284472f782..de1940902e658927f6f91609081083d04656b72a 100644 (file)
@@ -1,6 +1,7 @@
 use crate::{location_info, settings::structs::Settings, LemmyError};
 use anyhow::{anyhow, Context};
 use deser_hjson::from_str;
+use once_cell::sync::Lazy;
 use regex::{Regex, RegexBuilder};
 use std::{env, fs, io::Error, sync::RwLock};
 
@@ -8,15 +9,15 @@ pub mod structs;
 
 static DEFAULT_CONFIG_FILE: &str = "config/config.hjson";
 
-lazy_static! {
-  static ref SETTINGS: RwLock<Settings> =
-    RwLock::new(Settings::init().expect("Failed to load settings file"));
-  static ref WEBFINGER_REGEX: Regex = Regex::new(&format!(
+static SETTINGS: Lazy<RwLock<Settings>> =
+  Lazy::new(|| RwLock::new(Settings::init().expect("Failed to load settings file")));
+static WEBFINGER_REGEX: Lazy<Regex> = Lazy::new(|| {
+  Regex::new(&format!(
     "^acct:([a-z0-9_]{{3,}})@{}$",
     Settings::get().hostname
   ))
-  .expect("compile webfinger regex");
-}
+  .expect("compile webfinger regex")
+});
 
 impl Settings {
   /// Reads config from configuration file.
index 3039a1cd474a0e5e7ec0526fb53aed3e0e8514f6..1f3252ffafe0727d26c6e2eb144c7161a1c65d14 100644 (file)
@@ -2,23 +2,26 @@ use crate::{ApiError, IpAddr};
 use actix_web::dev::ConnectionInfo;
 use chrono::{DateTime, FixedOffset, NaiveDateTime};
 use itertools::Itertools;
+use once_cell::sync::Lazy;
 use rand::{distributions::Alphanumeric, thread_rng, Rng};
 use regex::Regex;
 use url::Url;
 
-lazy_static! {
-  static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").expect("compile regex");
-
-  static ref USERNAME_MATCHES_REGEX: Regex = Regex::new(r"/u/[a-zA-Z][0-9a-zA-Z_]*").expect("compile regex");
-  // TODO keep this old one, it didn't work with port well tho
-  // static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)").expect("compile regex");
-  static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._:-]+)").expect("compile regex");
-  static ref VALID_ACTOR_NAME_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9_]{3,}$").expect("compile regex");
-  static ref VALID_POST_TITLE_REGEX: Regex = Regex::new(r".*\S.*").expect("compile regex");
-  static ref VALID_MATRIX_ID_REGEX: Regex = Regex::new(r"^@[A-Za-z0-9._=-]+:[A-Za-z0-9.-]+\.[A-Za-z]{2,}$").expect("compile regex");
-  // taken from https://en.wikipedia.org/wiki/UTM_parameters
-  static ref CLEAN_URL_PARAMS_REGEX: Regex = Regex::new(r"^utm_source|utm_medium|utm_campaign|utm_term|utm_content|gclid|gclsrc|dclid|fbclid$").expect("compile regex");
-}
+static MENTIONS_REGEX: Lazy<Regex> = Lazy::new(|| {
+  Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._:-]+)").expect("compile regex")
+});
+static VALID_ACTOR_NAME_REGEX: Lazy<Regex> =
+  Lazy::new(|| Regex::new(r"^[a-zA-Z0-9_]{3,}$").expect("compile regex"));
+static VALID_POST_TITLE_REGEX: Lazy<Regex> =
+  Lazy::new(|| Regex::new(r".*\S.*").expect("compile regex"));
+static VALID_MATRIX_ID_REGEX: Lazy<Regex> = Lazy::new(|| {
+  Regex::new(r"^@[A-Za-z0-9._=-]+:[A-Za-z0-9.-]+\.[A-Za-z]{2,}$").expect("compile regex")
+});
+// taken from https://en.wikipedia.org/wiki/UTM_parameters
+static CLEAN_URL_PARAMS_REGEX: Lazy<Regex> = Lazy::new(|| {
+  Regex::new(r"^utm_source|utm_medium|utm_campaign|utm_term|utm_content|gclid|gclsrc|dclid|fbclid$")
+    .expect("compile regex")
+});
 
 pub fn naive_from_unix(time: i64) -> NaiveDateTime {
   NaiveDateTime::from_timestamp(time, 0)