Skip to content

Commit

Permalink
Merge pull request #1628 from scpwiki/WJ-1189-lazy_static
Browse files Browse the repository at this point in the history
[WJ-1189] Replace lazy_static with once_cell
  • Loading branch information
emmiegit authored Sep 29, 2023
2 parents d5bde68 + 9fc4577 commit 3b26d57
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 86 deletions.
57 changes: 35 additions & 22 deletions deepwell/Cargo.lock

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

8 changes: 4 additions & 4 deletions deepwell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ watch = ["notify"]

[dependencies]
anyhow = "1"
arraystring = "0.3"
argon2 = "0.5"
arraystring = "0.3"
async-std = { version = "1", features = ["attributes"] }
cfg-if = "1"
clap = "4"
Expand All @@ -37,8 +37,8 @@ futures = { version = "0.3", features = ["async-await"], default-features = fals
hex = "0.4"
hostname = "0.3"
intl-memoizer = "0.5"
lazy_static = "1"
notify = { version = "6", optional = true }
once_cell = "1"
otp = { git = "https://github.com/TimDumol/rust-otp" }
rand = "0.8"
ref-map = "0.1"
Expand All @@ -63,8 +63,8 @@ toml = { version = "0.8", features = ["parse"] }
typenum = "1"
unic-langid = "0.9"
unicase = "2"
wikidot-normalize = "0.11"
wikidot-path = "0.5"
wikidot-normalize = "0.12"
wikidot-path = "0.6"

# NOTE: "indexmap" was formerly pinned to "=1.6.2" to avoid a cyclic dependency issue.
# This seems to no longer be necessary, but the comment is kept here in case it becomes a problem again.
Expand Down
63 changes: 31 additions & 32 deletions deepwell/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,50 @@ mod build {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}

use once_cell::sync::Lazy;

pub use self::build::{
BUILT_TIME_UTC, GIT_COMMIT_HASH, NUM_JOBS, PKG_AUTHORS, PKG_DESCRIPTION, PKG_LICENSE,
PKG_NAME, PKG_REPOSITORY, PKG_VERSION, RUSTC_VERSION, TARGET,
};

lazy_static! {
static ref VERSION_INFO: String = {
let mut version = format!("v{PKG_VERSION}");
pub static VERSION_INFO: Lazy<String> = Lazy::new(|| {
let mut version = format!("v{PKG_VERSION}");

if let Some(commit_hash) = *GIT_COMMIT_HASH_SHORT {
str_write!(&mut version, " [{commit_hash}]");
}
if let Some(commit_hash) = *GIT_COMMIT_HASH_SHORT {
str_write!(&mut version, " [{commit_hash}]");
}

version
};
version
});

pub static ref FULL_VERSION: String = {
let mut version = format!("{PKG_NAME} {}\n\nCompiled:\n", *VERSION_INFO);
pub static FULL_VERSION: Lazy<String> = Lazy::new(|| {
let mut version = format!("{PKG_NAME} {}\n\nCompiled:\n", *VERSION_INFO);

str_writeln!(&mut version, "* across {NUM_JOBS} threads");
str_writeln!(&mut version, "* by {RUSTC_VERSION}");
str_writeln!(&mut version, "* for {TARGET}");
str_writeln!(&mut version, "* on {BUILT_TIME_UTC}");
str_writeln!(&mut version, "* across {NUM_JOBS} threads");
str_writeln!(&mut version, "* by {RUSTC_VERSION}");
str_writeln!(&mut version, "* for {TARGET}");
str_writeln!(&mut version, "* on {BUILT_TIME_UTC}");

version
};
version
});

pub static ref VERSION: String = format!("{PKG_NAME} {}", *VERSION_INFO);
pub static VERSION: Lazy<String> = Lazy::new(|| format!("{PKG_NAME} {}", *VERSION_INFO));

pub static ref GIT_COMMIT_HASH_SHORT: Option<&'static str> = {
build::GIT_COMMIT_HASH.map(|s| &s[..8])
};
pub static GIT_COMMIT_HASH_SHORT: Lazy<Option<&'static str>> =
Lazy::new(|| build::GIT_COMMIT_HASH.map(|s| &s[..8]));

pub static ref HOSTNAME: String = {
// According to the gethostname(3p) man page,
// there don't seem to be any errors possible.
//
// However it is possible that converting from
// OsStr can fail.
hostname::get()
.expect("Unable to get hostname")
.into_string()
.expect("Unable to convert to UTF-8 string")
};
}
pub static HOSTNAME: Lazy<String> = Lazy::new(|| {
// According to the gethostname(3p) man page,
// there don't seem to be any errors possible.
//
// However it is possible that converting from
// OsStr can fail.
hostname::get()
.expect("Unable to get hostname")
.into_string()
.expect("Unable to convert to UTF-8 string")
});

