constify some checked methods

This commit is contained in:
Trevor Spiteri 2021-02-01 11:34:09 +01:00
parent c4a63ae243
commit c3a92b5c72
3 changed files with 60 additions and 13 deletions

View File

@ -81,6 +81,19 @@ The conversions supported cover the following cases.
### Version 1.6.0 news (unreleased) ### Version 1.6.0 news (unreleased)
* The crate now requires rustc version 1.47.0 or later. * The crate now requires rustc version 1.47.0 or later.
* The following methods are now `const` functions:
* [`checked_neg`][f-cn-1-6], [`checked_add`][f-cad-1-6],
[`checked_sub`][f-cs-1-6], [`checked_mul_int`][f-cmi-1-6],
[`checked_shl`][f-cshl-1-6], [`checked_shr`][f-cshr-1-6],
[`checked_abs`][f-cab-1-6]
[f-cab-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_abs
[f-cad-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_add
[f-cmi-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_mul_int
[f-cn-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_neg
[f-cs-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_sub
[f-cshl-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_shl
[f-cshr-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_shr
### Version 1.5.0 news (2020-11-05) ### Version 1.5.0 news (2020-11-05)

View File

@ -9,6 +9,19 @@ Version 1.6.0 (unreleased)
========================== ==========================
* The crate now requires rustc version 1.47.0 or later. * The crate now requires rustc version 1.47.0 or later.
* The following methods are now `const` functions:
* [`checked_neg`][f-cn-1-6], [`checked_add`][f-cad-1-6],
[`checked_sub`][f-cs-1-6], [`checked_mul_int`][f-cmi-1-6],
[`checked_shl`][f-cshl-1-6], [`checked_shr`][f-cshr-1-6],
[`checked_abs`][f-cab-1-6]
[f-cab-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_abs
[f-cad-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_add
[f-cmi-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_mul_int
[f-cn-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_neg
[f-cs-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_sub
[f-cshl-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_shl
[f-cshr-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_shr
Version 1.5.0 (2020-11-05) Version 1.5.0 (2020-11-05)
========================== ==========================

View File

@ -674,8 +674,11 @@ assert_eq!(Fix::from_num(5).checked_neg(), None);",
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None [`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
"; ";
#[inline] #[inline]
pub fn checked_neg(self) -> Option<$Fixed<Frac>> { pub const fn checked_neg(self) -> Option<$Fixed<Frac>> {
self.to_bits().checked_neg().map(Self::from_bits) match self.to_bits().checked_neg() {
None => None,
Some(bits) => Some(Self::from_bits(bits)),
}
} }
} }
@ -695,8 +698,11 @@ assert_eq!(Fix::MAX.checked_add(one), None);
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None [`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
"; ";
#[inline] #[inline]
pub fn checked_add(self, rhs: $Fixed<Frac>) -> Option<$Fixed<Frac>> { pub const fn checked_add(self, rhs: $Fixed<Frac>) -> Option<$Fixed<Frac>> {
self.to_bits().checked_add(rhs.to_bits()).map(Self::from_bits) match self.to_bits().checked_add(rhs.to_bits()) {
None => None,
Some(bits) => Some(Self::from_bits(bits)),
}
} }
} }
@ -716,8 +722,11 @@ assert_eq!(Fix::MIN.checked_sub(one), None);
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None [`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
"; ";
#[inline] #[inline]
pub fn checked_sub(self, rhs: $Fixed<Frac>) -> Option<$Fixed<Frac>> { pub const fn checked_sub(self, rhs: $Fixed<Frac>) -> Option<$Fixed<Frac>> {
self.to_bits().checked_sub(rhs.to_bits()).map(Self::from_bits) match self.to_bits().checked_sub(rhs.to_bits()) {
None => None,
Some(bits) => Some(Self::from_bits(bits)),
}
} }
} }
@ -827,8 +836,11 @@ assert_eq!(Fix::MAX.checked_mul_int(2), None);
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None [`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
"; ";
#[inline] #[inline]
pub fn checked_mul_int(self, rhs: $Inner) -> Option<$Fixed<Frac>> { pub const fn checked_mul_int(self, rhs: $Inner) -> Option<$Fixed<Frac>> {
self.to_bits().checked_mul(rhs).map(Self::from_bits) match self.to_bits().checked_mul(rhs) {
None => None,
Some(bits) => Some(Self::from_bits(bits)),
}
} }
} }
@ -922,8 +934,11 @@ assert_eq!((Fix::from_num(1) / 2).checked_shl(", $s_nbits, "), None);
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None [`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
"; ";
#[inline] #[inline]
pub fn checked_shl(self, rhs: u32) -> Option<$Fixed<Frac>> { pub const fn checked_shl(self, rhs: u32) -> Option<$Fixed<Frac>> {
self.to_bits().checked_shl(rhs).map(Self::from_bits) match self.to_bits().checked_shl(rhs) {
None => None,
Some(bits) => Some(Self::from_bits(bits)),
}
} }
} }
@ -943,8 +958,11 @@ assert_eq!(Fix::from_num(4).checked_shr(", $s_nbits, "), None);
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None [`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
"; ";
#[inline] #[inline]
pub fn checked_shr(self, rhs: u32) -> Option<$Fixed<Frac>> { pub const fn checked_shr(self, rhs: u32) -> Option<$Fixed<Frac>> {
self.to_bits().checked_shr(rhs).map(Self::from_bits) match self.to_bits().checked_shr(rhs) {
None => None,
Some(bits) => Some(Self::from_bits(bits)),
}
} }
} }
@ -968,7 +986,10 @@ assert_eq!(Fix::MIN.checked_abs(), None);
"; ";
#[inline] #[inline]
pub fn checked_abs(self) -> Option<$Fixed<Frac>> { pub fn checked_abs(self) -> Option<$Fixed<Frac>> {
self.to_bits().checked_abs().map(Self::from_bits) match self.to_bits().checked_abs() {
None => None,
Some(bits) => Some(Self::from_bits(bits)),
}
} }
} }
} }