diff --git a/Cargo.lock b/Cargo.lock index 02b4c361e..ee9184bc6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -279,7 +279,7 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", - "clippy-utilities 0.2.0", + "clippy-utilities", "etcd-client", "indicatif", "rand", @@ -473,12 +473,6 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" -[[package]] -name = "clippy-utilities" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc5b2535647ee31eb24c1e306dce64401e3dc2a5bce4733b50546d151639b0da" - [[package]] name = "clippy-utilities" version = "0.2.0" @@ -609,7 +603,7 @@ dependencies = [ "async-trait", "bincode", "bytes", - "clippy-utilities 0.2.0", + "clippy-utilities", "curp-external-api", "curp-test-utils", "dashmap", @@ -872,7 +866,7 @@ dependencies = [ "async-trait", "bincode", "bytes", - "clippy-utilities 0.2.0", + "clippy-utilities", "madsim-tokio", "opentelemetry", "parking_lot", @@ -1621,7 +1615,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c013377d78bca1015c5c9d56a4f65f0ab3257cbd8b3af0b98e44af889010dbb" dependencies = [ - "clippy-utilities 0.2.0", + "clippy-utilities", ] [[package]] @@ -3394,7 +3388,7 @@ name = "utils" version = "0.1.0" dependencies = [ "async-trait", - "clippy-utilities 0.2.0", + "clippy-utilities", "dashmap", "derive_builder 0.13.0", "getset", @@ -3812,7 +3806,7 @@ dependencies = [ "axum", "bytes", "clap", - "clippy-utilities 0.1.0", + "clippy-utilities", "crc32fast", "crossbeam-skiplist", "curp", @@ -3870,7 +3864,7 @@ name = "xline-client" version = "0.1.0" dependencies = [ "anyhow", - "clippy-utilities 0.1.0", + "clippy-utilities", "curp", "futures", "getrandom", diff --git a/crates/curp/src/client/stream.rs b/crates/curp/src/client/stream.rs index 3cf790b58..86d9e6b7f 100644 --- a/crates/curp/src/client/stream.rs +++ b/crates/curp/src/client/stream.rs @@ -3,9 +3,8 @@ use std::{sync::Arc, time::Duration}; use futures::Future; use tracing::{debug, warn}; -use crate::rpc::{connect::ConnectApi, CurpError, Redirect}; - use super::state::State; +use crate::rpc::{connect::ConnectApi, CurpError, Redirect}; /// Stream client config #[derive(Debug)] diff --git a/crates/xline-client/Cargo.toml b/crates/xline-client/Cargo.toml index d7429d9b2..e8cd6d193 100644 --- a/crates/xline-client/Cargo.toml +++ b/crates/xline-client/Cargo.toml @@ -12,7 +12,7 @@ keywords = ["Client", "Xline", "RPC"] [dependencies] anyhow = "1.0.81" -clippy-utilities = "0.1.0" +clippy-utilities = "0.2.0" curp = { path = "../curp" } futures = "0.3.25" getrandom = "0.2" diff --git a/crates/xline-client/src/lease_gen.rs b/crates/xline-client/src/lease_gen.rs index 170460109..b59a5d547 100644 --- a/crates/xline-client/src/lease_gen.rs +++ b/crates/xline-client/src/lease_gen.rs @@ -1,6 +1,6 @@ use std::sync::atomic::{AtomicU64, Ordering}; -use clippy_utilities::Cast; +use clippy_utilities::NumericCast; /// Generator of unique lease id /// Note that this Lease Id generation method may cause collisions, @@ -37,7 +37,7 @@ impl LeaseIdGenerator { return self.next(); } // set the highest bit to 0 as we need only need positive values - (id & 0x7fff_ffff_ffff_ffff).cast() + (id & 0x7fff_ffff_ffff_ffff).numeric_cast() } } diff --git a/crates/xline/Cargo.toml b/crates/xline/Cargo.toml index f1fd724ad..4031ed976 100644 --- a/crates/xline/Cargo.toml +++ b/crates/xline/Cargo.toml @@ -18,7 +18,7 @@ async-trait = "0.1.53" axum = "0.6.20" bytes = "1.4.0" clap = { version = "4", features = ["derive"] } -clippy-utilities = "0.1.0" +clippy-utilities = "0.2.0" crc32fast = "1.3.2" crossbeam-skiplist = "0.1.1" curp = { path = "../curp", version = "0.1.0", features = ["client-metrics"] } diff --git a/crates/xline/src/id_gen.rs b/crates/xline/src/id_gen.rs index ff4c20716..a84addc48 100644 --- a/crates/xline/src/id_gen.rs +++ b/crates/xline/src/id_gen.rs @@ -4,7 +4,7 @@ use std::{ time::{SystemTime, UNIX_EPOCH}, }; -use clippy_utilities::{Cast, OverflowArithmetic}; +use clippy_utilities::{NumericCast, OverflowArithmetic}; use curp::members::ServerId; /// Generator of unique id @@ -30,7 +30,7 @@ impl IdGenerator { .as_millis(); ts &= (u128::MAX.overflowing_shr(88).0); // lower 40 bits (128 - 40) ts = ts.overflowing_shl(8).0; // shift left 8 bits - let suffix = AtomicU64::new(ts.cast()); + let suffix = AtomicU64::new(ts.numeric_cast()); Self { prefix, suffix } } @@ -38,7 +38,7 @@ impl IdGenerator { pub(crate) fn next(&self) -> i64 { let suffix = self.suffix.fetch_add(1, Ordering::Relaxed); let id = self.prefix | suffix; - (id & 0x7fff_ffff_ffff_ffff).cast() + (id & 0x7fff_ffff_ffff_ffff).numeric_cast() } } diff --git a/crates/xline/src/metrics.rs b/crates/xline/src/metrics.rs index 0fe84be29..3bacc729c 100644 --- a/crates/xline/src/metrics.rs +++ b/crates/xline/src/metrics.rs @@ -1,4 +1,4 @@ -use clippy_utilities::Cast; +use clippy_utilities::NumericCast; use opentelemetry::{ metrics::{Counter, MetricsError}, KeyValue, @@ -68,7 +68,7 @@ impl Metrics { } if let Some(limit) = limit { - observer.observe_u64(&fd_limit, limit.cast(), &[]); + observer.observe_u64(&fd_limit, limit.numeric_cast(), &[]); } })?; diff --git a/crates/xline/src/restore.rs b/crates/xline/src/restore.rs index 6c7870520..2801d5867 100644 --- a/crates/xline/src/restore.rs +++ b/crates/xline/src/restore.rs @@ -2,7 +2,7 @@ use std::path::{Path, PathBuf}; use anyhow::Result; use bytes::BytesMut; -use clippy_utilities::Cast; +use clippy_utilities::NumericCast; use engine::{Engine, EngineType, Snapshot, SnapshotApi, StorageEngine}; use tokio_util::io::read_buf; use utils::table_names::XLINE_TABLES; @@ -22,7 +22,7 @@ pub async fn restore, D: Into>( let mut snapshot_f = tokio::fs::File::open(snapshot_path).await?; let tmp_path = format!("/tmp/snapshot-{}", uuid::Uuid::new_v4()); let mut rocks_snapshot = Snapshot::new_for_receiving(EngineType::Rocks((&tmp_path).into()))?; - let mut buf = BytesMut::with_capacity(MAINTENANCE_SNAPSHOT_CHUNK_SIZE.cast()); + let mut buf = BytesMut::with_capacity(MAINTENANCE_SNAPSHOT_CHUNK_SIZE.numeric_cast()); while let Ok(n) = read_buf(&mut snapshot_f, &mut buf).await { if n == 0 { break; diff --git a/crates/xline/src/server/command.rs b/crates/xline/src/server/command.rs index 754c4d49d..792b0ad69 100644 --- a/crates/xline/src/server/command.rs +++ b/crates/xline/src/server/command.rs @@ -109,7 +109,7 @@ where /// functions used to estimate request write size mod size_estimate { - use clippy_utilities::{Cast, OverflowArithmetic}; + use clippy_utilities::{NumericCast, OverflowArithmetic}; use xlineapi::{PutRequest, Request, RequestWrapper, TxnRequest}; /// Estimate the put size @@ -118,7 +118,7 @@ mod size_estimate { let kv_size = req.key.len().overflow_add(req.value.len()).overflow_add(32); // size of `KeyValue` struct 1010 // padding(1008) + cf_handle(2) .overflow_add(rev_size.overflow_mul(2)) - .overflow_add(kv_size.cast()) + .overflow_add(kv_size.numeric_cast()) } /// Estimate the txn size diff --git a/crates/xline/src/server/lease_server.rs b/crates/xline/src/server/lease_server.rs index 63db2c559..5b46a1627 100644 --- a/crates/xline/src/server/lease_server.rs +++ b/crates/xline/src/server/lease_server.rs @@ -1,7 +1,7 @@ use std::{pin::Pin, sync::Arc, time::Duration}; use async_stream::{stream, try_stream}; -use clippy_utilities::Cast; +use clippy_utilities::NumericCast; use curp::members::ClusterInfo; use futures::stream::Stream; use tokio::time; @@ -369,8 +369,8 @@ where let res = LeaseTimeToLiveResponse { header: Some(self.lease_storage.gen_header()), id: time_to_live_req.id, - ttl: lease.remaining().as_secs().cast(), - granted_ttl: lease.ttl().as_secs().cast(), + ttl: lease.remaining().as_secs().numeric_cast(), + granted_ttl: lease.ttl().as_secs().numeric_cast(), keys, }; return Ok(tonic::Response::new(res)); diff --git a/crates/xline/src/server/maintenance.rs b/crates/xline/src/server/maintenance.rs index b662b3fb2..8ea586818 100644 --- a/crates/xline/src/server/maintenance.rs +++ b/crates/xline/src/server/maintenance.rs @@ -2,7 +2,7 @@ use std::{fmt::Debug, pin::Pin, sync::Arc}; use async_stream::try_stream; use bytes::BytesMut; -use clippy_utilities::{Cast, OverflowArithmetic}; +use clippy_utilities::{NumericCast, OverflowArithmetic}; use curp::{cmd::CommandExecutor as _, members::ClusterInfo, server::RawCurp}; use engine::SnapshotApi; use futures::stream::Stream; @@ -150,13 +150,13 @@ where let response = StatusResponse { header: Some(self.header_gen.gen_header()), version: env!("CARGO_PKG_VERSION").to_owned(), - db_size: size.cast(), + db_size: size.numeric_cast(), leader: leader.unwrap_or(0), // None means this member believes there is no leader raft_index: commit_index, raft_term: term, raft_applied_index: last_applied, errors, - db_size_in_use: size.cast(), + db_size_in_use: size.numeric_cast(), is_learner, }; Ok(tonic::Response::new(response)) @@ -251,7 +251,7 @@ fn snapshot_stream( let mut checksum_gen = Sha256::new(); while remain_size > 0 { let buf_size = std::cmp::min(MAINTENANCE_SNAPSHOT_CHUNK_SIZE, remain_size); - let mut buf = BytesMut::with_capacity(buf_size.cast()); + let mut buf = BytesMut::with_capacity(buf_size.numeric_cast()); remain_size = remain_size.overflow_sub(buf_size); snapshot.read_buf_exact(&mut buf).await.map_err(|_e| {tonic::Status::internal("snapshot read failed")})?; // etcd client will use the size of the snapshot to determine whether checksum is included, @@ -259,7 +259,7 @@ fn snapshot_stream( // of 512 bytes let padding = MIN_PAGE_SIZE.overflow_sub(buf_size.overflow_rem(MIN_PAGE_SIZE)); if padding != 0 { - buf.extend_from_slice(&vec![0; padding.cast()]); + buf.extend_from_slice(&vec![0; padding.numeric_cast()]); } checksum_gen.update(&buf); yield SnapshotResponse { @@ -309,12 +309,12 @@ mod test { recv_data.append(&mut data?.blob); } assert_eq!( - recv_data.len() % MIN_PAGE_SIZE.cast::(), + recv_data.len() % MIN_PAGE_SIZE.numeric_cast::(), Sha256::output_size() ); let mut snap2 = persistent.get_snapshot(snapshot_path).unwrap(); - let size = snap2.size().cast(); + let size = snap2.size().numeric_cast(); let mut snap2_data = BytesMut::with_capacity(size); snap2.read_buf_exact(&mut snap2_data).await.unwrap(); let snap1_data = recv_data[..size].to_vec(); diff --git a/crates/xline/src/server/xline_server.rs b/crates/xline/src/server/xline_server.rs index b92a25ec9..65c694505 100644 --- a/crates/xline/src/server/xline_server.rs +++ b/crates/xline/src/server/xline_server.rs @@ -1,7 +1,7 @@ use std::{sync::Arc, time::Duration}; use anyhow::{anyhow, Result}; -use clippy_utilities::{Cast, OverflowArithmetic}; +use clippy_utilities::{NumericCast, OverflowArithmetic}; use curp::{ client::ClientBuilder as CurpClientBuilder, members::{get_cluster_info_from_remote, ClusterInfo}, @@ -187,12 +187,12 @@ impl XlineServer { heartbeat_interval: Duration, candidate_timeout_ticks: u8, ) -> Arc { - let min_ttl = 3 * heartbeat_interval * candidate_timeout_ticks.cast() / 2; + let min_ttl = 3 * heartbeat_interval * candidate_timeout_ticks.numeric_cast() / 2; // Safe ceiling let min_ttl_secs = min_ttl .as_secs() - .overflow_add((min_ttl.subsec_nanos() > 0).cast()); - Arc::new(LeaseCollection::new(min_ttl_secs.cast())) + .overflow_add(u64::from(min_ttl.subsec_nanos() > 0)); + Arc::new(LeaseCollection::new(min_ttl_secs.numeric_cast())) } /// Construct underlying storages, including `KvStore`, `LeaseStore`, `AuthStore` diff --git a/crates/xline/src/storage/auth_store/store.rs b/crates/xline/src/storage/auth_store/store.rs index 347e0df47..52ccd1e2c 100644 --- a/crates/xline/src/storage/auth_store/store.rs +++ b/crates/xline/src/storage/auth_store/store.rs @@ -7,7 +7,7 @@ use std::{ }, }; -use clippy_utilities::Cast; +use clippy_utilities::NumericCast; use itertools::Itertools; use jsonwebtoken::{DecodingKey, EncodingKey}; use log::debug; @@ -294,7 +294,7 @@ where debug!("handle_auth_status"); AuthStatusResponse { header: Some(self.header_gen.gen_auth_header()), - auth_revision: self.revision().cast(), + auth_revision: self.revision().numeric_cast(), enabled: self.is_enabled(), } } diff --git a/crates/xline/src/storage/kv_store.rs b/crates/xline/src/storage/kv_store.rs index d3029aa0d..61c91eb84 100644 --- a/crates/xline/src/storage/kv_store.rs +++ b/crates/xline/src/storage/kv_store.rs @@ -7,7 +7,7 @@ use std::{ }, }; -use clippy_utilities::{Cast, OverflowArithmetic}; +use clippy_utilities::{NumericCast, OverflowArithmetic}; use prost::Message; use tokio::sync::mpsc; use tracing::{debug, warn}; @@ -585,12 +585,12 @@ where &req.key, &req.range_end, req.revision, - storage_fetch_limit.cast(), + storage_fetch_limit.numeric_cast(), req.count_only, )?; let mut response = RangeResponse { header: Some(self.header_gen.gen_header()), - count: total.cast(), + count: total.numeric_cast(), ..RangeResponse::default() }; if kvs.is_empty() { @@ -606,9 +606,9 @@ where ); Self::sort_kvs(&mut kvs, req.sort_order(), req.sort_target()); - if (req.limit > 0) && (kvs.len() > req.limit.cast()) { + if (req.limit > 0) && (kvs.len() > req.limit.numeric_cast()) { response.more = true; - kvs.truncate(req.limit.cast()); + kvs.truncate(req.limit.numeric_cast()); } if req.keys_only { kvs.iter_mut().for_each(|kv| kv.value.clear()); @@ -648,7 +648,7 @@ where header: Some(self.header_gen.gen_header()), ..DeleteRangeResponse::default() }; - response.deleted = prev_kvs.len().cast(); + response.deleted = prev_kvs.len().numeric_cast(); if req.prev_kv { response.prev_kvs = prev_kvs; } @@ -782,7 +782,7 @@ where continue; } }; - sub_revision = sub_revision.overflow_add(events.len().cast()); + sub_revision = sub_revision.overflow_add(events.len().numeric_cast()); all_events.append(&mut events); all_ops.append(&mut ops); } diff --git a/crates/xline/src/storage/kvwatcher.rs b/crates/xline/src/storage/kvwatcher.rs index dc008019f..803ac3c59 100644 --- a/crates/xline/src/storage/kvwatcher.rs +++ b/crates/xline/src/storage/kvwatcher.rs @@ -604,7 +604,7 @@ mod test { use std::{collections::BTreeMap, time::Duration}; - use clippy_utilities::{Cast, OverflowArithmetic}; + use clippy_utilities::{NumericCast, OverflowArithmetic}; use test_macros::abort_on_panic; use tokio::time::{sleep, timeout}; use utils::config::EngineConfig; @@ -673,7 +673,7 @@ mod test { db.as_ref(), "foo", vec![i], - i.overflow_add(2).cast(), + i.overflow_add(2).numeric_cast(), ) .await; } @@ -737,7 +737,14 @@ mod test { }); for i in 0..100_u8 { - put(store.as_ref(), db.as_ref(), "foo", vec![i], i.cast()).await; + put( + store.as_ref(), + db.as_ref(), + "foo", + vec![i], + i.numeric_cast(), + ) + .await; } handle.await.unwrap(); drop(store); diff --git a/crates/xline/src/storage/lease_store/lease_collection.rs b/crates/xline/src/storage/lease_store/lease_collection.rs index 1ebf84d55..677bbb94f 100644 --- a/crates/xline/src/storage/lease_store/lease_collection.rs +++ b/crates/xline/src/storage/lease_store/lease_collection.rs @@ -3,7 +3,7 @@ use std::{ time::{Duration, Instant}, }; -use clippy_utilities::Cast; +use clippy_utilities::NumericCast; use itertools::Itertools; use parking_lot::RwLock; use utils::parking_lot_lock::RwLockMap; @@ -74,7 +74,7 @@ impl LeaseCollection { return Err(ExecuteError::LeaseExpired(lease_id)); } let expiry = lease.refresh(Duration::default()); - let ttl = lease.ttl().as_secs().cast(); + let ttl = lease.ttl().as_secs().numeric_cast(); (expiry, ttl) }; let _ignore = inner.expired_queue.update(lease_id, expiry); @@ -133,7 +133,7 @@ impl LeaseCollection { /// Grant a lease pub(crate) fn grant(&self, lease_id: i64, ttl: i64, is_leader: bool) -> PbLease { - let mut lease = Lease::new(lease_id, ttl.max(self.min_ttl).cast()); + let mut lease = Lease::new(lease_id, ttl.max(self.min_ttl).numeric_cast()); self.inner.map_write(|mut inner| { if is_leader { let expiry = lease.refresh(Duration::ZERO); @@ -145,8 +145,8 @@ impl LeaseCollection { }); PbLease { id: lease.id(), - ttl: lease.ttl().as_secs().cast(), - remaining_ttl: lease.remaining_ttl().as_secs().cast(), + ttl: lease.ttl().as_secs().numeric_cast(), + remaining_ttl: lease.remaining_ttl().as_secs().numeric_cast(), } }