diff options
author | ClementTsang <clementjhtsang@gmail.com> | 2019-12-27 18:19:57 -0500 |
---|---|---|
committer | ClementTsang <clementjhtsang@gmail.com> | 2019-12-27 18:19:57 -0500 |
commit | 25d0ae45b4afd8c47f289076e548375a9a3aa902 (patch) | |
tree | 5e1fcdaa3400590918d1bcc5f0b81e1c4cd4e1e1 /src/utils/gen_util.rs | |
parent | a8bcccc8cf0c6292f779121666f01356b7c4da1c (diff) |
Some cleaning to avoid duplicate code
Diffstat (limited to 'src/utils/gen_util.rs')
-rw-r--r-- | src/utils/gen_util.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/utils/gen_util.rs b/src/utils/gen_util.rs new file mode 100644 index 00000000..30a257bc --- /dev/null +++ b/src/utils/gen_util.rs @@ -0,0 +1,47 @@ +use std::cmp::Ordering; + +pub fn float_min(a: f32, b: f32) -> f32 { + match a.partial_cmp(&b) { + Some(x) => match x { + Ordering::Greater => b, + Ordering::Less => a, + Ordering::Equal => a, + }, + None => a, + } +} + +pub fn float_max(a: f32, b: f32) -> f32 { + match a.partial_cmp(&b) { + Some(x) => match x { + Ordering::Greater => a, + Ordering::Less => b, + Ordering::Equal => a, + }, + None => a, + } +} + +/// Returns a tuple containing the value and the unit. In units of 1024. +/// This only supports up to a tebibyte. +pub fn get_exact_byte_values(bytes: u64) -> (f64, String) { + match bytes { + b if b < 1024 => (bytes as f64, "B".to_string()), + b if b < 1_048_576 => (bytes as f64 / 1024.0, "KiB".to_string()), + b if b < 1_073_741_824 => (bytes as f64 / 1_048_576.0, "MiB".to_string()), + b if b < 1_099_511_627_776 => (bytes as f64 / 1_073_741_824.0, "GiB".to_string()), + _ => (bytes as f64 / 1_099_511_627_776.0, "TiB".to_string()), + } +} + +/// Returns a tuple containing the value and the unit. In units of 1000. +/// This only supports up to a terabyte. Note the "byte" unit will have a space appended to match the others. +pub fn get_simple_byte_values(bytes: u64) -> (f64, String) { + match bytes { + b if b < 1000 => (bytes as f64, "B".to_string()), + b if b < 1_000_000 => (bytes as f64 / 1000.0, "KB".to_string()), + b if b < 1_000_000_000 => (bytes as f64 / 1_000_000.0, "MB".to_string()), + b if b < 1_000_000_000_000 => (bytes as f64 / 1_000_000_000.0, "GB".to_string()), + _ => (bytes as f64 / 1_000_000_000_000.0, "TB".to_string()), + } +} |