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