Skip to content

Commit

Permalink
feat: hide generic wrapping impl in overflow_based module
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Mar 1, 2024
1 parent 8203d47 commit c508d4a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 18 deletions.
46 changes: 28 additions & 18 deletions corelib/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3010,25 +3010,35 @@ impl U256OverflowingMul of core::num::traits::OverflowingMul<u256> {
}

/// WrappingAdd implementations
impl TWrappingAdd<T, +core::num::traits::OverflowingAdd<T>> of core::num::traits::WrappingAdd<T> {
fn wrapping_add(self: T, v: T) -> T {
let (result, _) = self.overflowing_add(v);
result
}
}
impl U8WrappingAdd = core::num::traits::ops::wrapping::overflow_based::TWrappingAdd<u8>;
impl U16WrappingAdd = core::num::traits::ops::wrapping::overflow_based::TWrappingAdd<u16>;
impl U32WrappingAdd = core::num::traits::ops::wrapping::overflow_based::TWrappingAdd<u32>;
impl U64WrappingAdd = core::num::traits::ops::wrapping::overflow_based::TWrappingAdd<u64>;
impl U128WrappingAdd = core::num::traits::ops::wrapping::overflow_based::TWrappingAdd<u128>;
impl U256WrappingAdd = core::num::traits::ops::wrapping::overflow_based::TWrappingAdd<u256>;
impl I8WrappingAdd = core::num::traits::ops::wrapping::overflow_based::TWrappingAdd<i8>;
impl I16WrappingAdd = core::num::traits::ops::wrapping::overflow_based::TWrappingAdd<i16>;
impl I32WrappingAdd = core::num::traits::ops::wrapping::overflow_based::TWrappingAdd<i32>;
impl I64WrappingAdd = core::num::traits::ops::wrapping::overflow_based::TWrappingAdd<i64>;
impl I128WrappingAdd = core::num::traits::ops::wrapping::overflow_based::TWrappingAdd<i128>;

/// WrappingSub implementations
impl TWrappingSub<T, +core::num::traits::OverflowingSub<T>> of core::num::traits::WrappingSub<T> {
fn wrapping_sub(self: T, v: T) -> T {
let (result, _) = self.overflowing_sub(v);
result
}
}
impl U8WrappingSub = core::num::traits::ops::wrapping::overflow_based::TWrappingSub<u8>;
impl U16WrappingSub = core::num::traits::ops::wrapping::overflow_based::TWrappingSub<u16>;
impl U32WrappingSub = core::num::traits::ops::wrapping::overflow_based::TWrappingSub<u32>;
impl U64WrappingSub = core::num::traits::ops::wrapping::overflow_based::TWrappingSub<u64>;
impl U128WrappingSub = core::num::traits::ops::wrapping::overflow_based::TWrappingSub<u128>;
impl U256WrappingSub = core::num::traits::ops::wrapping::overflow_based::TWrappingSub<u256>;
impl I8WrappingSub = core::num::traits::ops::wrapping::overflow_based::TWrappingSub<i8>;
impl I16WrappingSub = core::num::traits::ops::wrapping::overflow_based::TWrappingSub<i16>;
impl I32WrappingSub = core::num::traits::ops::wrapping::overflow_based::TWrappingSub<i32>;
impl I64WrappingSub = core::num::traits::ops::wrapping::overflow_based::TWrappingSub<i64>;
impl I128WrappingSub = core::num::traits::ops::wrapping::overflow_based::TWrappingSub<i128>;

/// WrappingMul implementations
impl TWrappingMul<T, +core::num::traits::OverflowingMul<T>> of core::num::traits::WrappingMul<T> {
fn wrapping_mul(self: T, v: T) -> T {
let (result, _) = self.overflowing_mul(v);
result
}
}
impl U8WrappingMul = core::num::traits::ops::wrapping::overflow_based::TWrappingMul<u8>;
impl U16WrappingMul = core::num::traits::ops::wrapping::overflow_based::TWrappingMul<u16>;
impl U32WrappingMul = core::num::traits::ops::wrapping::overflow_based::TWrappingMul<u32>;
impl U64WrappingMul = core::num::traits::ops::wrapping::overflow_based::TWrappingMul<u64>;
impl U128WrappingMul = core::num::traits::ops::wrapping::overflow_based::TWrappingMul<u128>;
impl U256WrappingMul = core::num::traits::ops::wrapping::overflow_based::TWrappingMul<u256>;
29 changes: 29 additions & 0 deletions corelib/src/num/traits/ops/wrapping.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,32 @@ pub trait WrappingMul<T> {
/// of the type.
fn wrapping_mul(self: T, v: T) -> T;
}

pub(crate) mod overflow_based {
pub(crate) impl TWrappingAdd<
T, +core::num::traits::OverflowingAdd<T>
> of core::num::traits::WrappingAdd<T> {
fn wrapping_add(self: T, v: T) -> T {
let (result, _) = self.overflowing_add(v);
result
}
}

pub(crate) impl TWrappingSub<
T, +core::num::traits::OverflowingSub<T>
> of core::num::traits::WrappingSub<T> {
fn wrapping_sub(self: T, v: T) -> T {
let (result, _) = self.overflowing_sub(v);
result
}
}

pub(crate) impl TWrappingMul<
T, +core::num::traits::OverflowingMul<T>
> of core::num::traits::WrappingMul<T> {
fn wrapping_mul(self: T, v: T) -> T {
let (result, _) = self.overflowing_mul(v);
result
}
}
}

0 comments on commit c508d4a

Please sign in to comment.