-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathhashrate.rs
More file actions
135 lines (119 loc) · 3.91 KB
/
hashrate.rs
File metadata and controls
135 lines (119 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::{
fmt::{Display, Formatter},
ops::Div,
str::FromStr,
};
use measurements::Power;
#[cfg(feature = "python")]
use pyo3::prelude::*;
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "python", pyclass(from_py_object, str, module = "asic_rs"))]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum HashRateUnit {
Hash,
KiloHash,
MegaHash,
GigaHash,
#[default]
TeraHash,
PetaHash,
ExaHash,
ZettaHash,
YottaHash,
}
impl HashRateUnit {
fn to_multiplier(&self) -> f64 {
match self {
HashRateUnit::Hash => 1e0,
HashRateUnit::KiloHash => 1e3,
HashRateUnit::MegaHash => 1e6,
HashRateUnit::GigaHash => 1e9,
HashRateUnit::TeraHash => 1e12,
HashRateUnit::PetaHash => 1e15,
HashRateUnit::ExaHash => 1e18,
HashRateUnit::ZettaHash => 1e21,
HashRateUnit::YottaHash => 1e24,
}
}
}
impl FromStr for HashRateUnit {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let normalized = s.trim().to_ascii_uppercase().replace([' ', '_'], "");
match normalized.as_str() {
"HASH" | "H" | "HS" | "H/S" => Ok(HashRateUnit::Hash),
"KILOHASH" | "KH" | "KHS" | "KH/S" => Ok(HashRateUnit::KiloHash),
"MEGAHASH" | "MH" | "MHS" | "MH/S" => Ok(HashRateUnit::MegaHash),
"GIGAHASH" | "GH" | "GHS" | "GH/S" => Ok(HashRateUnit::GigaHash),
"TERAHASH" | "TH" | "THS" | "TH/S" => Ok(HashRateUnit::TeraHash),
"PETAHASH" | "PH" | "PHS" | "PH/S" => Ok(HashRateUnit::PetaHash),
"EXAHASH" | "EH" | "EHS" | "EH/S" => Ok(HashRateUnit::ExaHash),
"ZETTAHASH" | "ZH" | "ZHS" | "ZH/S" => Ok(HashRateUnit::ZettaHash),
"YOTTAHASH" | "YH" | "YHS" | "YH/S" => Ok(HashRateUnit::YottaHash),
_ => Err(()),
}
}
}
impl Display for HashRateUnit {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
HashRateUnit::Hash => write!(f, "H/s"),
HashRateUnit::KiloHash => write!(f, "KH/s"),
HashRateUnit::MegaHash => write!(f, "MH/s"),
HashRateUnit::GigaHash => write!(f, "GH/s"),
HashRateUnit::TeraHash => write!(f, "TH/s"),
HashRateUnit::PetaHash => write!(f, "PH/s"),
HashRateUnit::ExaHash => write!(f, "EH/s"),
HashRateUnit::ZettaHash => write!(f, "ZH/s"),
HashRateUnit::YottaHash => write!(f, "YH/s"),
}
}
}
#[cfg_attr(
feature = "python",
pyclass(from_py_object, get_all, module = "asic_rs")
)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HashRate {
/// The current amount of hashes being computed
pub value: f64,
/// The unit of the hashes in value
pub unit: HashRateUnit,
/// The algorithm of the computed hashes
pub algo: String,
}
impl HashRate {
pub fn as_unit(self, unit: HashRateUnit) -> Self {
let base = self.value * self.unit.to_multiplier(); // Convert to base unit (e.g., bytes)
Self {
value: base / unit.clone().to_multiplier(),
unit,
algo: self.algo,
}
}
}
impl Display for HashRate {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let precision = f.precision();
match precision {
Some(precision) => {
write!(f, "{:.*} {}", precision, self.value, self.unit)
}
None => {
write!(f, "{} {}", self.value, self.unit)
}
}
}
}
impl PartialEq for HashRate {
fn eq(&self, other: &Self) -> bool {
other.clone().as_unit(self.unit.clone()).value == self.value
}
}
impl Eq for HashRate {}
impl Div<HashRate> for Power {
type Output = f64;
fn div(self, hash_rate: HashRate) -> Self::Output {
self.as_watts() / hash_rate.value
}
}