nil_core/continent/
distance.rs1use crate::military::maneuver::distance::ManeuverDistance;
5use derive_more::Display;
6use nil_util::ConstDeref;
7use serde::{Deserialize, Serialize};
8use std::ops::{Add, AddAssign, Sub, SubAssign};
9
10#[derive(Copy, Debug, Display, Deserialize, Serialize, ConstDeref)]
11#[derive_const(Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
12pub struct Distance(u8);
13
14impl Distance {
15 #[inline]
16 pub const fn new(distance: u8) -> Self {
17 Self(distance)
18 }
19}
20
21const impl From<u8> for Distance {
22 fn from(value: u8) -> Self {
23 Self(value)
24 }
25}
26
27const impl From<Distance> for u8 {
28 fn from(value: Distance) -> Self {
29 value.0
30 }
31}
32
33const impl From<Distance> for i16 {
34 fn from(value: Distance) -> Self {
35 i16::from(value.0)
36 }
37}
38
39const impl From<Distance> for f64 {
40 fn from(value: Distance) -> Self {
41 f64::from(value.0)
42 }
43}
44
45const impl PartialEq<u8> for Distance {
46 fn eq(&self, other: &u8) -> bool {
47 self.0.eq(other)
48 }
49}
50
51const impl Add for Distance {
52 type Output = Distance;
53
54 fn add(self, rhs: Self) -> Self::Output {
55 Self(self.0.saturating_add(rhs.0))
56 }
57}
58
59const impl AddAssign for Distance {
60 fn add_assign(&mut self, rhs: Self) {
61 *self = *self + rhs;
62 }
63}
64
65const impl Sub for Distance {
66 type Output = Distance;
67
68 fn sub(self, rhs: Self) -> Self::Output {
69 Self(self.0.saturating_sub(rhs.0))
70 }
71}
72
73const impl SubAssign for Distance {
74 fn sub_assign(&mut self, rhs: Self) {
75 *self = *self - rhs;
76 }
77}
78
79const impl Add<u8> for Distance {
80 type Output = Distance;
81
82 fn add(self, rhs: u8) -> Self::Output {
83 Self(self.0.saturating_add(rhs))
84 }
85}
86
87const impl AddAssign<u8> for Distance {
88 fn add_assign(&mut self, rhs: u8) {
89 *self = *self + rhs;
90 }
91}
92
93const impl Sub<u8> for Distance {
94 type Output = Distance;
95
96 fn sub(self, rhs: u8) -> Self::Output {
97 Self(self.0.saturating_sub(rhs))
98 }
99}
100
101const impl Sub<ManeuverDistance> for Distance {
102 type Output = ManeuverDistance;
103
104 fn sub(self, rhs: ManeuverDistance) -> Self::Output {
105 ManeuverDistance::from(f64::from(self) - f64::from(rhs))
106 }
107}
108
109const impl SubAssign<u8> for Distance {
110 fn sub_assign(&mut self, rhs: u8) {
111 *self = *self - rhs;
112 }
113}