From 47b8c7b5ae1fc10eab28430f9c2878524dadb302 Mon Sep 17 00:00:00 2001 From: Yoan Lecoq Date: Sat, 29 Apr 2017 08:29:02 +0200 Subject: [PATCH] impl Wrapping ops for Wrapping --- traits/src/ops/wrapping.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/traits/src/ops/wrapping.rs b/traits/src/ops/wrapping.rs index 40b266d..cd68ac4 100644 --- a/traits/src/ops/wrapping.rs +++ b/traits/src/ops/wrapping.rs @@ -1,4 +1,5 @@ use std::ops::{Add, Sub, Mul}; +use std::num::Wrapping; macro_rules! wrapping_impl { ($trait_name:ident, $method:ident, $t:ty) => { @@ -76,6 +77,23 @@ wrapping_impl!(WrappingMul, wrapping_mul, i32); wrapping_impl!(WrappingMul, wrapping_mul, i64); wrapping_impl!(WrappingMul, wrapping_mul, isize); +// Well this is a bit funny, but all the more appropriate. +impl WrappingAdd for Wrapping where Wrapping: Add> { + fn wrapping_add(&self, v: &Self) -> Self { + Wrapping(self.0.wrapping_add(&v.0)) + } +} +impl WrappingSub for Wrapping where Wrapping: Sub> { + fn wrapping_sub(&self, v: &Self) -> Self { + Wrapping(self.0.wrapping_sub(&v.0)) + } +} +impl WrappingMul for Wrapping where Wrapping: Mul> { + fn wrapping_mul(&self, v: &Self) -> Self { + Wrapping(self.0.wrapping_mul(&v.0)) + } +} + #[test] fn test_wrapping_traits() {