nil_core/ranking/
score.rs1use derive_more::{Deref, From, Into};
5use nil_num::impl_mul_ceil;
6use serde::{Deserialize, Serialize};
7use std::cmp::Ordering;
8use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
9
10#[derive(
11 Clone,
12 Copy,
13 Debug,
14 Default,
15 Deref,
16 From,
17 Into,
18 PartialEq,
19 Eq,
20 PartialOrd,
21 Ord,
22 Deserialize,
23 Serialize,
24)]
25#[into(u32, f64)]
26pub struct Score(u32);
27
28impl Score {
29 pub const ZERO: Score = Score(0);
30
31 #[inline]
32 pub const fn new(value: u32) -> Self {
33 Self(value)
34 }
35}
36
37impl From<f64> for Score {
38 fn from(value: f64) -> Self {
39 Self::new(value as u32)
40 }
41}
42
43impl PartialEq<u32> for Score {
44 fn eq(&self, other: &u32) -> bool {
45 self.0.eq(other)
46 }
47}
48
49impl PartialOrd<u32> for Score {
50 fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
51 self.0.partial_cmp(other)
52 }
53}
54
55impl Add for Score {
56 type Output = Score;
57
58 fn add(self, rhs: Self) -> Self::Output {
59 Self(self.0.saturating_add(rhs.0))
60 }
61}
62
63impl AddAssign for Score {
64 fn add_assign(&mut self, rhs: Self) {
65 *self = *self + rhs;
66 }
67}
68
69impl Add<u32> for Score {
70 type Output = Score;
71
72 fn add(self, rhs: u32) -> Self::Output {
73 Self(self.0.saturating_add(rhs))
74 }
75}
76
77impl AddAssign<u32> for Score {
78 fn add_assign(&mut self, rhs: u32) {
79 *self = *self + rhs;
80 }
81}
82
83impl Sub for Score {
84 type Output = Score;
85
86 fn sub(self, rhs: Self) -> Self::Output {
87 Self(self.0.saturating_sub(rhs.0))
88 }
89}
90
91impl SubAssign for Score {
92 fn sub_assign(&mut self, rhs: Self) {
93 *self = *self - rhs;
94 }
95}
96
97impl Mul for Score {
98 type Output = Score;
99
100 fn mul(self, rhs: Score) -> Self::Output {
101 Self(self.0.saturating_mul(rhs.0))
102 }
103}
104
105impl Mul<u32> for Score {
106 type Output = Score;
107
108 fn mul(self, rhs: u32) -> Self::Output {
109 Self(self.0.saturating_mul(rhs))
110 }
111}
112
113impl Mul<f64> for Score {
114 type Output = f64;
115
116 fn mul(self, rhs: f64) -> Self::Output {
117 f64::from(self.0) * rhs
118 }
119}
120
121impl MulAssign for Score {
122 fn mul_assign(&mut self, rhs: Self) {
123 *self = *self * rhs;
124 }
125}
126
127impl_mul_ceil!(Score);