#[test]
fn info() {
Expand Down
3 changes: 0 additions & 3 deletions deepwell/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
#[macro_use]
extern crate futures;

#[macro_use]
extern crate lazy_static;

#[macro_use]
extern crate serde;

Expand Down
11 changes: 6 additions & 5 deletions deepwell/src/services/blob/mime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//! Evaluates MIME types using libmagic.
//!
//! Because it is a binding to a C library, it cannot be shared among threads.
//! So we cannot use `lazy_static` and we can't have it in a coroutine.
//! So we cannot use `once_cell::Lazy` and we can't have it in a coroutine.
//! We don't load the `Magic` instance locally because it's an expensive operation
//! and it would be inefficient to load it for each invocation.
//!
Expand All @@ -30,6 +30,7 @@
use super::prelude::*;
use crossfire::mpsc;
use filemagic::{FileMagicError, Flags as MagicFlags, Magic};
use once_cell::sync::Lazy;
use std::convert::Infallible;
use std::sync::Once;
use std::{process, thread};
Expand All @@ -41,10 +42,8 @@ type RequestPayload = (Vec<u8>, ResponseSender);
type RequestSender = mpsc::TxFuture<RequestPayload, mpsc::SharedSenderFRecvB>;
type RequestReceiver = mpsc::RxBlocking<RequestPayload, mpsc::SharedSenderFRecvB>;

lazy_static! {
static ref QUEUE: (RequestSender, RequestReceiver) =
mpsc::bounded_tx_future_rx_blocking(64);
}
static QUEUE: Lazy<(RequestSender, RequestReceiver)> =
Lazy::new(|| mpsc::bounded_tx_future_rx_blocking(64));

macro_rules! sink {
() => {
Expand Down Expand Up @@ -78,6 +77,8 @@ fn main_loop() -> Result<Infallible> {
}

/// Starts the thread containing the `Magic` instance.
///
/// If the thread is already started, then this does nothing.
pub fn spawn_magic_thread() {
static START: Once = Once::new();

Expand Down
15 changes: 9 additions & 6 deletions deepwell/src/services/file_revision/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@ use crate::models::file_revision::{
};
use crate::services::{OutdateService, PageService};
use crate::web::FetchDirection;
use once_cell::sync::Lazy;
use std::num::NonZeroI32;

lazy_static! {
/// The changes for the first revision.
/// The first revision is always considered to have changed everything.
static ref ALL_CHANGES: Vec<String> = vec![
/// The changes for the first revision.
/// The first revision is always considered to have changed everything.
///
/// See `services/page_revision/service.rs`.
static ALL_CHANGES: Lazy<Vec<String>> = Lazy::new(|| {
vec![
str!("page"),
str!("name"),
str!("blob"),
str!("mime"),
str!("licensing"),
];
}
]
});

#[derive(Debug)]
pub struct FileRevisionService;
Expand Down
7 changes: 3 additions & 4 deletions deepwell/src/services/job/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ use crate::api::ApiServerState;
use crate::services::{PageRevisionService, SessionService, TextService};
use async_std::task;
use crossfire::mpsc;
use once_cell::sync::Lazy;
use sea_orm::TransactionTrait;
use std::convert::Infallible;
use std::sync::Arc;

lazy_static! {
static ref QUEUE: (mpsc::TxUnbounded<Job>, mpsc::RxUnbounded<Job>) =
mpsc::unbounded_future();
}
static QUEUE: Lazy<(mpsc::TxUnbounded<Job>, mpsc::RxUnbounded<Job>)> =
Lazy::new(mpsc::unbounded_future);

macro_rules! sink {
() => {
Expand Down
Loading

0 comments on commit 3b26d57

Please sign in to comment.