nil_core/resources/
gold.rs1use derive_more::Display;
5use nil_util::{ConstDeref, F64Math};
6use serde::{Deserialize, Serialize};
7
8#[derive(Copy, Debug, Display, Deserialize, Serialize, ConstDeref, F64Math)]
9#[derive_const(Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
10#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
11pub struct Gold(u32);
12
13impl Gold {
14 pub const MIN: Self = Self::new(0);
15 pub const MAX: Self = Self::new(u32::MAX);
16
17 #[inline]
18 pub const fn new(value: u32) -> Self {
19 Self(value)
20 }
21
22 #[inline]
23 pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
24 self.0.checked_sub(rhs.0).map(Self::new)
25 }
26}
27
28const impl From<u32> for Gold {
29 fn from(value: u32) -> Self {
30 Self::new(value)
31 }
32}
33
34const impl From<Gold> for u32 {
35 fn from(value: Gold) -> Self {
36 value.0
37 }
38}
39
40const impl From<f64> for Gold {
41 fn from(value: f64) -> Self {
42 debug_assert!(value.is_finite());
43 debug_assert!(value >= 0.0);
44 Self(value.trunc() as u32)
45 }
46}
47
48const impl From<Gold> for f64 {
49 fn from(value: Gold) -> Self {
50 f64::from(value.0)
51 }
52}