diff --git a/Cargo.toml b/Cargo.toml index bf4f71c7..af9c719e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -740,9 +740,6 @@ significant_drop_tightening = { level = "allow", priority = 1 } # TODO pedantic = "warn" ## some sadness -cast_possible_truncation = { level = "allow", priority = 1 } -cast_precision_loss = { level = "allow", priority = 1 } -cast_sign_loss = { level = "allow", priority = 1 } doc_markdown = { level = "allow", priority = 1 } enum_glob_use = { level = "allow", priority = 1 } error_impl_error = { level = "allow", priority = 1 } diff --git a/src/core/utils/math.rs b/src/core/utils/math.rs index 3dc7e27f..155721e7 100644 --- a/src/core/utils/math.rs +++ b/src/core/utils/math.rs @@ -60,7 +60,8 @@ pub fn usize_from_f64(val: f64) -> Result { return Err(Error::Arithmetic("Converting negative float to unsigned integer")); } - Ok(val as usize) + //SAFETY: + Ok(unsafe { val.to_int_unchecked::() }) } #[inline] @@ -83,5 +84,5 @@ pub fn ruma_from_usize(val: usize) -> ruma::UInt { #[inline] #[must_use] -#[allow(clippy::as_conversions)] +#[allow(clippy::as_conversions, clippy::cast_possible_truncation)] pub fn usize_from_u64_truncated(val: u64) -> usize { val as usize } diff --git a/src/core/utils/time.rs b/src/core/utils/time.rs index bf6192b2..9a31632e 100644 --- a/src/core/utils/time.rs +++ b/src/core/utils/time.rs @@ -2,7 +2,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH}; #[inline] #[must_use] -#[allow(clippy::as_conversions)] +#[allow(clippy::as_conversions, clippy::cast_possible_truncation)] pub fn now_millis() -> u64 { UNIX_EPOCH .elapsed() @@ -28,7 +28,7 @@ pub fn format(ts: SystemTime, str: &str) -> String { } #[must_use] -#[allow(clippy::as_conversions)] +#[allow(clippy::as_conversions, clippy::cast_possible_truncation, clippy::cast_sign_loss)] pub fn pretty(d: Duration) -> String { use Unit::*; @@ -50,7 +50,7 @@ pub fn pretty(d: Duration) -> String { /// part is the largest Unit containing a non-zero value, the frac part is a /// rational remainder left over. #[must_use] -#[allow(clippy::as_conversions)] +#[allow(clippy::as_conversions, clippy::cast_precision_loss)] pub fn whole_and_frac(d: Duration) -> (Unit, f64) { use Unit::*; diff --git a/src/database/engine.rs b/src/database/engine.rs index 7c9522e1..53e39abc 100644 --- a/src/database/engine.rs +++ b/src/database/engine.rs @@ -134,23 +134,21 @@ impl Engine { .fetch_sub(1, std::sync::atomic::Ordering::Relaxed); } - #[allow(clippy::as_conversions, clippy::cast_sign_loss, clippy::cast_possible_truncation)] pub fn memory_usage(&self) -> Result { let mut res = String::new(); let stats = get_memory_usage_stats(Some(&[&self.db]), Some(&[&self.row_cache])).or_else(or_else)?; + let mibs = |input| f64::from(u32::try_from(input / 1024).unwrap_or(0)) / 1024.0; writeln!( res, "Memory buffers: {:.2} MiB\nPending write: {:.2} MiB\nTable readers: {:.2} MiB\nRow cache: {:.2} MiB", - stats.mem_table_total as f64 / 1024.0 / 1024.0, - stats.mem_table_unflushed as f64 / 1024.0 / 1024.0, - stats.mem_table_readers_total as f64 / 1024.0 / 1024.0, - self.row_cache.get_usage() as f64 / 1024.0 / 1024.0, - ) - .expect("should be able to write to string buffer"); + mibs(stats.mem_table_total), + mibs(stats.mem_table_unflushed), + mibs(stats.mem_table_readers_total), + mibs(u64::try_from(self.row_cache.get_usage())?), + )?; for (name, cache) in &*self.col_cache.read().expect("locked") { - writeln!(res, "{} cache: {:.2} MiB", name, cache.get_usage() as f64 / 1024.0 / 1024.0,) - .expect("should be able to write to string buffer"); + writeln!(res, "{} cache: {:.2} MiB", name, mibs(u64::try_from(cache.get_usage())?))?; } Ok(res) @@ -214,8 +212,7 @@ impl Engine { rfc2822_from_seconds(info.timestamp), info.size, info.num_files, - ) - .expect("should be able to write to string buffer"); + )?; } Ok(res) @@ -226,16 +223,16 @@ impl Engine { Err(e) => Ok(String::from(e)), Ok(files) => { let mut res = String::new(); - writeln!(res, "| lev | sst | keys | dels | size | column |").expect("written to string buffer"); - writeln!(res, "| ---: | :--- | ---: | ---: | ---: | :--- |").expect("written to string buffer"); + writeln!(res, "| lev | sst | keys | dels | size | column |")?; + writeln!(res, "| ---: | :--- | ---: | ---: | ---: | :--- |")?; for file in files { writeln!( res, "| {} | {:<13} | {:7}+ | {:4}- | {:9} | {} |", file.level, file.name, file.num_entries, file.num_deletions, file.size, file.column_family_name, - ) - .expect("should be able to writeln to string buffer"); + )?; } + Ok(res) }, }