loosen Rhs constraint on MulAssign

This commit is contained in:
Trevor Spiteri 2020-09-22 19:09:41 +02:00
parent 0aa8af089f
commit a0ea3f2c60
3 changed files with 25 additions and 7 deletions

View File

@ -80,8 +80,11 @@ The conversions supported cover the following cases.
### Version 1.3.0 news (unreleased) ### Version 1.3.0 news (unreleased)
* The [`MulAssign`] implementation on fixed-point numbers now
accepts an rhs fixed-point number with a different number of
fractional bits from `self`.
* The following methods were added to all fixed-point types, to the * The following methods were added to all fixed-point types, to the
[`Fixed`][tf-1-3] trait, and to the [`Wrapping`][w-1-3] wrapper: [`Fixed`][tf-ma-1-3] trait, and to the [`Wrapping`][w-ma-1-3] wrapper:
* [`mul_add`][f-ma-1-3], [`checked_mul_add`][f-cma-1-3], * [`mul_add`][f-ma-1-3], [`checked_mul_add`][f-cma-1-3],
[`saturating_mul_add`][f-sma-1-3], [`saturating_mul_add`][f-sma-1-3],
[`wrapping_mul_add`][f-wma-1-3], [`wrapping_mul_add`][f-wma-1-3],
@ -90,14 +93,15 @@ The conversions supported cover the following cases.
providing arithmetic methods that panic on overflow even when providing arithmetic methods that panic on overflow even when
debug assertions are disabled. debug assertions are disabled.
[`MulAssign`]: https://doc.rust-lang.org/nightly/core/ops/trait.MulAssign.html
[f-cma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_mul_add [f-cma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_mul_add
[f-ma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.mul_add [f-ma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.mul_add
[f-oma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.overflowing_mul_add [f-oma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.overflowing_mul_add
[f-sma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.saturating_mul_add [f-sma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.saturating_mul_add
[f-wma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.wrapping_mul_add [f-wma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.wrapping_mul_add
[feat-un-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/#experimental-optional-features [feat-un-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/#experimental-optional-features
[tf-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.Fixed.html [tf-ma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.Fixed.html#tymethod.mul_add
[w-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.Wrapping.html [w-ma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.Wrapping.html#method.mul_add
### Version 1.2.0 news (2020-09-02) ### Version 1.2.0 news (2020-09-02)

View File

@ -8,6 +8,9 @@ as-is, without any warranty. -->
Version 1.3.0 (unreleased) Version 1.3.0 (unreleased)
========================== ==========================
* The [`MulAssign`] implementation on fixed-point numbers now
accepts an rhs fixed-point number with a different number of
fractional bits from `self`.
* The following methods were added to all fixed-point types, to the * The following methods were added to all fixed-point types, to the
[`Fixed`][tf-1-3] trait, and to the [`Wrapping`][w-1-3] wrapper: [`Fixed`][tf-1-3] trait, and to the [`Wrapping`][w-1-3] wrapper:
* [`mul_add`][f-ma-1-3], [`checked_mul_add`][f-cma-1-3], * [`mul_add`][f-ma-1-3], [`checked_mul_add`][f-cma-1-3],
@ -407,3 +410,5 @@ Version 0.1.0 (2018-08-10)
for the number of fractional bits. for the number of fractional bits.
* Many methods and trait implementations available for primitive * Many methods and trait implementations available for primitive
integers are now also supported by the fixed-point numbers. integers are now also supported by the fixed-point numbers.
[`MulAssign`]: https://doc.rust-lang.org/nightly/core/ops/trait.MulAssign.html

View File

@ -238,14 +238,23 @@ macro_rules! fixed_arith {
refs! { impl Mul for $Fixed($LeEqU) { mul } } refs! { impl Mul for $Fixed($LeEqU) { mul } }
impl<Frac: $LeEqU> MulAssign<$Fixed<Frac>> for $Fixed<Frac> { impl<Frac, RhsFrac: $LeEqU> MulAssign<$Fixed<RhsFrac>> for $Fixed<Frac> {
#[inline] #[inline]
fn mul_assign(&mut self, rhs: $Fixed<Frac>) { fn mul_assign(&mut self, rhs: $Fixed<RhsFrac>) {
*self = (*self).mul(rhs) let (ans, overflow) = self.to_bits().mul_overflow(rhs.to_bits(), RhsFrac::U32);
debug_assert!(!overflow, "overflow");
*self = Self::from_bits(ans);
} }
} }
refs_assign! { impl MulAssign for $Fixed($LeEqU) { mul_assign } } impl<Frac, RhsFrac: $LeEqU> MulAssign<&'_ $Fixed<RhsFrac>> for $Fixed<Frac> {
#[inline]
fn mul_assign(&mut self, rhs: &$Fixed<RhsFrac>) {
let (ans, overflow) = self.to_bits().mul_overflow(rhs.to_bits(), RhsFrac::U32);
debug_assert!(!overflow, "overflow");
*self = Self::from_bits(ans);
}
}
impl<Frac: $LeEqU> Div<$Fixed<Frac>> for $Fixed<Frac> { impl<Frac: $LeEqU> Div<$Fixed<Frac>> for $Fixed<Frac> {
type Output = $Fixed<Frac>; type Output = $Fixed<Frac>;