Skip to main content

nil_core/military/unit/stats/
speed.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use derive_more::Display;
5use nil_util::{ConstDeref, F64Math};
6use serde::{Deserialize, Serialize};
7use std::cmp::Ordering;
8
9/// Represents how many fields a unit can travel in one round.
10#[derive(Copy, Debug, Display, Deserialize, Serialize, ConstDeref, F64Math)]
11#[derive_const(Clone, Default, PartialEq, PartialOrd)]
12#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
13pub struct Speed(f64);
14
15impl Speed {
16  #[inline]
17  pub const fn new(value: f64) -> Self {
18    debug_assert!(value >= 0.0);
19    debug_assert!(value.is_finite());
20    Self(value.max(0.0))
21  }
22}
23
24const impl From<Speed> for f64 {
25  fn from(value: Speed) -> Self {
26    value.0
27  }
28}
29
30const impl PartialEq<f64> for Speed {
31  fn eq(&self, other: &f64) -> bool {
32    self.0.eq(other)
33  }
34}
35
36const impl PartialOrd<f64> for Speed {
37  fn partial_cmp(&self, other: &f64) -> Option<Ordering> {
38    self.0.partial_cmp(other)
39  }
40}