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"
## 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 }

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"));
}
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]
@ -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 }

View file

@ -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::*;

View file

@ -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<String> {
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)
},
}