-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use checked arithmetic in RuneUpdater (#3423)
- Loading branch information
Showing
4 changed files
with
190 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
use { | ||
super::*, | ||
std::{ | ||
cmp::{PartialEq, PartialOrd}, | ||
ops::{Add, AddAssign, Div, Rem, Sub, SubAssign}, | ||
}, | ||
}; | ||
|
||
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Default, Serialize, Deserialize)] | ||
pub(super) struct Lot(pub(super) u128); | ||
|
||
impl Lot { | ||
#[cfg(test)] | ||
const MAX: Self = Self(u128::MAX); | ||
|
||
pub(super) fn n(self) -> u128 { | ||
self.0 | ||
} | ||
|
||
fn checked_add(self, rhs: Self) -> Option<Self> { | ||
Some(Self(self.0.checked_add(rhs.0)?)) | ||
} | ||
|
||
fn checked_sub(self, rhs: Self) -> Option<Self> { | ||
Some(Self(self.0.checked_sub(rhs.0)?)) | ||
} | ||
} | ||
|
||
impl TryFrom<Lot> for usize { | ||
type Error = <usize as TryFrom<u128>>::Error; | ||
fn try_from(lot: Lot) -> Result<Self, Self::Error> { | ||
usize::try_from(lot.0) | ||
} | ||
} | ||
|
||
impl Add for Lot { | ||
type Output = Self; | ||
fn add(self, other: Self) -> Self::Output { | ||
self.checked_add(other).expect("lot overflow") | ||
} | ||
} | ||
|
||
impl AddAssign for Lot { | ||
fn add_assign(&mut self, other: Self) { | ||
*self = *self + other; | ||
} | ||
} | ||
|
||
impl Add<u128> for Lot { | ||
type Output = Self; | ||
fn add(self, other: u128) -> Self::Output { | ||
self + Lot(other) | ||
} | ||
} | ||
|
||
impl AddAssign<u128> for Lot { | ||
fn add_assign(&mut self, other: u128) { | ||
*self += Lot(other); | ||
} | ||
} | ||
|
||
impl Sub for Lot { | ||
type Output = Self; | ||
fn sub(self, other: Self) -> Self::Output { | ||
self.checked_sub(other).expect("lot underflow") | ||
} | ||
} | ||
|
||
impl SubAssign for Lot { | ||
fn sub_assign(&mut self, other: Self) { | ||
*self = *self - other; | ||
} | ||
} | ||
|
||
impl Div<u128> for Lot { | ||
type Output = Self; | ||
fn div(self, other: u128) -> Self::Output { | ||
Lot(self.0 / other) | ||
} | ||
} | ||
|
||
impl Rem<u128> for Lot { | ||
type Output = Self; | ||
fn rem(self, other: u128) -> Self::Output { | ||
Lot(self.0 % other) | ||
} | ||
} | ||
|
||
impl PartialEq<u128> for Lot { | ||
fn eq(&self, other: &u128) -> bool { | ||
self.0 == *other | ||
} | ||
} | ||
|
||
impl PartialOrd<u128> for Lot { | ||
fn partial_cmp(&self, other: &u128) -> Option<std::cmp::Ordering> { | ||
self.0.partial_cmp(other) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
#[should_panic(expected = "lot overflow")] | ||
fn add() { | ||
let _ = Lot::MAX + 1; | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "lot overflow")] | ||
fn add_assign() { | ||
let mut l = Lot::MAX; | ||
l += Lot(1); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "lot overflow")] | ||
fn add_u128() { | ||
let _ = Lot::MAX + 1; | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "lot overflow")] | ||
fn add_assign_u128() { | ||
let mut l = Lot::MAX; | ||
l += 1; | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "lot underflow")] | ||
fn sub() { | ||
let _ = Lot(0) - Lot(1); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "lot underflow")] | ||
fn sub_assign() { | ||
let mut l = Lot(0); | ||
l -= Lot(1); | ||
} | ||
|
||
#[test] | ||
fn div() { | ||
assert_eq!(Lot(100) / 2, Lot(50)); | ||
} | ||
|
||
#[test] | ||
fn rem() { | ||
assert_eq!(Lot(77) % 8, Lot(5)); | ||
} | ||
|
||
#[test] | ||
fn partial_eq() { | ||
assert_eq!(Lot(100), 100); | ||
} | ||
|
||
#[test] | ||
fn partial_ord() { | ||
assert!(Lot(100) > 10); | ||
} | ||
} |
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