subweight_core/
traits.rs

1use serde::{Deserialize, Serialize};
2
3pub trait One {
4	fn one() -> Self;
5}
6
7pub trait Zero {
8	fn zero() -> Self;
9}
10
11impl One for u128 {
12	fn one() -> Self {
13		1
14	}
15}
16
17impl Zero for u128 {
18	fn zero() -> Self {
19		0
20	}
21}
22
23impl ValueFormatter for u128 {
24	fn format_scalar(&self) -> String {
25		crate::Dimension::fmt_scalar(*self)
26	}
27}
28
29pub trait ValueFormatter {
30	fn format_scalar(&self) -> String;
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd, Ord, Eq)]
34pub struct Weight {
35	pub time: u128,
36	pub proof: u128,
37}
38
39impl Zero for Weight {
40	fn zero() -> Self {
41		Self { time: 0, proof: 0 }
42	}
43}
44
45impl One for Weight {
46	fn one() -> Self {
47		Self { time: 1, proof: 1 }
48	}
49}
50
51impl From<(u128, u128)> for Weight {
52	fn from((time, proof): (u128, u128)) -> Self {
53		Self { time, proof }
54	}
55}
56
57impl From<Weight> for (u128, u128) {
58	fn from(val: Weight) -> Self {
59		(val.time, val.proof)
60	}
61}
62
63impl From<u128> for Weight {
64	fn from(time: u128) -> Self {
65		Self { time, proof: 0 }
66	}
67}
68
69impl ValueFormatter for Weight {
70	fn format_scalar(&self) -> String {
71		format!("({}, {})", self.time, self.proof)
72	}
73}
74
75impl core::fmt::Display for Weight {
76	fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
77		write!(f, "({}, {})", self.time, self.proof)
78	}
79}
80
81impl Weight {
82	pub fn mul_scalar(&self, other: u128) -> Self {
83		Self { time: self.time * other, proof: self.proof * other }
84	}
85}