From e9c3129ea3d908932d8e9468eb477fcadc060da6 Mon Sep 17 00:00:00 2001 From: Clar Charr Date: Mon, 26 Mar 2018 16:46:04 -0400 Subject: [PATCH 1/4] Add signed-specific methods to Wrapping. --- src/libcore/num/wrapping.rs | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/libcore/num/wrapping.rs b/src/libcore/num/wrapping.rs index 826883fdc3f01..e6fd25a3be966 100644 --- a/src/libcore/num/wrapping.rs +++ b/src/libcore/num/wrapping.rs @@ -630,6 +630,48 @@ macro_rules! wrapping_int_impl { wrapping_int_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 } +macro_rules! wrapping_int_impl_signed { + ($($t:ty)*) => ($( + impl Wrapping<$t> { + /// Computes the absolute value of `self`. + #[inline] + #[unstable(feature = "wrapping_int_impl", issue = "32463")] + pub fn abs(self) -> Wrapping<$t> { + Wrapping(self.0.wrapping_abs()) + } + + /// Returns a number representing sign of `self`. + /// + /// - `0` if the number is zero + /// - `1` if the number is positive + /// - `-1` if the number is negative + #[inline] + #[unstable(feature = "wrapping_int_impl", issue = "32463")] + pub fn signum(self) -> Wrapping<$t> { + Wrapping(self.0.signum()) + } + + /// Returns `true` if `self` is positive and `false` if the number is zero or + /// negative. + #[inline] + #[unstable(feature = "wrapping_int_impl", issue = "32463")] + pub fn is_positive(self) -> bool { + self.0.is_positive() + } + + /// Returns `true` if `self` is negative and `false` if the number is zero or + /// positive. + #[inline] + #[unstable(feature = "wrapping_int_impl", issue = "32463")] + pub fn is_negative(self) -> bool { + self.0.is_negative() + } + } + )*) +} + +wrapping_int_impl_signed! { isize i8 i16 i32 i64 i128 } + mod shift_max { #![allow(non_upper_case_globals)] From eda3ee54c5bc44abf16490e57c52e34c6be0ff69 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sun, 1 Apr 2018 21:01:45 -0700 Subject: [PATCH 2/4] Add {is, next}_power_of_two to num::Wrapping --- src/libcore/num/mod.rs | 24 ++++++++++++++++++++ src/libcore/num/wrapping.rs | 45 +++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 18e0aa453d8df..81faae4226709 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -2927,6 +2927,30 @@ $EndFeature, " self.one_less_than_next_power_of_two().checked_add(1) } } + + doc_comment! { + concat!("Returns the smallest power of two greater than or equal to `n`. If +the next power of two is greater than the type's maximum value, +the return value is wrapped to `0`. + +# Examples + +Basic usage: + +``` +", $Feature, +"#![feature(wrapping_int_impl)] + +assert_eq!(2", stringify!($SelfT), ".wrapping_next_power_of_two(), 2); +assert_eq!(3", stringify!($SelfT), ".wrapping_next_power_of_two(), 4); +assert_eq!(", stringify!($SelfT), "::max_value().wrapping_next_power_of_two(), 0);", +$EndFeature, " +```"), + #[unstable(feature = "wrapping_int_impl", issue = "32463")] + pub fn wrapping_next_power_of_two(self) -> Self { + self.one_less_than_next_power_of_two().wrapping_add(1) + } + } } } diff --git a/src/libcore/num/wrapping.rs b/src/libcore/num/wrapping.rs index e6fd25a3be966..80dd370e4050e 100644 --- a/src/libcore/num/wrapping.rs +++ b/src/libcore/num/wrapping.rs @@ -672,6 +672,51 @@ macro_rules! wrapping_int_impl_signed { wrapping_int_impl_signed! { isize i8 i16 i32 i64 i128 } +macro_rules! wrapping_int_impl_unsigned { + ($($t:ty)*) => ($( + impl Wrapping<$t> { + /// Returns `true` if and only if `self == 2^k` for some `k`. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// assert!(Wrapping(16).is_power_of_two()); + /// assert!(!Wrapping(10).is_power_of_two()); + /// assert!(!Wrapping(0).is_power_of_two()); + /// ``` + #[inline] + #[unstable(feature = "wrapping_int_impl", issue = "32463")] + pub fn is_power_of_two(self) -> bool { + self.0.is_power_of_two() + } + + /// Returns the smallest power of two greater than or equal to `n`. If + /// the next power of two is greater than the type's maximum value, + /// the return value is wrapped to `0`. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(wrapping_int_impl)] + /// + /// assert_eq!(Wrapping(2).next_power_of_two(), Wrapping(2)); + /// assert_eq!(Wrapping(3).next_power_of_two(), Wrapping(4)); + /// assert_eq!(Wrapping(200_u8).next_power_of_two(), Wrapping(0)); + /// ``` + #[inline] + #[unstable(feature = "wrapping_int_impl", issue = "32463")] + pub fn next_power_of_two(self) -> Self { + Wrapping(self.0.wrapping_next_power_of_two()) + } + } + )*) +} + +wrapping_int_impl_unsigned! { usize u8 u16 u32 u64 u128 } mod shift_max { #![allow(non_upper_case_globals)] From a366076941e0ed16789bc8599056ded877aad755 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Mon, 2 Apr 2018 09:29:03 -0700 Subject: [PATCH 3/4] Fix doctests --- src/libcore/num/wrapping.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libcore/num/wrapping.rs b/src/libcore/num/wrapping.rs index 80dd370e4050e..eaa9129f881aa 100644 --- a/src/libcore/num/wrapping.rs +++ b/src/libcore/num/wrapping.rs @@ -682,6 +682,10 @@ macro_rules! wrapping_int_impl_unsigned { /// Basic usage: /// /// ``` + /// #![feature(wrapping_int_impl)] + /// + /// use std::num::wrapping; + /// /// assert!(Wrapping(16).is_power_of_two()); /// assert!(!Wrapping(10).is_power_of_two()); /// assert!(!Wrapping(0).is_power_of_two()); @@ -703,6 +707,8 @@ macro_rules! wrapping_int_impl_unsigned { /// ``` /// #![feature(wrapping_int_impl)] /// + /// use std::num::wrapping; + /// /// assert_eq!(Wrapping(2).next_power_of_two(), Wrapping(2)); /// assert_eq!(Wrapping(3).next_power_of_two(), Wrapping(4)); /// assert_eq!(Wrapping(200_u8).next_power_of_two(), Wrapping(0)); From 2e0561b3a65335bc7eaa2c0fb6eaafbf57332029 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Tue, 3 Apr 2018 22:39:31 -0700 Subject: [PATCH 4/4] Actually fix the doctests --- src/libcore/num/mod.rs | 5 ++--- src/libcore/num/wrapping.rs | 14 +++++++------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 81faae4226709..b85db1b897b99 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -2938,9 +2938,8 @@ the return value is wrapped to `0`. Basic usage: ``` -", $Feature, -"#![feature(wrapping_int_impl)] - +#![feature(wrapping_int_impl)] +", $Feature, " assert_eq!(2", stringify!($SelfT), ".wrapping_next_power_of_two(), 2); assert_eq!(3", stringify!($SelfT), ".wrapping_next_power_of_two(), 4); assert_eq!(", stringify!($SelfT), "::max_value().wrapping_next_power_of_two(), 0);", diff --git a/src/libcore/num/wrapping.rs b/src/libcore/num/wrapping.rs index eaa9129f881aa..4569c20c6a57f 100644 --- a/src/libcore/num/wrapping.rs +++ b/src/libcore/num/wrapping.rs @@ -684,11 +684,11 @@ macro_rules! wrapping_int_impl_unsigned { /// ``` /// #![feature(wrapping_int_impl)] /// - /// use std::num::wrapping; + /// use std::num::Wrapping; /// - /// assert!(Wrapping(16).is_power_of_two()); - /// assert!(!Wrapping(10).is_power_of_two()); - /// assert!(!Wrapping(0).is_power_of_two()); + /// assert!(Wrapping(16_u32).is_power_of_two()); + /// assert!(!Wrapping(10_u64).is_power_of_two()); + /// assert!(!Wrapping(0_usize).is_power_of_two()); /// ``` #[inline] #[unstable(feature = "wrapping_int_impl", issue = "32463")] @@ -707,10 +707,10 @@ macro_rules! wrapping_int_impl_unsigned { /// ``` /// #![feature(wrapping_int_impl)] /// - /// use std::num::wrapping; + /// use std::num::Wrapping; /// - /// assert_eq!(Wrapping(2).next_power_of_two(), Wrapping(2)); - /// assert_eq!(Wrapping(3).next_power_of_two(), Wrapping(4)); + /// assert_eq!(Wrapping(2_u32).next_power_of_two(), Wrapping(2)); + /// assert_eq!(Wrapping(3_u64).next_power_of_two(), Wrapping(4)); /// assert_eq!(Wrapping(200_u8).next_power_of_two(), Wrapping(0)); /// ``` #[inline]