Skip to content

Commit a989d19

Browse files
authored
Merge pull request #479 from tchebb/lazylock
Use `std::sync::LazyLock` instead of `once_cell` version
2 parents 2db763e + cf58f2f commit a989d19

File tree

11 files changed

+46
-51
lines changed

11 files changed

+46
-51
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ rust-embed = { version = "8.1.0", features = ["include-exclude"] }
3838
libflate = "2.0.0"
3939
brotli = { version = "7.0.0", features = ["std"] }
4040
toml = "0.8.8"
41-
once_cell = "1.19.0"
4241
serde_yaml = "0.9.29"
4342
build_html = "2.4.0"
4443
uuid = { version = "1.6.1", features = ["v4"] }

src/client.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use hyper::{body, body::Buf, header, Body, Client, Method, Request, Response, Ur
88
use hyper_rustls::HttpsConnector;
99
use libflate::gzip;
1010
use log::{error, trace, warn};
11-
use once_cell::sync::Lazy;
1211
use percent_encoding::{percent_encode, CONTROLS};
1312
use serde_json::Value;
1413

1514
use std::sync::atomic::Ordering;
1615
use std::sync::atomic::{AtomicBool, AtomicU16};
16+
use std::sync::LazyLock;
1717
use std::{io, result::Result};
1818

1919
use crate::dbg_msg;
@@ -30,12 +30,12 @@ const REDDIT_SHORT_URL_BASE_HOST: &str = "redd.it";
3030
const ALTERNATIVE_REDDIT_URL_BASE: &str = "https://www.reddit.com";
3131
const ALTERNATIVE_REDDIT_URL_BASE_HOST: &str = "www.reddit.com";
3232

33-
pub static HTTPS_CONNECTOR: Lazy<HttpsConnector<HttpConnector>> =
34-
Lazy::new(|| hyper_rustls::HttpsConnectorBuilder::new().with_native_roots().https_only().enable_http2().build());
33+
pub static HTTPS_CONNECTOR: LazyLock<HttpsConnector<HttpConnector>> =
34+
LazyLock::new(|| hyper_rustls::HttpsConnectorBuilder::new().with_native_roots().https_only().enable_http2().build());
3535

36-
pub static CLIENT: Lazy<Client<HttpsConnector<HttpConnector>>> = Lazy::new(|| Client::builder().build::<_, Body>(HTTPS_CONNECTOR.clone()));
36+
pub static CLIENT: LazyLock<Client<HttpsConnector<HttpConnector>>> = LazyLock::new(|| Client::builder().build::<_, Body>(HTTPS_CONNECTOR.clone()));
3737

38-
pub static OAUTH_CLIENT: Lazy<ArcSwap<Oauth>> = Lazy::new(|| {
38+
pub static OAUTH_CLIENT: LazyLock<ArcSwap<Oauth>> = LazyLock::new(|| {
3939
let client = block_on(Oauth::new());
4040
tokio::spawn(token_daemon());
4141
ArcSwap::new(client.into())
@@ -154,7 +154,7 @@ async fn stream(url: &str, req: &Request<Body>) -> Result<Response<Body>, String
154154
let parsed_uri = url.parse::<Uri>().map_err(|_| "Couldn't parse URL".to_string())?;
155155

156156
// Build the hyper client from the HTTPS connector.
157-
let client: &Lazy<Client<_, Body>> = &CLIENT;
157+
let client: &LazyLock<Client<_, Body>> = &CLIENT;
158158

159159
let mut builder = Request::get(parsed_uri);
160160

@@ -222,7 +222,7 @@ fn request(method: &'static Method, path: String, redirect: bool, quarantine: bo
222222
let url = format!("{base_path}{path}");
223223

224224
// Construct the hyper client from the HTTPS connector.
225-
let client: &Lazy<Client<_, Body>> = &CLIENT;
225+
let client: &LazyLock<Client<_, Body>> = &CLIENT;
226226

227227
// Build request to Reddit. When making a GET, request gzip compression.
228228
// (Reddit doesn't do brotli yet.)

src/config.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use once_cell::sync::Lazy;
21
use serde::{Deserialize, Serialize};
3-
use std::{env::var, fs::read_to_string};
2+
use std::{env::var, fs::read_to_string, sync::LazyLock};
43

5-
// Waiting for https://github.com/rust-lang/rust/issues/74465 to land, so we
6-
// can reduce reliance on once_cell.
74
/// This is the local static that is initialized at runtime (technically at
85
/// first request) and contains the instance settings.
9-
pub static CONFIG: Lazy<Config> = Lazy::new(Config::load);
6+
pub static CONFIG: LazyLock<Config> = LazyLock::new(Config::load);
107

118
/// This serves as the frontend for an archival API - on removed comments, this URL
129
/// will be the base of a link, to display removed content (on another site).

src/instance_info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use crate::{
66
use askama::Template;
77
use build_html::{Container, Html, HtmlContainer, Table};
88
use hyper::{http::Error, Body, Request, Response};
9-
use once_cell::sync::Lazy;
109
use serde::{Deserialize, Serialize};
10+
use std::sync::LazyLock;
1111
use time::OffsetDateTime;
1212

1313
/// This is the local static that is initialized at runtime (technically at
1414
/// the first request to the info endpoint) and contains the data
1515
/// retrieved from the info endpoint.
16-
pub static INSTANCE_INFO: Lazy<InstanceInfo> = Lazy::new(InstanceInfo::new);
16+
pub static INSTANCE_INFO: LazyLock<InstanceInfo> = LazyLock::new(InstanceInfo::new);
1717

1818
/// Handles instance info endpoint
1919
pub async fn instance_info(req: Request<Body>) -> Result<Response<Body>, String> {

src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
use cached::proc_macro::cached;
66
use clap::{Arg, ArgAction, Command};
77
use std::str::FromStr;
8+
use std::sync::LazyLock;
89

910
use futures_lite::FutureExt;
1011
use hyper::Uri;
1112
use hyper::{header::HeaderValue, Body, Request, Response};
1213
use log::{info, warn};
13-
use once_cell::sync::Lazy;
1414
use redlib::client::{canonical_path, proxy, rate_limit_check, CLIENT};
1515
use redlib::server::{self, RequestExt};
1616
use redlib::utils::{error, redirect, ThemeAssets};
@@ -200,11 +200,11 @@ async fn main() {
200200
// at first request
201201

202202
info!("Evaluating config.");
203-
Lazy::force(&config::CONFIG);
203+
LazyLock::force(&config::CONFIG);
204204
info!("Evaluating instance info.");
205-
Lazy::force(&instance_info::INSTANCE_INFO);
205+
LazyLock::force(&instance_info::INSTANCE_INFO);
206206
info!("Creating OAUTH client.");
207-
Lazy::force(&OAUTH_CLIENT);
207+
LazyLock::force(&OAUTH_CLIENT);
208208

209209
// Define default headers (added to all responses)
210210
app.default_headers = headers! {

src/oauth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl Oauth {
102102
trace!("Sending token request...\n\n{request:?}");
103103

104104
// Send request
105-
let client: &once_cell::sync::Lazy<client::Client<_, Body>> = &CLIENT;
105+
let client: &std::sync::LazyLock<client::Client<_, Body>> = &CLIENT;
106106
let resp = client.request(request).await?;
107107

108108
trace!("Received response with status {} and length {:?}", resp.status(), resp.headers().get("content-length"));

src/post.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use crate::utils::{
1111
use hyper::{Body, Request, Response};
1212

1313
use askama::Template;
14-
use once_cell::sync::Lazy;
1514
use regex::Regex;
1615
use std::collections::{HashMap, HashSet};
16+
use std::sync::LazyLock;
1717

1818
// STRUCTS
1919
#[derive(Template)]
@@ -29,7 +29,7 @@ struct PostTemplate {
2929
comment_query: String,
3030
}
3131

32-
static COMMENT_SEARCH_CAPTURE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\?q=(.*)&type=comment").unwrap());
32+
static COMMENT_SEARCH_CAPTURE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\?q=(.*)&type=comment").unwrap());
3333

3434
pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
3535
// Build Reddit API path

src/search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::{
99
};
1010
use askama::Template;
1111
use hyper::{Body, Request, Response};
12-
use once_cell::sync::Lazy;
1312
use regex::Regex;
13+
use std::sync::LazyLock;
1414

1515
// STRUCTS
1616
struct SearchParams {
@@ -52,7 +52,7 @@ struct SearchTemplate {
5252
}
5353

5454
/// Regex matched against search queries to determine if they are reddit urls.
55-
static REDDIT_URL_MATCH: Lazy<Regex> = Lazy::new(|| Regex::new(r"^https?://([^\./]+\.)*reddit.com/").unwrap());
55+
static REDDIT_URL_MATCH: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^https?://([^\./]+\.)*reddit.com/").unwrap());
5656

5757
// SERVICES
5858
pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {

src/subreddit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use htmlescape::decode_html;
1313
use hyper::{Body, Request, Response};
1414

1515
use chrono::DateTime;
16-
use once_cell::sync::Lazy;
1716
use regex::Regex;
17+
use std::sync::LazyLock;
1818
use time::{Duration, OffsetDateTime};
1919

2020
// STRUCTS
@@ -58,7 +58,7 @@ struct WallTemplate {
5858
url: String,
5959
}
6060

61-
static GEO_FILTER_MATCH: Lazy<Regex> = Lazy::new(|| Regex::new(r"geo_filter=(?<region>\w+)").unwrap());
61+
static GEO_FILTER_MATCH: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"geo_filter=(?<region>\w+)").unwrap());
6262

6363
// SERVICES
6464
pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {

0 commit comments

Comments
 (0)