mitigate additional cast lints

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-07 07:14:09 +00:00
parent dcd7422c45
commit dfd13780df
4 changed files with 18 additions and 23 deletions

View file

@ -740,9 +740,6 @@ significant_drop_tightening = { level = "allow", priority = 1 } # TODO
pedantic = "warn" pedantic = "warn"
## some sadness ## 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 } doc_markdown = { level = "allow", priority = 1 }
enum_glob_use = { level = "allow", priority = 1 } enum_glob_use = { level = "allow", priority = 1 }
error_impl_error = { level = "allow", priority = 1 } error_impl_error = { level = "allow", priority = 1 }

View file

@ -60,7 +60,8 @@ pub fn usize_from_f64(val: f64) -> Result<usize, Error> {
return Err(Error::Arithmetic("Converting negative float to unsigned integer")); return Err(Error::Arithmetic("Converting negative float to unsigned integer"));
} }
Ok(val as usize) //SAFETY: <https://doc.rust-lang.org/std/primitive.f64.html#method.to_int_unchecked>
Ok(unsafe { val.to_int_unchecked::<usize>() })
} }
#[inline] #[inline]
@ -83,5 +84,5 @@ pub fn ruma_from_usize(val: usize) -> ruma::UInt {
#[inline] #[inline]
#[must_use] #[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 } pub fn usize_from_u64_truncated(val: u64) -> usize { val as usize }

View file

@ -2,7 +2,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
#[inline] #[inline]
#[must_use] #[must_use]
#[allow(clippy::as_conversions)] #[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
pub fn now_millis() -> u64 { pub fn now_millis() -> u64 {
UNIX_EPOCH UNIX_EPOCH
.elapsed() .elapsed()
@ -28,7 +28,7 @@ pub fn format(ts: SystemTime, str: &str) -> String {
} }
#[must_use] #[must_use]
#[allow(clippy::as_conversions)] #[allow(clippy::as_conversions, clippy::cast_possible_truncation, clippy::cast_sign_loss)]
pub fn pretty(d: Duration) -> String { pub fn pretty(d: Duration) -> String {
use Unit::*; 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 /// part is the largest Unit containing a non-zero value, the frac part is a
/// rational remainder left over. /// rational remainder left over.
#[must_use] #[must_use]
#[allow(clippy::as_conversions)] #[allow(clippy::as_conversions, clippy::cast_precision_loss)]
pub fn whole_and_frac(d: Duration) -> (Unit, f64) { pub fn whole_and_frac(d: Duration) -> (Unit, f64) {
use Unit::*; use Unit::*;

View file

@ -134,23 +134,21 @@ impl Engine {
.fetch_sub(1, std::sync::atomic::Ordering::Relaxed); .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<String> { pub fn memory_usage(&self) -> Result<String> {
let mut res = String::new(); let mut res = String::new();
let stats = get_memory_usage_stats(Some(&[&self.db]), Some(&[&self.row_cache])).or_else(or_else)?; 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!( writeln!(
res, res,
"Memory buffers: {:.2} MiB\nPending write: {:.2} MiB\nTable readers: {:.2} MiB\nRow cache: {:.2} MiB", "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, mibs(stats.mem_table_total),
stats.mem_table_unflushed as f64 / 1024.0 / 1024.0, mibs(stats.mem_table_unflushed),
stats.mem_table_readers_total as f64 / 1024.0 / 1024.0, mibs(stats.mem_table_readers_total),
self.row_cache.get_usage() as f64 / 1024.0 / 1024.0, mibs(u64::try_from(self.row_cache.get_usage())?),
) )?;
.expect("should be able to write to string buffer");
for (name, cache) in &*self.col_cache.read().expect("locked") { 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,) writeln!(res, "{} cache: {:.2} MiB", name, mibs(u64::try_from(cache.get_usage())?))?;
.expect("should be able to write to string buffer");
} }
Ok(res) Ok(res)
@ -214,8 +212,7 @@ impl Engine {
rfc2822_from_seconds(info.timestamp), rfc2822_from_seconds(info.timestamp),
info.size, info.size,
info.num_files, info.num_files,
) )?;
.expect("should be able to write to string buffer");
} }
Ok(res) Ok(res)
@ -226,16 +223,16 @@ impl Engine {
Err(e) => Ok(String::from(e)), Err(e) => Ok(String::from(e)),
Ok(files) => { Ok(files) => {
let mut res = String::new(); let mut res = String::new();
writeln!(res, "| lev | sst | keys | dels | size | column |").expect("written to string buffer"); writeln!(res, "| lev | sst | keys | dels | size | column |")?;
writeln!(res, "| ---: | :--- | ---: | ---: | ---: | :--- |").expect("written to string buffer"); writeln!(res, "| ---: | :--- | ---: | ---: | ---: | :--- |")?;
for file in files { for file in files {
writeln!( writeln!(
res, res,
"| {} | {:<13} | {:7}+ | {:4}- | {:9} | {} |", "| {} | {:<13} | {:7}+ | {:4}- | {:9} | {} |",
file.level, file.name, file.num_entries, file.num_deletions, file.size, file.column_family_name, 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) Ok(res)
}, },
} }