fix as conversions

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-07 06:17:58 +00:00
parent 7397064edd
commit dcd7422c45
11 changed files with 107 additions and 51 deletions

View file

@ -12,15 +12,24 @@ static JEMALLOC: jemalloc::Jemalloc = jemalloc::Jemalloc;
#[must_use]
pub fn memory_usage() -> String {
use mallctl::stats;
let allocated = stats::allocated::read().unwrap_or_default() as f64 / 1024.0 / 1024.0;
let active = stats::active::read().unwrap_or_default() as f64 / 1024.0 / 1024.0;
let mapped = stats::mapped::read().unwrap_or_default() as f64 / 1024.0 / 1024.0;
let metadata = stats::metadata::read().unwrap_or_default() as f64 / 1024.0 / 1024.0;
let resident = stats::resident::read().unwrap_or_default() as f64 / 1024.0 / 1024.0;
let retained = stats::retained::read().unwrap_or_default() as f64 / 1024.0 / 1024.0;
let mibs = |input: Result<usize, mallctl::Error>| {
let input = input.unwrap_or_default();
let kibs = input / 1024;
let kibs = u32::try_from(kibs).unwrap_or_default();
let kibs = f64::from(kibs);
kibs / 1024.0
};
let allocated = mibs(stats::allocated::read());
let active = mibs(stats::active::read());
let mapped = mibs(stats::mapped::read());
let metadata = mibs(stats::metadata::read());
let resident = mibs(stats::resident::read());
let retained = mibs(stats::retained::read());
format!(
"allocated: {allocated:.2} MiB\n active: {active:.2} MiB\n mapped: {mapped:.2} MiB\n metadata: {metadata:.2} \
MiB\n resident: {resident:.2} MiB\n retained: {retained:.2} MiB\n "
"allocated: {allocated:.2} MiB\nactive: {active:.2} MiB\nmapped: {mapped:.2} MiB\nmetadata: {metadata:.2} \
MiB\nresident: {resident:.2} MiB\nretained: {retained:.2} MiB\n"
)
}

View file

@ -2,6 +2,8 @@ use std::{cmp, time::Duration};
pub use checked_ops::checked_ops;
use crate::{Error, Result};
/// Checked arithmetic expression. Returns a Result<R, Error::Arithmetic>
#[macro_export]
macro_rules! checked {
@ -50,3 +52,36 @@ pub fn continue_exponential_backoff(min: Duration, max: Duration, elapsed: Durat
let min = cmp::min(min, max);
elapsed < min
}
#[inline]
#[allow(clippy::as_conversions)]
pub fn usize_from_f64(val: f64) -> Result<usize, Error> {
if val < 0.0 {
return Err(Error::Arithmetic("Converting negative float to unsigned integer"));
}
Ok(val as usize)
}
#[inline]
#[must_use]
pub fn usize_from_ruma(val: ruma::UInt) -> usize {
usize::try_from(val).expect("failed conversion from ruma::UInt to usize")
}
#[inline]
#[must_use]
pub fn ruma_from_u64(val: u64) -> ruma::UInt {
ruma::UInt::try_from(val).expect("failed conversion from u64 to ruma::UInt")
}
#[inline]
#[must_use]
pub fn ruma_from_usize(val: usize) -> ruma::UInt {
ruma::UInt::try_from(val).expect("failed conversion from usize to ruma::UInt")
}
#[inline]
#[must_use]
#[allow(clippy::as_conversions)]
pub fn usize_from_u64_truncated(val: u64) -> usize { val as usize }

View file

@ -28,11 +28,12 @@ pub fn format(ts: SystemTime, str: &str) -> String {
}
#[must_use]
#[allow(clippy::as_conversions)]
pub fn pretty(d: Duration) -> String {
use Unit::*;
let fmt = |w, f, u| format!("{w}.{f} {u}");
let gen64 = |w, f, u| fmt(w, (f * 100.0) as u64, u);
let gen64 = |w, f, u| fmt(w, (f * 100.0) as u32, u);
let gen128 = |w, f, u| gen64(u64::try_from(w).expect("u128 to u64"), f, u);
match whole_and_frac(d) {
(Days(whole), frac) => gen64(whole, frac, "days"),
@ -49,6 +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)]
pub fn whole_and_frac(d: Duration) -> (Unit, f64) {
use Unit::*;