num-traits icon indicating copy to clipboard operation
num-traits copied to clipboard

Consider modifying the Mul<Self, Output = Self> bound on WrappingMul

Open aerkiaga opened this issue 1 year ago • 2 comments

Currently, the WrappingMul trait has a Mul<Self, Output = Self> bound on it. This is incompatible with compile-time large numeric types types that use const generics to increase their width upon multiplication rather than panicking or overflowing, but which may still want to provide a wrapping version of the operation. I propose removing the Output = Self associated type bound from it.

This is ~not~ a breaking change.

aerkiaga avatar Jan 09 '25 00:01 aerkiaga

It is a breaking change, because users can no longer rely on that equivalence.

use num_traits::WrappingMul;

pub trait WrappingMul2: Sized + std::ops::Mul<Self> {
    fn wrapping_mul(&self, v: &Self) -> Self;
}

fn mul<T: WrappingMul>(x: T, y: T) -> T {
    // We know Mul::Output is T
    x * y
}

fn mul2<T: WrappingMul2>(x: T, y: T) -> T {
    x * y
//  ^^^^^ expected type parameter `T`, found associated type
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ea08daf166ec856c0470515b7e9e51d5

cuviper avatar Jan 09 '25 22:01 cuviper

for somebody it is breaking change, for others, hopefully, we can see next major version bump in optimistic roadmap?

dzmitry-lahoda avatar Feb 10 '25 19:02 dzmitry-lahoda