Skip to main content

nil_core/military/maneuver/
distance.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::continent::Distance;
5use crate::military::unit::stats::speed::Speed;
6use nil_util::{ConstDeref, F64Math};
7use serde::{Deserialize, Serialize};
8use std::cmp::Ordering;
9use std::ops::{Sub, SubAssign};
10
11#[derive(Copy, Debug, Deserialize, Serialize, ConstDeref, F64Math)]
12#[derive_const(Clone)]
13#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
14pub struct ManeuverDistance(f64);
15
16impl const From<ManeuverDistance> for f64 {
17  fn from(value: ManeuverDistance) -> Self {
18    value.0
19  }
20}
21
22impl const From<Distance> for ManeuverDistance {
23  fn from(distance: Distance) -> Self {
24    Self(f64::from(distance))
25  }
26}
27
28impl const PartialEq for ManeuverDistance {
29  fn eq(&self, other: &Self) -> bool {
30    matches!(self.0.total_cmp(&other.0), Ordering::Equal)
31  }
32}
33
34impl const Eq for ManeuverDistance {}
35
36impl const PartialOrd for ManeuverDistance {
37  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
38    Some(self.cmp(other))
39  }
40}
41
42impl const Ord for ManeuverDistance {
43  fn cmp(&self, other: &Self) -> Ordering {
44    self.0.total_cmp(&other.0)
45  }
46}
47
48impl const PartialEq<f64> for ManeuverDistance {
49  fn eq(&self, other: &f64) -> bool {
50    matches!(self.0.total_cmp(other), Ordering::Equal)
51  }
52}
53
54impl const PartialOrd<f64> for ManeuverDistance {
55  fn partial_cmp(&self, other: &f64) -> Option<Ordering> {
56    Some(self.0.total_cmp(other))
57  }
58}
59
60impl const Sub for ManeuverDistance {
61  type Output = ManeuverDistance;
62
63  fn sub(mut self, rhs: Self) -> Self::Output {
64    self -= rhs;
65    self
66  }
67}
68
69impl const Sub<Speed> for ManeuverDistance {
70  type Output = ManeuverDistance;
71
72  fn sub(mut self, rhs: Speed) -> Self::Output {
73    self -= rhs;
74    self
75  }
76}
77
78impl const SubAssign for ManeuverDistance {
79  fn sub_assign(&mut self, rhs: Self) {
80    *self = Self(self.0 - rhs.0);
81  }
82}
83
84impl const SubAssign<Speed> for ManeuverDistance {
85  fn sub_assign(&mut self, rhs: Speed) {
86    *self = Self(self.0 - f64::from(rhs));
87  }
88}