From 51b469962a3e6c88069dfb0e47f8d9b57ee96e63 Mon Sep 17 00:00:00 2001 From: quexeky Date: Wed, 3 Dec 2025 07:33:32 +1100 Subject: [PATCH] refactor: Add lints to use in future and fix some Signed-off-by: quexeky --- torrential/Cargo.lock | 334 ++++++++++++++++++++++++++++++- torrential/Cargo.toml | 21 ++ torrential/benches/torrential.rs | 14 ++ torrential/src/download.rs | 6 +- torrential/src/main.rs | 23 ++- torrential/src/remote.rs | 10 +- torrential/src/util.rs | 2 +- 7 files changed, 387 insertions(+), 23 deletions(-) create mode 100644 torrential/benches/torrential.rs diff --git a/torrential/Cargo.lock b/torrential/Cargo.lock index 19f4222c..317c8538 100644 --- a/torrential/Cargo.lock +++ b/torrential/Cargo.lock @@ -2,6 +2,36 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "alloca" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5a7d05ea6aea7e9e64d25b9156ba2fee3fdd659e34e41063cd2fc7cd020d7f4" +dependencies = [ + "cc", +] + +[[package]] +name = "anes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + [[package]] name = "anyhow" version = "1.0.100" @@ -174,6 +204,12 @@ version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + [[package]] name = "cc" version = "1.2.48" @@ -196,6 +232,58 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "ciborium" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" + +[[package]] +name = "ciborium-ll" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" +dependencies = [ + "ciborium-io", + "half", +] + +[[package]] +name = "clap" +version = "4.5.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9e340e012a1bf4935f5282ed1436d1489548e8f72308207ea5df0e23d2d03f8" +dependencies = [ + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.5.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d76b5d13eaa18c901fd2f7fca939fefe3a0727a953561fefdf3b2922b8569d00" +dependencies = [ + "anstyle", + "clap_lex", +] + +[[package]] +name = "clap_lex" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d" + [[package]] name = "colored" version = "3.0.0" @@ -205,12 +293,72 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "criterion" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0dfe5e9e71bdcf4e4954f7d14da74d1cdb92a3a07686452d1509652684b1aab" +dependencies = [ + "alloca", + "anes", + "cast", + "ciborium", + "clap", + "criterion-plot", + "itertools", + "num-traits", + "oorandom", + "page_size", + "plotters", + "rayon", + "regex", + "serde", + "serde_json", + "tinytemplate", + "walkdir", +] + +[[package]] +name = "criterion-plot" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5de36c2bee19fba779808f92bf5d9b0fa5a40095c277aba10c458a12b35d21d6" +dependencies = [ + "cast", + "itertools", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + [[package]] name = "dashmap" version = "6.1.0" @@ -303,6 +451,12 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + [[package]] name = "find-msvc-tools" version = "0.1.5" @@ -384,6 +538,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "half" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" +dependencies = [ + "cfg-if", + "crunchy", + "zerocopy", +] + [[package]] name = "hashbrown" version = "0.14.5" @@ -622,6 +787,15 @@ dependencies = [ "serde", ] +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.15" @@ -646,9 +820,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.177" +version = "0.2.178" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" +checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091" [[package]] name = "litemap" @@ -780,6 +954,22 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "oorandom" +version = "11.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" + +[[package]] +name = "page_size" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30d5b2194ed13191c1999ae0704b7839fb18384fa22e49b57eeaa97d79ce40da" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "parking_lot_core" version = "0.9.12" @@ -821,6 +1011,34 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "plotters" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" +dependencies = [ + "num-traits", + "plotters-backend", + "plotters-svg", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-backend" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" + +[[package]] +name = "plotters-svg" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" +dependencies = [ + "plotters-backend", +] + [[package]] name = "potential_utf" version = "0.1.4" @@ -906,7 +1124,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.60.2", ] [[package]] @@ -953,6 +1171,26 @@ dependencies = [ "getrandom 0.3.4", ] +[[package]] +name = "rayon" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "368f01d005bf8fd9b1206fb6fa653e6c4a81ceb1466406b81792d87c5677a58f" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "rcgen" version = "0.13.2" @@ -976,6 +1214,35 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" + [[package]] name = "reqwest" version = "0.12.24" @@ -1090,6 +1357,15 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -1334,6 +1610,16 @@ dependencies = [ "zerovec", ] +[[package]] +name = "tinytemplate" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "tinyvec" version = "1.10.0" @@ -1405,6 +1691,7 @@ version = "0.1.0" dependencies = [ "anyhow", "axum", + "criterion", "dashmap", "droplet-rs", "log", @@ -1519,6 +1806,16 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "want" version = "0.3.1" @@ -1640,6 +1937,37 @@ dependencies = [ "rustls-pki-types", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-link" version = "0.2.1" diff --git a/torrential/Cargo.toml b/torrential/Cargo.toml index 07c059bc..9e354b85 100644 --- a/torrential/Cargo.toml +++ b/torrential/Cargo.toml @@ -21,3 +21,24 @@ anyhow = "1.0.100" serde_json = "1.0.145" url = { version = "2.5.7", default-features = false } tokio-util = { version = "0.7.17", features = ["io"] } + +[lints.clippy] +pedantic = { level = "warn", priority = -1 } + +almost_swapped = "deny" +unwrap_used = "warn" +expect_used = "warn" +dbg_macro = "deny" +needless_match = "deny" + +cast_possible_truncation = "warn" +cast_possible_wrap = "warn" +single_match_else = "warn" +inconsistent_digit_grouping = "warn" + +[[bench]] +name = "torrential" +harness = false + +[dev-dependencies] +criterion = "0.8.0" diff --git a/torrential/benches/torrential.rs b/torrential/benches/torrential.rs new file mode 100644 index 00000000..ad7fd90a --- /dev/null +++ b/torrential/benches/torrential.rs @@ -0,0 +1,14 @@ +use criterion::{criterion_group, criterion_main, Criterion}; + +fn torrential() { + +} + +// The benchmark function setup +fn benchmark(c: &mut Criterion) { + c.bench_function("fibonacci 20", |b| b.iter(|| torrential())); +} + +// Grouping your benchmarks +criterion_group!(benches, benchmark); +criterion_main!(benches); \ No newline at end of file diff --git a/torrential/src/download.rs b/torrential/src/download.rs index ebcf63de..32272beb 100644 --- a/torrential/src/download.rs +++ b/torrential/src/download.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, path::PathBuf, time::Instant}; +use std::{collections::HashMap, hash::RandomState, time::Instant}; use droplet_rs::versions::{create_backend_constructor, types::VersionBackend}; use reqwest::StatusCode; @@ -9,7 +9,7 @@ use crate::{ util::ErrorOption, }; -pub async fn create_download_context<'a>( +pub async fn create_download_context( init_data: &AppInitData, game_id: String, version_name: String, @@ -21,7 +21,7 @@ pub async fn create_download_context<'a>( let mut chunk_lookup_table = HashMap::with_capacity_and_hasher( context.manifest.values().map(|v| v.ids.len()).sum(), - Default::default(), + RandomState::default(), ); for (path, file_chunks) in context.manifest { diff --git a/torrential/src/main.rs b/torrential/src/main.rs index b9310b67..afa4a7f2 100644 --- a/torrential/src/main.rs +++ b/torrential/src/main.rs @@ -4,7 +4,7 @@ use droplet_rs::versions::types::{MinimumFileObject, VersionBackend, VersionFile use reqwest::header; use simple_logger::SimpleLogger; use std::{ - collections::HashMap, env::set_current_dir, path::PathBuf, str::FromStr, sync::Arc, + collections::HashMap, env::set_current_dir, path::PathBuf, str::FromStr as _, sync::Arc, time::Instant, }; use tokio_util::io::ReaderStream; @@ -101,7 +101,7 @@ async fn set_token( let valid_library_sources = filter_library_sources(library_sources); - set_generated_token(state, token, valid_library_sources)?; + set_generated_token(&state, token, valid_library_sources)?; info!("connected to drop server successfully"); @@ -139,7 +139,7 @@ fn initialise_logger() { } async fn acquire_permit<'a>() -> SemaphorePermit<'a> { - GLOBAL_CONTEXT_SEMAPHORE + return GLOBAL_CONTEXT_SEMAPHORE .acquire() .await .expect("failed to acquire semaphore") @@ -164,12 +164,12 @@ async fn get_file_reader( .backend .reader( &VersionFile { - relative_filename: relative_filename.to_string(), + relative_filename: relative_filename.clone(), permission: 0, size: 0, }, - start.try_into().unwrap(), - end.try_into().unwrap(), + start as u64, + end as u64, ) .await .map_err(|v| { @@ -221,14 +221,15 @@ async fn healthcheck(State(state): State>) -> StatusCode { if !initialised { return StatusCode::SERVICE_UNAVAILABLE; } - return StatusCode::OK; + StatusCode::OK } fn check_token_exists(state: &Arc, payload: &TokenPayload) -> bool { if let Some(existing_data) = state.token.get() { - if existing_data.token != payload.token { - panic!("already set up but provided with a different token"); - } + assert!( + existing_data.token == payload.token, + "already set up but provided with a different token" + ); return true; } false @@ -261,7 +262,7 @@ fn filter_library_sources( .collect() } fn set_generated_token( - state: Arc, + state: &Arc, token: String, libraries: HashMap, ) -> Result<(), StatusCode> { diff --git a/torrential/src/remote.rs b/torrential/src/remote.rs index d242cbc9..a8bf3da3 100644 --- a/torrential/src/remote.rs +++ b/torrential/src/remote.rs @@ -35,7 +35,7 @@ static REMOTE_URL: LazyLock = LazyLock::new(|| { .map_or("http://localhost:3000", |v| v), ) .expect("failed to parse URL"); - info!("using Drop server url {}", url); + info!("using Drop server url {url}"); url }); @@ -50,7 +50,7 @@ pub async fn fetch_download_context( game: game_id, version: version_name, }) - .header("Authorization", format!("Bearer {}", token)) + .header("Authorization", format!("Bearer {token}")) .send() .await?; @@ -65,7 +65,7 @@ pub async fn fetch_download_context( context_response .text() .await - .unwrap_or("(failed to read body)".to_string()) + .unwrap_or("(failed to read body)".to_owned()) ) .into()); } @@ -92,7 +92,7 @@ pub struct LibrarySource { pub async fn fetch_library_sources(token: &String) -> Result> { let source_response = CLIENT .get(REMOTE_URL.join("/api/v1/admin/library/sources")?) - .header("Authorization", format!("Bearer {}", token)) + .header("Authorization", format!("Bearer {token}")) .send() .await?; @@ -103,7 +103,7 @@ pub async fn fetch_library_sources(token: &String) -> Result> source_response .text() .await - .unwrap_or("(failed to read body)".to_string()) + .unwrap_or("(failed to read body)".to_owned()) )); } diff --git a/torrential/src/util.rs b/torrential/src/util.rs index 07e319cb..be1b8579 100644 --- a/torrential/src/util.rs +++ b/torrential/src/util.rs @@ -30,7 +30,7 @@ impl From for StatusCode { Ok(status) => status, Err(err) => { error!("{err:?}"); - StatusCode::INTERNAL_SERVER_ERROR + Self::INTERNAL_SERVER_ERROR } } }