Sfoglia il codice sorgente

impl Wrapping ops for Wrapping<T>

Yoan Lecoq 8 anni fa
parent
commit
47b8c7b5ae
1 ha cambiato i file con 18 aggiunte e 0 eliminazioni
  1. 18 0
      traits/src/ops/wrapping.rs

+ 18 - 0
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<T: WrappingAdd> WrappingAdd for Wrapping<T> where Wrapping<T>: Add<Output = Wrapping<T>> {
+    fn wrapping_add(&self, v: &Self) -> Self {
+        Wrapping(self.0.wrapping_add(&v.0))
+    }
+}
+impl<T: WrappingSub> WrappingSub for Wrapping<T> where Wrapping<T>: Sub<Output = Wrapping<T>> {
+    fn wrapping_sub(&self, v: &Self) -> Self {
+        Wrapping(self.0.wrapping_sub(&v.0))
+    }
+}
+impl<T: WrappingMul> WrappingMul for Wrapping<T> where Wrapping<T>: Mul<Output = Wrapping<T>> {
+    fn wrapping_mul(&self, v: &Self) -> Self {
+        Wrapping(self.0.wrapping_mul(&v.0))
+    }
+}
+
 
 #[test]
 fn test_wrapping_traits() {