craydate/
clamped_float.rs

1/// A floating point value that is clamped to be within `LOW` and `HIGH`.
2#[derive(Debug, PartialEq, PartialOrd)]
3#[repr(transparent)]
4pub struct ClampedFloatInclusive<const LOW: i32, const HIGH: i32>(f32);
5impl<const LOW: i32, const HIGH: i32> ClampedFloatInclusive<LOW, HIGH> {
6  /// Constructs a new ClampedFloatInclusive.
7  pub fn new(f: f32) -> Self {
8    ClampedFloatInclusive(f.clamp(LOW as f32, HIGH as f32))
9  }
10
11  pub(crate) fn as_mut_ptr(&mut self) -> *mut f32 {
12    &mut self.0 as *mut f32
13  }
14
15  /// Converts to an `f32` without bounds.
16  pub fn to_f32(self) -> f32 {
17    self.0
18  }
19}
20
21impl<const LOW: i32, const HIGH: i32> From<f32> for ClampedFloatInclusive<LOW, HIGH> {
22  fn from(f: f32) -> Self {
23    ClampedFloatInclusive::new(f)
24  }
25}
26impl<const LOW: i32, const HIGH: i32> From<ClampedFloatInclusive<LOW, HIGH>> for f32 {
27  fn from(c: ClampedFloatInclusive<LOW, HIGH>) -> Self {
28    c.to_f32()
29  }
30}
31
32impl<const LOW: i32, const HIGH: i32> Default for ClampedFloatInclusive<LOW, HIGH> {
33  fn default() -> Self {
34    Self(LOW as f32)
35  }
36}
37
38impl<const LOW: i32, const HIGH: i32> core::ops::Add for ClampedFloatInclusive<LOW, HIGH> {
39  type Output = Self;
40
41  fn add(self, rhs: Self) -> Self::Output {
42    Self::new(self.0.add(rhs.0))
43  }
44}
45impl<const LOW: i32, const HIGH: i32> core::ops::Sub for ClampedFloatInclusive<LOW, HIGH> {
46  type Output = Self;
47
48  fn sub(self, rhs: Self) -> Self::Output {
49    Self::new(self.0.sub(rhs.0))
50  }
51}
52impl<const LOW: i32, const HIGH: i32> core::ops::Mul for ClampedFloatInclusive<LOW, HIGH> {
53  type Output = Self;
54
55  fn mul(self, rhs: Self) -> Self::Output {
56    Self::new(self.0.mul(rhs.0))
57  }
58}
59impl<const LOW: i32, const HIGH: i32> core::ops::Div for ClampedFloatInclusive<LOW, HIGH> {
60  type Output = Self;
61
62  fn div(self, rhs: Self) -> Self::Output {
63    Self::new(self.0.div(rhs.0))
64  }
65}
66
67impl<const LOW: i32, const HIGH: i32> core::ops::Add<f32> for ClampedFloatInclusive<LOW, HIGH> {
68  type Output = Self;
69
70  fn add(self, rhs: f32) -> Self::Output {
71    Self::new(self.0.add(rhs))
72  }
73}
74impl<const LOW: i32, const HIGH: i32> core::ops::Sub<f32> for ClampedFloatInclusive<LOW, HIGH> {
75  type Output = Self;
76
77  fn sub(self, rhs: f32) -> Self::Output {
78    Self::new(self.0.sub(rhs))
79  }
80}
81impl<const LOW: i32, const HIGH: i32> core::ops::Mul<f32> for ClampedFloatInclusive<LOW, HIGH> {
82  type Output = Self;
83
84  fn mul(self, rhs: f32) -> Self::Output {
85    Self::new(self.0.mul(rhs))
86  }
87}
88impl<const LOW: i32, const HIGH: i32> core::ops::Div<f32> for ClampedFloatInclusive<LOW, HIGH> {
89  type Output = Self;
90
91  fn div(self, rhs: f32) -> Self::Output {
92    Self::new(self.0.div(rhs))
93  }
94}
95
96impl<const LOW: i32, const HIGH: i32> core::fmt::Display for ClampedFloatInclusive<LOW, HIGH> {
97  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
98    self.0.fmt(f)
99  }
100}