From c3a92b5c72b9b5b66c9e48f186527b55508a6d2b Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Mon, 1 Feb 2021 11:34:09 +0100 Subject: [PATCH] constify some checked methods --- README.md | 13 ++++++++++++ RELEASES.md | 13 ++++++++++++ src/macros_no_frac.rs | 47 +++++++++++++++++++++++++++++++------------ 3 files changed, 60 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 397832e..f100d57 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,19 @@ The conversions supported cover the following cases. ### Version 1.6.0 news (unreleased) * 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) diff --git a/RELEASES.md b/RELEASES.md index 04ac16d..95b5f2d 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -9,6 +9,19 @@ Version 1.6.0 (unreleased) ========================== * 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) ========================== diff --git a/src/macros_no_frac.rs b/src/macros_no_frac.rs index 7a280b7..67a4192 100644 --- a/src/macros_no_frac.rs +++ b/src/macros_no_frac.rs @@ -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 "; #[inline] - pub fn checked_neg(self) -> Option<$Fixed> { - self.to_bits().checked_neg().map(Self::from_bits) + pub const fn checked_neg(self) -> Option<$Fixed> { + 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 "; #[inline] - pub fn checked_add(self, rhs: $Fixed) -> Option<$Fixed> { - self.to_bits().checked_add(rhs.to_bits()).map(Self::from_bits) + pub const fn checked_add(self, rhs: $Fixed) -> Option<$Fixed> { + 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 "; #[inline] - pub fn checked_sub(self, rhs: $Fixed) -> Option<$Fixed> { - self.to_bits().checked_sub(rhs.to_bits()).map(Self::from_bits) + pub const fn checked_sub(self, rhs: $Fixed) -> Option<$Fixed> { + 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 "; #[inline] - pub fn checked_mul_int(self, rhs: $Inner) -> Option<$Fixed> { - self.to_bits().checked_mul(rhs).map(Self::from_bits) + pub const fn checked_mul_int(self, rhs: $Inner) -> Option<$Fixed> { + 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 "; #[inline] - pub fn checked_shl(self, rhs: u32) -> Option<$Fixed> { - self.to_bits().checked_shl(rhs).map(Self::from_bits) + pub const fn checked_shl(self, rhs: u32) -> Option<$Fixed> { + 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 "; #[inline] - pub fn checked_shr(self, rhs: u32) -> Option<$Fixed> { - self.to_bits().checked_shr(rhs).map(Self::from_bits) + pub const fn checked_shr(self, rhs: u32) -> Option<$Fixed> { + 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] pub fn checked_abs(self) -> Option<$Fixed> { - self.to_bits().checked_abs().map(Self::from_bits) + match self.to_bits().checked_abs() { + None => None, + Some(bits) => Some(Self::from_bits(bits)), + } } } }