-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Number type with a simpler Option<f32> (#144)
* Update RELEASES.md * Implement extension traits for Option<f32> * Replace usages of Number with Option<f32> * Rename module from number.rs to math.rs * Note removal of associated traits * Make behavior of maybe_sub consistent * Thanks clippy! * dead_code XD * Update math operations to be consistent with previous impl * Reduce churn in PR * Follow line length calculation spec correctly * Fix undefined math logic for MaybeMath trait
- Loading branch information
1 parent
712df30
commit 7283137
Showing
17 changed files
with
310 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
//! Contains numberical helper traits and functions | ||
|
||
/// A trait to conveniently calculate minimums and maximums when some data may not be defined | ||
/// | ||
/// If the left-hand value is [`None`], these operations return [`None`]. | ||
/// If the right-hand value is [`None`], it is treated as zero. | ||
pub(crate) trait MaybeMath<In, Out> { | ||
/// Returns the minimum of `self` and `rhs` | ||
fn maybe_min(self, rhs: In) -> Out; | ||
|
||
/// Returns the maximum of `self` and `rhs` | ||
fn maybe_max(self, rhs: In) -> Out; | ||
|
||
/// Adds `self` and `rhs`. | ||
fn maybe_add(self, rhs: In) -> Out; | ||
|
||
/// Subracts rhs from `self`, treating [`None`] values as default | ||
fn maybe_sub(self, rhs: In) -> Out; | ||
} | ||
|
||
impl MaybeMath<Option<f32>, Option<f32>> for Option<f32> { | ||
fn maybe_min(self, rhs: Option<f32>) -> Option<f32> { | ||
match (self, rhs) { | ||
(Some(l), Some(r)) => Some(l.min(r)), | ||
(Some(_l), None) => self, | ||
(None, Some(_r)) => None, | ||
(None, None) => None, | ||
} | ||
} | ||
|
||
fn maybe_max(self, rhs: Option<f32>) -> Option<f32> { | ||
match (self, rhs) { | ||
(Some(l), Some(r)) => Some(l.max(r)), | ||
(Some(_l), None) => self, | ||
(None, Some(_r)) => None, | ||
(None, None) => None, | ||
} | ||
} | ||
|
||
fn maybe_add(self, rhs: Option<f32>) -> Option<f32> { | ||
match (self, rhs) { | ||
(Some(l), Some(r)) => Some(l + r), | ||
(Some(_l), None) => self, | ||
(None, Some(_r)) => None, | ||
(None, None) => None, | ||
} | ||
} | ||
|
||
fn maybe_sub(self, rhs: Option<f32>) -> Option<f32> { | ||
match (self, rhs) { | ||
(Some(l), Some(r)) => Some(l - r), | ||
(Some(_l), None) => self, | ||
(None, Some(_r)) => None, | ||
(None, None) => None, | ||
} | ||
} | ||
} | ||
|
||
impl MaybeMath<f32, Option<f32>> for Option<f32> { | ||
// The logic here is subtle and critical; | ||
// explicit match statements make it more clear what's going on | ||
#[allow(clippy::manual_map)] | ||
fn maybe_min(self, rhs: f32) -> Option<f32> { | ||
match self { | ||
Some(val) => Some(val.min(rhs)), | ||
None => None, | ||
} | ||
} | ||
|
||
#[allow(clippy::manual_map)] | ||
fn maybe_max(self, rhs: f32) -> Option<f32> { | ||
match self { | ||
Some(val) => Some(val.max(rhs)), | ||
None => None, | ||
} | ||
} | ||
|
||
#[allow(clippy::manual_map)] | ||
fn maybe_add(self, rhs: f32) -> Option<f32> { | ||
match self { | ||
Some(val) => Some(val + rhs), | ||
None => None, | ||
} | ||
} | ||
|
||
#[allow(clippy::manual_map)] | ||
fn maybe_sub(self, rhs: f32) -> Option<f32> { | ||
match self { | ||
Some(val) => Some(val - rhs), | ||
None => None, | ||
} | ||
} | ||
} | ||
|
||
impl MaybeMath<Option<f32>, f32> for f32 { | ||
fn maybe_min(self, rhs: Option<f32>) -> f32 { | ||
match rhs { | ||
Some(val) => self.min(val), | ||
None => self, | ||
} | ||
} | ||
|
||
fn maybe_max(self, rhs: Option<f32>) -> f32 { | ||
match rhs { | ||
Some(val) => self.max(val), | ||
None => self, | ||
} | ||
} | ||
|
||
fn maybe_add(self, rhs: Option<f32>) -> f32 { | ||
match rhs { | ||
Some(val) => self + val, | ||
None => self, | ||
} | ||
} | ||
|
||
fn maybe_sub(self, rhs: Option<f32>) -> f32 { | ||
match rhs { | ||
Some(val) => self - val, | ||
None => self, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.