From 49422dc9f10ab5ae11fe664f1f47c709c6b6debf Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Thu, 15 Apr 2021 17:42:32 +0200 Subject: [PATCH] constify unwrapped_div_int --- README.md | 2 ++ RELEASES.md | 2 ++ src/macros_no_frac.rs | 24 ++++++++++++++---------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d191a6e..2493201 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ The conversions supported cover the following cases. * [`rem_euclid`][f-re-1-8], [`checked_rem_euclid`][f-cre-1-8] * [`checked_div_int`][f-cdi-1-8], [`wrapping_div_int`][f-wdi-1-8], + [`unwrapped_div_int`][f-udi-1-8], [`overflowing_div_int`][f-odi-1-8] * The following methods were added to all fixed-point numbers: * [`const_not`][f-cn-1-8] @@ -133,6 +134,7 @@ The conversions supported cover the following cases. [f-re-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.rem_euclid [f-sdei-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.saturating_div_euclid_int [f-srei-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.saturating_rem_euclid_int +[f-udi-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_div_int [f-ur-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_rem [f-ure-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_rem_euclid [f-uri-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_rem_int diff --git a/RELEASES.md b/RELEASES.md index 8324b52..d85ca43 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -26,6 +26,7 @@ Version 1.8.0 (unreleased) * [`rem_euclid`][f-re-1-8], [`checked_rem_euclid`][f-cre-1-8] * [`checked_div_int`][f-cdi-1-8], [`wrapping_div_int`][f-wdi-1-8], + [`unwrapped_div_int`][f-udi-1-8], [`overflowing_div_int`][f-odi-1-8] * The following methods were added to all fixed-point numbers: * [`const_not`][f-cn-1-8] @@ -46,6 +47,7 @@ Version 1.8.0 (unreleased) [f-re-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.rem_euclid [f-sdei-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.saturating_div_euclid_int [f-srei-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.saturating_rem_euclid_int +[f-udi-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_div_int [f-ur-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_rem [f-ure-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_rem_euclid [f-uri-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_rem_int diff --git a/src/macros_no_frac.rs b/src/macros_no_frac.rs index a5884db..28c09f8 100644 --- a/src/macros_no_frac.rs +++ b/src/macros_no_frac.rs @@ -2264,29 +2264,33 @@ type Fix = ", $s_fixed, "; // 1.5 is binary 1.1 let one_point_5 = Fix::from_bits(0b11 << (4 - 1)); assert_eq!(Fix::from_num(3).unwrapped_div_int(2), one_point_5); +``` + +The following panics because the divisor is zero. + +```should_panic +use fixed::{types::extra::U4, ", $s_fixed, "}; +type Fix = ", $s_fixed, "; +let _divisor_is_zero = Fix::from_num(3).unwrapped_div_int(0); +``` ", if_signed_else_empty_str! { $Signedness, - "``` - + " The following panics because of overflow. ```should_panic use fixed::{types::extra::U4, ", $s_fixed, "}; type Fix = ", $s_fixed, "; let _overflow = Fix::MIN.unwrapped_div_int(-1); +``` ", - }, - "``` -"; + }; #[inline] #[track_caller] #[must_use = "this returns the result of the operation, without modifying the original"] - pub fn unwrapped_div_int(self, rhs: $Inner) -> $Fixed { - match self.overflowing_div_int(rhs) { - (_, true) => panic!("overflow"), - (ans, false) => ans, - } + pub const fn unwrapped_div_int(self, rhs: $Inner) -> $Fixed { + Self::from_bits(self.to_bits() / rhs) } }