simplify code for many method calls

This commit is contained in:
Trevor Spiteri 2019-08-16 21:16:55 +02:00
parent 8adaa2b5d1
commit 7b3b76f78c
6 changed files with 77 additions and 95 deletions

View File

@ -74,22 +74,22 @@ macro_rules! sealed_int {
#[inline]
fn checked_add(self, val: $Int) -> Option<$Int> {
<$Int>::checked_add(self, val)
self.checked_add(val)
}
#[inline]
fn checked_mul(self, val: $Int) -> Option<$Int> {
<$Int>::checked_mul(self, val)
self.checked_mul(val)
}
#[inline]
fn overflowing_add(self, val: $Int) -> ($Int, bool) {
<$Int>::overflowing_add(self, val)
self.overflowing_add(val)
}
#[inline]
fn leading_zeros(self) -> u32 {
<$Int>::leading_zeros(self)
self.leading_zeros()
}
#[inline]

View File

@ -343,9 +343,9 @@ assert_eq!(two_point_75.to_string(), \"2.8\");
impl<Frac> Default for $Fixed<Frac> {
#[inline]
fn default() -> $Fixed<Frac> {
fn default() -> Self {
$Fixed {
bits: <$Inner>::default(),
bits: Default::default(),
phantom: PhantomData
}
}
@ -447,7 +447,7 @@ let f = Fix::from_bits(0b11_0010);
assert_eq!(f.count_ones(), 3);
```
";
$Fixed($Inner) => fn count_ones(self) -> u32
$Fixed => fn count_ones(self) -> u32
);
delegate!(
"Returns the number of zeros in the binary
@ -463,7 +463,7 @@ let f = Fix::from_bits(!0b11_0010);
assert_eq!(f.count_zeros(), 3);
```
";
$Fixed($Inner) => fn count_zeros(self) -> u32
$Fixed => fn count_zeros(self) -> u32
);
delegate!(
"Returns the number of leading zeros in the binary
@ -481,7 +481,7 @@ assert_eq!(f.leading_zeros(), ",
" - 6);
```
";
$Fixed($Inner) => fn leading_zeros(self) -> u32
$Fixed => fn leading_zeros(self) -> u32
);
delegate!(
"Returns the number of trailing zeros in the binary
@ -497,7 +497,7 @@ let f = Fix::from_bits(0b10_0000);
assert_eq!(f.trailing_zeros(), 5);
```
";
$Fixed($Inner) => fn trailing_zeros(self) -> u32
$Fixed => fn trailing_zeros(self) -> u32
);
delegate!(
"Shifts to the left by *n* bits, wrapping the
@ -519,7 +519,7 @@ assert_eq!(bits.rotate_left(3), rot);
assert_eq!(Fix::from_bits(bits).rotate_left(3), Fix::from_bits(rot));
```
";
$Fixed($Inner) => fn rotate_left(self, n: u32)
$Fixed => fn rotate_left(self, n: u32)
);
delegate!(
"Shifts to the right by *n* bits, wrapping the
@ -541,7 +541,7 @@ assert_eq!(bits.rotate_right(3), rot);
assert_eq!(Fix::from_bits(bits).rotate_right(3), Fix::from_bits(rot));
```
";
$Fixed($Inner) => fn rotate_right(self, n: u32)
$Fixed => fn rotate_right(self, n: u32)
);
if_signed! {
@ -561,7 +561,7 @@ assert_eq!(five.abs(), five);
assert_eq!(minus_five.abs(), five);
```
";
$Fixed($Inner) => fn abs(self)
$Fixed => fn abs(self)
);
comment!(
@ -626,7 +626,7 @@ assert!(half.is_power_of_two());
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
";
$Fixed($Inner) => fn is_power_of_two(self) -> bool
$Fixed => fn is_power_of_two(self) -> bool
);
delegate!(
@ -653,7 +653,7 @@ assert_eq!(three_eights.next_power_of_two(), half);
assert_eq!(half.next_power_of_two(), half);
```
";
$Fixed($Inner) => fn next_power_of_two(self)
$Fixed => fn next_power_of_two(self)
);
comment!(
@ -678,7 +678,7 @@ assert!(Fix::max_value().checked_next_power_of_two().is_none());
";
#[inline]
pub fn checked_next_power_of_two(self) -> Option<$Fixed<Frac>> {
<$Inner>::checked_next_power_of_two(self.to_bits()).map(Self::from_bits)
self.to_bits().checked_next_power_of_two().map(Self::from_bits)
}
);
}
@ -701,7 +701,7 @@ assert!(!Fix::from_num(-5).is_positive());
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
";
$Fixed($Inner) => fn is_positive(self) -> bool
$Fixed => fn is_positive(self) -> bool
);
delegate!(
@ -720,7 +720,7 @@ assert!(Fix::from_num(-5).is_negative());
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
";
$Fixed($Inner) => fn is_negative(self) -> bool
$Fixed => fn is_negative(self) -> bool
);
}

View File

@ -74,33 +74,39 @@ macro_rules! delegate {
}
}
};
($($comment:expr),*; $Fixed:ident($Inner:ty) => fn $method:ident(self)) => {
($($comment:expr),*; $Fixed:ident => fn $method:ident(self)) => {
doc_comment! {
concat!($($comment),*);
#[inline]
pub fn $method(self) -> $Fixed<Frac> {
Self::from_bits(<$Inner>::$method(self.to_bits()))
Self::from_bits(self.to_bits().$method())
}
}
};
($($comment:expr),*; $Fixed:ident($Inner:ty) => fn $method:ident(self) -> $ret_ty:ty) => {
($($comment:expr),*; $Fixed:ident => fn $method:ident(self, rhs)) => {
doc_comment! {
concat!($($comment),*);
#[inline]
pub fn $method(self) -> $ret_ty {
<$Inner>::$method(self.to_bits())
pub fn $method(self, rhs: $Fixed<Frac>) -> $Fixed<Frac> {
Self::from_bits(self.to_bits().$method(rhs.to_bits()))
}
}
};
(
$($comment:expr),*;
$Fixed:ident($Inner:ty) => fn $method:ident(self, $param:ident: $param_ty:ty)
) => {
($($comment:expr),*; $Fixed:ident => fn $method:ident(self) -> $Ret:ty) => {
doc_comment! {
concat!($($comment),*);
#[inline]
pub fn $method(self, $param: $param_ty) -> $Fixed<Frac> {
Self::from_bits(<$Inner>::$method(self.to_bits(), $param))
pub fn $method(self) -> $Ret {
self.to_bits().$method()
}
}
};
($($comment:expr),*; $Fixed:ident => fn $method:ident(self, $param:ident: $Param:ty)) => {
doc_comment! {
concat!($($comment),*);
#[inline]
pub fn $method(self, $param: $Param) -> $Fixed<Frac> {
Self::from_bits(self.to_bits().$method($param))
}
}
};

View File

@ -47,7 +47,7 @@ assert_eq!(Fix::from_num(5).checked_neg(), None);",
";
#[inline]
pub fn checked_neg(self) -> Option<$Fixed<Frac>> {
<$Inner>::checked_neg(self.to_bits()).map(Self::from_bits)
self.to_bits().checked_neg().map(Self::from_bits)
}
);
@ -69,7 +69,7 @@ assert_eq!(Fix::max_value().checked_add(one), None);
";
#[inline]
pub fn checked_add(self, rhs: $Fixed<Frac>) -> Option<$Fixed<Frac>> {
<$Inner>::checked_add(self.to_bits(), rhs.to_bits()).map(Self::from_bits)
self.to_bits().checked_add(rhs.to_bits()).map(Self::from_bits)
}
);
@ -91,7 +91,7 @@ assert_eq!(Fix::min_value().checked_sub(one), None);
";
#[inline]
pub fn checked_sub(self, rhs: $Fixed<Frac>) -> Option<$Fixed<Frac>> {
<$Inner>::checked_sub(self.to_bits(), rhs.to_bits()).map(Self::from_bits)
self.to_bits().checked_sub(rhs.to_bits()).map(Self::from_bits)
}
);
@ -167,7 +167,7 @@ assert_eq!(Fix::max_value().checked_mul_int(2), None);
";
#[inline]
pub fn checked_mul_int(self, rhs: $Inner) -> Option<$Fixed<Frac>> {
<$Inner>::checked_mul(self.to_bits(), rhs).map(Self::from_bits)
self.to_bits().checked_mul(rhs).map(Self::from_bits)
}
);
@ -201,7 +201,7 @@ assert_eq!(Fix::from_num(1).checked_div_int(0), None);
";
#[inline]
pub fn checked_div_int(self, rhs: $Inner) -> Option<$Fixed<Frac>> {
<$Inner>::checked_div(self.to_bits(), rhs).map(Self::from_bits)
self.to_bits().checked_div(rhs).map(Self::from_bits)
}
);
@ -236,7 +236,7 @@ assert_eq!(Fix::from_num(1).checked_rem_int(0), None);
";
#[inline]
pub fn checked_rem_int(self, rhs: $Inner) -> Option<$Fixed<Frac>> {
<$Inner>::checked_rem(self.to_bits(), rhs).map(Self::from_bits)
self.to_bits().checked_rem(rhs).map(Self::from_bits)
}
);
@ -261,7 +261,7 @@ assert_eq!((Fix::from_num(1) / 2).checked_shl(",
";
#[inline]
pub fn checked_shl(self, rhs: u32) -> Option<$Fixed<Frac>> {
<$Inner>::checked_shl(self.to_bits(), rhs).map(Self::from_bits)
self.to_bits().checked_shl(rhs).map(Self::from_bits)
}
);
@ -286,7 +286,7 @@ assert_eq!(Fix::from_num(4).checked_shr(",
";
#[inline]
pub fn checked_shr(self, rhs: u32) -> Option<$Fixed<Frac>> {
<$Inner>::checked_shr(self.to_bits(), rhs).map(Self::from_bits)
self.to_bits().checked_shr(rhs).map(Self::from_bits)
}
);
@ -311,7 +311,7 @@ assert_eq!(Fix::min_value().checked_abs(), None);
";
#[inline]
pub fn checked_abs(self) -> Option<$Fixed<Frac>> {
<$Inner>::checked_abs(self.to_bits()).map(Self::from_bits)
self.to_bits().checked_abs().map(Self::from_bits)
}
);
}
@ -354,7 +354,7 @@ assert_eq!(Fix::from_num(5).saturating_neg(), Fix::from_num(0));",
}
);
comment!(
delegate!(
"Saturating addition. Returns the sum, saturating on overflow.
# Examples
@ -367,13 +367,10 @@ assert_eq!(Fix::from_num(3).saturating_add(Fix::from_num(2)), Fix::from_num(5));
assert_eq!(Fix::max_value().saturating_add(Fix::from_num(1)), Fix::max_value());
```
";
#[inline]
pub fn saturating_add(self, rhs: $Fixed<Frac>) -> $Fixed<Frac> {
Self::from_bits(<$Inner>::saturating_add(self.to_bits(), rhs.to_bits()))
}
$Fixed => fn saturating_add(self, rhs)
);
comment!(
delegate!(
"Saturating subtraction. Returns the difference, saturating on overflow.
# Examples
@ -393,10 +390,7 @@ assert_eq!(Fix::from_num(0).saturating_sub(Fix::from_num(1)), Fix::from_num(0));
"
```
";
#[inline]
pub fn saturating_sub(self, rhs: $Fixed<Frac>) -> $Fixed<Frac> {
Self::from_bits(<$Inner>::saturating_sub(self.to_bits(), rhs.to_bits()))
}
$Fixed => fn saturating_sub(self, rhs)
);
comment!(
@ -467,7 +461,7 @@ assert_eq!(Fix::max_value().saturating_mul_int(2), Fix::max_value());
";
#[inline]
pub fn saturating_mul_int(self, rhs: $Inner) -> $Fixed<Frac> {
Self::from_bits(<$Inner>::saturating_mul(self.to_bits(), rhs))
Self::from_bits(self.to_bits().saturating_mul(rhs))
}
);
@ -495,7 +489,7 @@ assert_eq!(Fix::min_value().saturating_abs(), Fix::max_value());
);
}
comment!(
delegate!(
"Wrapping negation. Returns the negated value, wrapping on overflow.
",
@ -525,13 +519,10 @@ assert_eq!(Fix::from_num(5).wrapping_neg(), Fix::from_bits(neg_five_bits));",
"
```
";
#[inline]
pub fn wrapping_neg(self) -> $Fixed<Frac> {
Self::from_bits(<$Inner>::wrapping_neg(self.to_bits()))
}
$Fixed => fn wrapping_neg(self)
);
comment!(
delegate!(
"Wrapping addition. Returns the sum, wrapping on overflow.
# Examples
@ -548,13 +539,10 @@ assert_eq!(Fix::max_value().wrapping_add(one), ",
"one_minus_bit);
```
";
#[inline]
pub fn wrapping_add(self, rhs: $Fixed<Frac>) -> $Fixed<Frac> {
Self::from_bits(<$Inner>::wrapping_add(self.to_bits(), rhs.to_bits()))
}
$Fixed => fn wrapping_add(self, rhs)
);
comment!(
delegate!(
"Wrapping subtraction. Returns the difference, wrapping on overflow.
# Examples
@ -576,10 +564,7 @@ assert_eq!(Fix::from_num(0)",
".wrapping_sub(one), Fix::max_value() - one_minus_bit);
```
";
#[inline]
pub fn wrapping_sub(self, rhs: $Fixed<Frac>) -> $Fixed<Frac> {
Self::from_bits(<$Inner>::wrapping_sub(self.to_bits(), rhs.to_bits()))
}
$Fixed => fn wrapping_sub(self, rhs)
);
comment!(
@ -646,7 +631,7 @@ assert_eq!(Fix::max_value().wrapping_mul_int(4), wrapped);
";
#[inline]
pub fn wrapping_mul_int(self, rhs: $Inner) -> $Fixed<Frac> {
Self::from_bits(<$Inner>::wrapping_mul(self.to_bits(), rhs))
Self::from_bits(self.to_bits().wrapping_mul(rhs))
}
);
@ -686,7 +671,7 @@ assert_eq!(Fix::from_num(3).wrapping_div_int(2), one_point_5);
";
#[inline]
pub fn wrapping_div_int(self, rhs: $Inner) -> $Fixed<Frac> {
Self::from_bits(<$Inner>::wrapping_div(self.to_bits(), rhs))
Self::from_bits(self.to_bits().wrapping_div(rhs))
}
);
@ -725,11 +710,11 @@ assert_eq!(Fix::from_bits(0b10101).wrapping_rem_int(8), Fix::from_bits(0b101));
";
#[inline]
pub fn wrapping_rem_int(self, rhs: $Inner) -> $Fixed<Frac> {
Self::from_bits(<$Inner>::wrapping_rem(self.to_bits(), rhs))
Self::from_bits(self.to_bits().wrapping_rem(rhs))
}
);
comment!(
delegate!(
"Wrapping shift left. Wraps `rhs` if `rhs` ≥ ",
$s_nbits,
", then shifts and returns the number.
@ -746,13 +731,10 @@ assert_eq!((Fix::from_num(1) / 2).wrapping_shl(3 + ",
"), Fix::from_num(4));
```
";
#[inline]
pub fn wrapping_shl(self, rhs: u32) -> $Fixed<Frac> {
Self::from_bits(<$Inner>::wrapping_shl(self.to_bits(), rhs))
}
$Fixed => fn wrapping_shl(self, rhs: u32)
);
comment!(
delegate!(
"Wrapping shift right. Wraps `rhs` if `rhs` ≥ ",
$s_nbits,
", then shifts and returns the number.
@ -769,15 +751,12 @@ assert_eq!((Fix::from_num(4)).wrapping_shr(3 + ",
"), Fix::from_num(1) / 2);
```
";
#[inline]
pub fn wrapping_shr(self, rhs: u32) -> $Fixed<Frac> {
Self::from_bits(<$Inner>::wrapping_shr(self.to_bits(), rhs))
}
$Fixed => fn wrapping_shr(self, rhs: u32)
);
if_signed! {
$Signedness;
comment!(
delegate!(
"Wrapping absolute value. Returns the absolute value, wrapping on overflow.
Overflow can only occur when trying to find the absolute value of the minimum value.
@ -792,10 +771,7 @@ assert_eq!(Fix::from_num(-5).wrapping_abs(), Fix::from_num(5));
assert_eq!(Fix::min_value().wrapping_abs(), Fix::min_value());
```
";
#[inline]
pub fn wrapping_abs(self) -> $Fixed<Frac> {
Self::from_bits(<$Inner>::wrapping_abs(self.to_bits()))
}
$Fixed => fn wrapping_abs(self)
);
}
@ -837,7 +813,7 @@ assert_eq!(Fix::from_num(5).overflowing_neg(), (Fix::from_bits(neg_five_bits), t
";
#[inline]
pub fn overflowing_neg(self) -> ($Fixed<Frac>, bool) {
let (ans, o) = <$Inner>::overflowing_neg(self.to_bits());
let (ans, o) = self.to_bits().overflowing_neg();
(Self::from_bits(ans), o)
}
);
@ -867,7 +843,7 @@ assert_eq!(Fix::max_value().overflowing_add(one), (",
";
#[inline]
pub fn overflowing_add(self, rhs: $Fixed<Frac>) -> ($Fixed<Frac>, bool) {
let (ans, o) = <$Inner>::overflowing_add(self.to_bits(), rhs.to_bits());
let (ans, o) = self.to_bits().overflowing_add(rhs.to_bits());
(Self::from_bits(ans), o)
}
);
@ -902,7 +878,7 @@ assert_eq!(Fix::from_num(0)",
";
#[inline]
pub fn overflowing_sub(self, rhs: $Fixed<Frac>) -> ($Fixed<Frac>, bool) {
let (ans, o) = <$Inner>::overflowing_sub(self.to_bits(), rhs.to_bits());
let (ans, o) = self.to_bits().overflowing_sub(rhs.to_bits());
(Self::from_bits(ans), o)
}
);
@ -986,7 +962,7 @@ assert_eq!(Fix::max_value().overflowing_mul_int(4), (wrapped, true));
";
#[inline]
pub fn overflowing_mul_int(self, rhs: $Inner) -> ($Fixed<Frac>, bool) {
let (ans, o) = <$Inner>::overflowing_mul(self.to_bits(), rhs);
let (ans, o) = self.to_bits().overflowing_mul(rhs);
(Self::from_bits(ans), o)
}
);
@ -1030,7 +1006,7 @@ assert_eq!(Fix::from_num(3).overflowing_div_int(2), (one_point_5, false));
";
#[inline]
pub fn overflowing_div_int(self, rhs: $Inner) -> ($Fixed<Frac>, bool) {
let (ans, o) = <$Inner>::overflowing_div(self.to_bits(), rhs);
let (ans, o) = self.to_bits().overflowing_div(rhs);
(Self::from_bits(ans), o)
}
);
@ -1073,7 +1049,7 @@ assert_eq!(Fix::from_bits(0b10101).overflowing_rem_int(8), (Fix::from_bits(0b101
";
#[inline]
pub fn overflowing_rem_int(self, rhs: $Inner) -> ($Fixed<Frac>, bool) {
let (ans, o) = <$Inner>::overflowing_rem(self.to_bits(), rhs);
let (ans, o) = self.to_bits().overflowing_rem(rhs);
(Self::from_bits(ans), o)
}
);
@ -1103,7 +1079,7 @@ assert_eq!((Fix::from_num(1) / 2).overflowing_shl(3 + ",
";
#[inline]
pub fn overflowing_shl(self, rhs: u32) -> ($Fixed<Frac>, bool) {
let (ans, o) = <$Inner>::overflowing_shl(self.to_bits(), rhs);
let (ans, o) = self.to_bits().overflowing_shl(rhs);
(Self::from_bits(ans), o)
}
);
@ -1133,7 +1109,7 @@ assert_eq!((Fix::from_num(4)).overflowing_shr(3 + ",
";
#[inline]
pub fn overflowing_shr(self, rhs: u32) -> ($Fixed<Frac>, bool) {
let (ans, o) = <$Inner>::overflowing_shr(self.to_bits(), rhs);
let (ans, o) = self.to_bits().overflowing_shr(rhs);
(Self::from_bits(ans), o)
}
);
@ -1164,7 +1140,7 @@ assert_eq!(Fix::min_value().overflowing_abs(), (Fix::min_value(), true));
";
#[inline]
pub fn overflowing_abs(self) -> ($Fixed<Frac>, bool) {
let (ans, o) = <$Inner>::overflowing_abs(self.to_bits());
let (ans, o) = self.to_bits().overflowing_abs();
(Self::from_bits(ans), o)
}
);

View File

@ -1237,7 +1237,7 @@ macro_rules! impl_float {
match kind {
FloatKind::Finite { .. } => {
let helper = FromFloatHelper { kind };
match <F as Sealed>::private_overflowing_from_float_helper(helper) {
match F::private_overflowing_from_float_helper(helper) {
(_, true) => None,
(wrapped, false) => Some(wrapped),
}
@ -1249,7 +1249,7 @@ macro_rules! impl_float {
fn saturating_to_fixed<F: Fixed>(self) -> F {
let kind = self.to_float_kind(F::frac_nbits(), F::int_nbits());
let helper = FromFloatHelper { kind };
<F as Sealed>::private_saturating_from_float_helper(helper)
F::private_saturating_from_float_helper(helper)
}
#[inline]
fn wrapping_to_fixed<F: Fixed>(self) -> F {
@ -1260,7 +1260,7 @@ macro_rules! impl_float {
fn overflowing_to_fixed<F: Fixed>(self) -> (F, bool) {
let kind = self.to_float_kind(F::frac_nbits(), F::int_nbits());
let helper = FromFloatHelper { kind };
<F as Sealed>::private_overflowing_from_float_helper(helper)
F::private_overflowing_from_float_helper(helper)
}
}
};

View File

@ -194,7 +194,7 @@ impl<F: Fixed> Wrapping<F> {
impl<F: Fixed> Display for Wrapping<F> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> FmtResult {
<F as Display>::fmt(&self.0, f)
Display::fmt(&self.0, f)
}
}