Skip to main content

nil_core/continent/
size.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use derive_more::Display;
5use nil_util::{ClampNew, ConstDeref};
6use serde::{Deserialize, Serialize};
7use std::cmp::Ordering;
8use std::num::NonZeroU8;
9
10#[derive(Copy, Debug, Display, Deserialize, Serialize, ClampNew, ConstDeref)]
11#[derive_const(Clone, PartialEq, Eq, PartialOrd, Ord)]
12#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
13pub struct ContinentSize(NonZeroU8);
14
15impl ContinentSize {
16  pub const MIN: ContinentSize = unsafe { Self::new_unchecked(100) };
17  pub const MAX: ContinentSize = unsafe { Self::new_unchecked(200) };
18
19  pub const fn new(size: u8) -> Self {
20    let size = size
21      .clamp(Self::MIN.0.get(), Self::MAX.0.get())
22      .next_multiple_of(10);
23
24    unsafe { Self::new_unchecked(size) }
25  }
26
27  /// # Safety
28  ///
29  /// The size must be between [`ContinentSize::MIN`] and [`ContinentSize::MAX`],
30  /// and be a multiple of 10.
31  #[inline]
32  pub const unsafe fn new_unchecked(size: u8) -> Self {
33    Self(unsafe { NonZeroU8::new_unchecked(size) })
34  }
35}
36
37const impl Default for ContinentSize {
38  fn default() -> Self {
39    Self::MIN
40  }
41}
42
43const impl From<u8> for ContinentSize {
44  fn from(value: u8) -> Self {
45    Self::new(value)
46  }
47}
48
49const impl From<ContinentSize> for u8 {
50  fn from(value: ContinentSize) -> Self {
51    value.0.get()
52  }
53}
54
55const impl From<ContinentSize> for u16 {
56  fn from(value: ContinentSize) -> Self {
57    u16::from(value.0.get())
58  }
59}
60
61const impl From<ContinentSize> for usize {
62  fn from(value: ContinentSize) -> Self {
63    usize::from(value.0.get())
64  }
65}
66
67const impl From<ContinentSize> for i16 {
68  fn from(value: ContinentSize) -> Self {
69    i16::from(value.0.get())
70  }
71}
72
73const impl From<ContinentSize> for f64 {
74  fn from(value: ContinentSize) -> Self {
75    f64::from(value.0.get())
76  }
77}
78
79const impl PartialEq<u8> for ContinentSize {
80  fn eq(&self, other: &u8) -> bool {
81    self.0.get().eq(other)
82  }
83}
84
85const impl PartialEq<usize> for ContinentSize {
86  fn eq(&self, other: &usize) -> bool {
87    usize::from(self.0.get()).eq(other)
88  }
89}
90
91const impl PartialEq<ContinentSize> for usize {
92  fn eq(&self, other: &ContinentSize) -> bool {
93    self.eq(&usize::from(other.0.get()))
94  }
95}
96
97const impl PartialOrd<u8> for ContinentSize {
98  fn partial_cmp(&self, other: &u8) -> Option<Ordering> {
99    self.0.get().partial_cmp(other)
100  }
101}
102
103const impl PartialOrd<usize> for ContinentSize {
104  fn partial_cmp(&self, other: &usize) -> Option<Ordering> {
105    usize::from(self.0.get()).partial_cmp(other)
106  }
107}
108
109const impl PartialOrd<ContinentSize> for usize {
110  fn partial_cmp(&self, other: &ContinentSize) -> Option<Ordering> {
111    self.partial_cmp(&usize::from(other.0.get()))
112  }
113}