From 04f359996e4d4063d695048f1d2f35dd7da11170 Mon Sep 17 00:00:00 2001 From: james58899 Date: Sun, 17 Mar 2024 20:38:23 +0000 Subject: [PATCH] Replace static to const value --- Cargo.lock | 28 +++++++++++++++++++++++++++- Cargo.toml | 2 +- src/gallery_downloader.rs | 2 +- src/main.rs | 6 +++--- src/middleware/mod.rs | 5 ++--- src/rpc_http_client.rs | 3 ++- src/util.rs | 3 ++- 7 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e18ec51..887d66c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -463,6 +463,26 @@ dependencies = [ "memchr", ] +[[package]] +name = "const_format" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a214c7af3d04997541b18d432afaff4c455e79e2029079647e72fc2bd27673" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f6ff08fd20f4f299298a28e2dfa8a8ba1036e6cd2460ac1de7b425d76f2500" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -1756,6 +1776,7 @@ dependencies = [ "bytes", "chrono", "clap", + "const_format", "cpufeatures", "filesize", "filetime", @@ -1768,7 +1789,6 @@ dependencies = [ "inquire", "log", "mime", - "once_cell", "openssl", "openssl-src", "parking_lot", @@ -3560,6 +3580,12 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" +[[package]] +name = "unicode-xid" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" + [[package]] name = "untrusted" version = "0.9.0" diff --git a/Cargo.toml b/Cargo.toml index 02a8a29..157b436 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ axum = { version = "0.7", default-features = false, features = ["http1", "matche bytes = "1.5" chrono = "0.4" clap = { version = "4.5", features = ["derive", "wrap_help"] } +const_format = "0.2.32" filesize = "0.2" filetime = "0.2" futures = "0.3" @@ -22,7 +23,6 @@ hyper-util = { version = "0.1", features = ["tokio"] } inquire = "0.7" log = { version = "0.4", features = ["std"] } mime = "0.3" -once_cell = "1.19" openssl = { version = "*", features = ["vendored"] } parking_lot = { version = "0.12", features = ["hardware-lock-elision", "deadlock_detection"] } pin-project-lite = "0.2" diff --git a/src/gallery_downloader.rs b/src/gallery_downloader.rs index 33b8a37..172fe2d 100644 --- a/src/gallery_downloader.rs +++ b/src/gallery_downloader.rs @@ -23,7 +23,7 @@ use crate::{error::Error, rpc::RPCClient, util}; type BoxError = Box; -static MAX_DOWNLOAD_TASK: u32 = 4; +const MAX_DOWNLOAD_TASK: u32 = 4; pub struct GalleryDownloader { client: Arc, diff --git a/src/main.rs b/src/main.rs index 0b4afff..29c918c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,9 +53,9 @@ static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; #[no_mangle] pub static mut malloc_conf: *const u8 = b"percpu_arena:phycpu,tcache:false,dirty_decay_ms:1000,muzzy_decay_ms:0\0".as_ptr(); -static VERSION : &str = concat!(env!("CARGO_PKG_VERSION"), "-", env!("VERGEN_GIT_SHA")); -pub static CLIENT_VERSION: &str = "1.6.2"; -static MAX_KEY_TIME_DRIFT: RangeInclusive = -300..=300; +const VERSION : &str = concat!(env!("CARGO_PKG_VERSION"), "-", env!("VERGEN_GIT_SHA")); +pub const CLIENT_VERSION: &str = "1.6.2"; +const MAX_KEY_TIME_DRIFT: RangeInclusive = -300..=300; #[derive(Parser)] #[command(version = VERSION)] diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs index 41f1c33..cffdbba 100644 --- a/src/middleware/mod.rs +++ b/src/middleware/mod.rs @@ -13,7 +13,7 @@ use axum::{ response::Response, Router, }; -use once_cell::sync::Lazy; +use const_format::concatcp; use tower::{timeout::TimeoutLayer, ServiceBuilder}; use crate::{AppState, CLIENT_VERSION}; @@ -21,8 +21,7 @@ use crate::{AppState, CLIENT_VERSION}; use self::connection_counter::ConnectionCounter; use self::logger::Logger; -static SERVER_HEADER: Lazy = - Lazy::new(|| HeaderValue::from_maybe_shared(format!("Genetic Lifeform and Distributed Open Server {CLIENT_VERSION}")).unwrap()); +static SERVER_HEADER: HeaderValue = HeaderValue::from_static(concatcp!("Genetic Lifeform and Distributed Open Server ", CLIENT_VERSION)); pub fn register_layer(router: Router>, data: &AppState) -> Router> { router diff --git a/src/rpc_http_client.rs b/src/rpc_http_client.rs index 31c3414..8aba5b9 100644 --- a/src/rpc_http_client.rs +++ b/src/rpc_http_client.rs @@ -9,6 +9,7 @@ use std::{ }; use bytes::Bytes; +use const_format::concatcp; use http_body_util::{BodyExt, Collected, Empty}; use hyper::{body::Incoming, client::conn::http1::handshake, http::Error as HttpError, Request, Response}; use hyper_util::rt::TokioIo; @@ -163,7 +164,7 @@ async fn create_stream(server: &str) -> Result, IoError> { fn build_request(url: &Url) -> Result>, HttpError> { Request::builder() .header("Host", url.host_str().unwrap()) - .header("User-Agent", format!("Hentai@Home {CLIENT_VERSION}")) + .header("User-Agent", concatcp!("Hentai@Home ",CLIENT_VERSION)) .uri(url.as_str()) .body(Empty::::new()) } diff --git a/src/util.rs b/src/util.rs index 0831430..058144f 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,6 @@ use std::time::Duration; +use const_format::concatcp; use futures::future::try_join_all; use openssl::sha::Sha1; use reqwest::Proxy; @@ -15,7 +16,7 @@ pub fn string_to_hash(str: String) -> String { pub fn create_http_client(timeout: Duration, proxy: Option) -> reqwest::Client { let mut builder = reqwest::ClientBuilder::new() - .user_agent(format!("Hentai@Home {CLIENT_VERSION}")) + .user_agent(concatcp!("Hentai@Home ", CLIENT_VERSION)) .tcp_keepalive(Duration::from_secs(75)) // Linux default keepalive inverval .connect_timeout(Duration::from_secs(5)) .timeout(timeout)