human: inline

This commit is contained in:
hinto.janai 2023-03-17 20:57:56 -04:00
parent a9287e1902
commit 64773c2856
No known key found for this signature in database
GPG key ID: D47CE05FA175A499

View file

@ -36,18 +36,22 @@ impl Default for HumanTime {
} }
impl HumanTime { impl HumanTime {
#[inline(always)]
pub const fn new() -> HumanTime { pub const fn new() -> HumanTime {
HumanTime(ZERO_SECONDS) HumanTime(ZERO_SECONDS)
} }
#[inline(always)]
pub const fn into_human(d: Duration) -> HumanTime { pub const fn into_human(d: Duration) -> HumanTime {
HumanTime(d) HumanTime(d)
} }
#[inline(always)]
pub const fn from_u64(u: u64) -> HumanTime { pub const fn from_u64(u: u64) -> HumanTime {
HumanTime(Duration::from_secs(u)) HumanTime(Duration::from_secs(u))
} }
#[inline(always)]
fn plural(f: &mut std::fmt::Formatter, started: &mut bool, name: &str, value: u64) -> std::fmt::Result { fn plural(f: &mut std::fmt::Formatter, started: &mut bool, name: &str, value: u64) -> std::fmt::Result {
if value > 0 { if value > 0 {
if *started { if *started {
@ -110,15 +114,19 @@ impl std::fmt::Display for HumanNumber {
} }
impl HumanNumber { impl HumanNumber {
#[inline(always)]
pub fn unknown() -> Self { pub fn unknown() -> Self {
Self("???".to_string()) Self("???".to_string())
} }
#[inline(always)]
pub fn from_str(s: &str) -> Self { pub fn from_str(s: &str) -> Self {
Self(s.to_string()) Self(s.to_string())
} }
#[inline(always)]
pub fn to_hashrate(f: f32) -> Self { pub fn to_hashrate(f: f32) -> Self {
Self(format!("{} H/s", Self::from_f32(f))) Self(format!("{} H/s", Self::from_f32(f)))
} }
#[inline(always)]
pub fn to_percent(f: f32) -> Self { pub fn to_percent(f: f32) -> Self {
if f < 0.01 { if f < 0.01 {
Self("0%".to_string()) Self("0%".to_string())
@ -126,54 +134,67 @@ impl HumanNumber {
Self(format!("{:.2}%", f)) Self(format!("{:.2}%", f))
} }
} }
#[inline(always)]
pub fn to_percent_3_point(f: f32) -> Self { pub fn to_percent_3_point(f: f32) -> Self {
Self(format!("{:.3}%", f)) Self(format!("{:.3}%", f))
} }
#[inline(always)]
pub fn to_percent_no_fmt(f: f32) -> Self { pub fn to_percent_no_fmt(f: f32) -> Self {
Self(format!("{}%", f)) Self(format!("{}%", f))
} }
#[inline(always)]
pub fn from_f64_to_percent_3_point(f: f64) -> Self { pub fn from_f64_to_percent_3_point(f: f64) -> Self {
Self(format!("{:.3}%", f)) Self(format!("{:.3}%", f))
} }
#[inline(always)]
pub fn from_f64_to_percent_6_point(f: f64) -> Self { pub fn from_f64_to_percent_6_point(f: f64) -> Self {
Self(format!("{:.6}%", f)) Self(format!("{:.6}%", f))
} }
#[inline(always)]
pub fn from_f64_to_percent_9_point(f: f64) -> Self { pub fn from_f64_to_percent_9_point(f: f64) -> Self {
Self(format!("{:.9}%", f)) Self(format!("{:.9}%", f))
} }
#[inline(always)]
pub fn from_f64_to_percent_no_fmt(f: f64) -> Self { pub fn from_f64_to_percent_no_fmt(f: f64) -> Self {
Self(format!("{}%", f)) Self(format!("{}%", f))
} }
#[inline(always)]
pub fn from_f32(f: f32) -> Self { pub fn from_f32(f: f32) -> Self {
let mut buf = num_format::Buffer::new(); let mut buf = num_format::Buffer::new();
buf.write_formatted(&(f as u64), &LOCALE); buf.write_formatted(&(f as u64), &LOCALE);
Self(buf.as_str().to_string()) Self(buf.as_str().to_string())
} }
#[inline(always)]
pub fn from_f64(f: f64) -> Self { pub fn from_f64(f: f64) -> Self {
let mut buf = num_format::Buffer::new(); let mut buf = num_format::Buffer::new();
buf.write_formatted(&(f as u128), &LOCALE); buf.write_formatted(&(f as u128), &LOCALE);
Self(buf.as_str().to_string()) Self(buf.as_str().to_string())
} }
#[inline(always)]
pub fn from_u16(u: u16) -> Self { pub fn from_u16(u: u16) -> Self {
let mut buf = num_format::Buffer::new(); let mut buf = num_format::Buffer::new();
buf.write_formatted(&u, &LOCALE); buf.write_formatted(&u, &LOCALE);
Self(buf.as_str().to_string()) Self(buf.as_str().to_string())
} }
#[inline(always)]
pub fn from_u32(u: u32) -> Self { pub fn from_u32(u: u32) -> Self {
let mut buf = num_format::Buffer::new(); let mut buf = num_format::Buffer::new();
buf.write_formatted(&u, &LOCALE); buf.write_formatted(&u, &LOCALE);
Self(buf.as_str().to_string()) Self(buf.as_str().to_string())
} }
#[inline(always)]
pub fn from_u64(u: u64) -> Self { pub fn from_u64(u: u64) -> Self {
let mut buf = num_format::Buffer::new(); let mut buf = num_format::Buffer::new();
buf.write_formatted(&u, &LOCALE); buf.write_formatted(&u, &LOCALE);
Self(buf.as_str().to_string()) Self(buf.as_str().to_string())
} }
#[inline(always)]
pub fn from_u128(u: u128) -> Self { pub fn from_u128(u: u128) -> Self {
let mut buf = num_format::Buffer::new(); let mut buf = num_format::Buffer::new();
buf.write_formatted(&u, &LOCALE); buf.write_formatted(&u, &LOCALE);
Self(buf.as_str().to_string()) Self(buf.as_str().to_string())
} }
#[inline(always)]
pub fn from_hashrate(array: [Option<f32>; 3]) -> Self { pub fn from_hashrate(array: [Option<f32>; 3]) -> Self {
let mut string = "[".to_string(); let mut string = "[".to_string();
let mut buf = num_format::Buffer::new(); let mut buf = num_format::Buffer::new();
@ -200,6 +221,7 @@ impl HumanNumber {
Self(string) Self(string)
} }
#[inline(always)]
pub fn from_load(array: [Option<f32>; 3]) -> Self { pub fn from_load(array: [Option<f32>; 3]) -> Self {
let mut string = "[".to_string(); let mut string = "[".to_string();
let mut n = 0; let mut n = 0;
@ -219,25 +241,30 @@ impl HumanNumber {
Self(string) Self(string)
} }
// [1_000_000] -> [1.000 MH/s] // [1_000_000] -> [1.000 MH/s]
#[inline(always)]
pub fn from_u64_to_megahash_3_point(hash: u64) -> Self { pub fn from_u64_to_megahash_3_point(hash: u64) -> Self {
let hash = (hash as f64)/1_000_000.0; let hash = (hash as f64)/1_000_000.0;
let hash = format!("{:.3} MH/s", hash); let hash = format!("{:.3} MH/s", hash);
Self(hash) Self(hash)
} }
// [1_000_000_000] -> [1.000 GH/s] // [1_000_000_000] -> [1.000 GH/s]
#[inline(always)]
pub fn from_u64_to_gigahash_3_point(hash: u64) -> Self { pub fn from_u64_to_gigahash_3_point(hash: u64) -> Self {
let hash = (hash as f64)/1_000_000_000.0; let hash = (hash as f64)/1_000_000_000.0;
let hash = format!("{:.3} GH/s", hash); let hash = format!("{:.3} GH/s", hash);
Self(hash) Self(hash)
} }
#[inline(always)]
pub fn from_f64_12_point(f: f64) -> Self { pub fn from_f64_12_point(f: f64) -> Self {
let f = format!("{:.12}", f); let f = format!("{:.12}", f);
Self(f) Self(f)
} }
#[inline(always)]
pub fn from_f64_no_fmt(f: f64) -> Self { pub fn from_f64_no_fmt(f: f64) -> Self {
let f = format!("{}", f); let f = format!("{}", f);
Self(f) Self(f)
} }
#[inline(always)]
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
self.0.as_str() self.0.as_str()
} }