impl Checked* and Saturating for Wrapping<T>

This commit is contained in:
Yoan Lecoq 2017-04-29 08:22:44 +02:00
parent f249edf546
commit 9c3e9791d2
2 changed files with 35 additions and 0 deletions

View File

@ -1,4 +1,5 @@
use std::ops::{Add, Sub, Mul, Div};
use std::num::Wrapping;
/// Performs addition that returns `None` instead of wrapping around on
/// overflow.
@ -89,3 +90,30 @@ checked_impl!(CheckedDiv, checked_div, i16);
checked_impl!(CheckedDiv, checked_div, i32);
checked_impl!(CheckedDiv, checked_div, i64);
checked_impl!(CheckedDiv, checked_div, isize);
// Was skeptical at first, since checked ops somewhat defeat the point of
// Wrapping<T>, but there are more pros than cons IMO :
// - These are methods, so users still have to be explicit about their intent;
// - Wrapping<T> could be used for to enforce wrapping semantics _most of the time_,
// and only have a handful of places where they want to perform checked ops;
// - This allows Wrapping<T> to implement PrimInt.
impl<T: CheckedAdd> CheckedAdd for Wrapping<T> where Wrapping<T>: Add<Output = Wrapping<T>>{
fn checked_add(&self, v: &Self) -> Option<Self> {
self.0.checked_add(&v.0).map(Wrapping)
}
}
impl<T: CheckedSub> CheckedSub for Wrapping<T> where Wrapping<T>: Sub<Output = Wrapping<T>> {
fn checked_sub(&self, v: &Self) -> Option<Self> {
self.0.checked_sub(&v.0).map(Wrapping)
}
}
impl<T: CheckedMul> CheckedMul for Wrapping<T> where Wrapping<T>: Mul<Output = Wrapping<T>>{
fn checked_mul(&self, v: &Self) -> Option<Self> {
self.0.checked_mul(&v.0).map(Wrapping)
}
}
impl<T: CheckedDiv> CheckedDiv for Wrapping<T> where Wrapping<T>: Div<Output = Wrapping<T>> {
fn checked_div(&self, v: &Self) -> Option<Self> {
self.0.checked_div(&v.0).map(Wrapping)
}
}

View File

@ -1,3 +1,5 @@
use std::num::Wrapping;
/// Saturating math operations
pub trait Saturating {
/// Saturating addition operator.
@ -26,3 +28,8 @@ macro_rules! saturating_impl {
}
saturating_impl!(Saturating for isize usize i8 u8 i16 u16 i32 u32 i64 u64);
impl<T: Saturating> Saturating for Wrapping<T> {
fn saturating_add(self, v: Self) -> Self { Wrapping(self.0.saturating_add(v.0)) }
fn saturating_sub(self, v: Self) -> Self { Wrapping(self.0.saturating_sub(v.0)) }
}