Skip to content

Commit eda3ee5

Browse files
scottmcmClar Roʒe
authored andcommitted
Add {is, next}_power_of_two to num::Wrapping
1 parent e9c3129 commit eda3ee5

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/libcore/num/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,6 +2927,30 @@ $EndFeature, "
29272927
self.one_less_than_next_power_of_two().checked_add(1)
29282928
}
29292929
}
2930+
2931+
doc_comment! {
2932+
concat!("Returns the smallest power of two greater than or equal to `n`. If
2933+
the next power of two is greater than the type's maximum value,
2934+
the return value is wrapped to `0`.
2935+
2936+
# Examples
2937+
2938+
Basic usage:
2939+
2940+
```
2941+
", $Feature,
2942+
"#![feature(wrapping_int_impl)]
2943+
2944+
assert_eq!(2", stringify!($SelfT), ".wrapping_next_power_of_two(), 2);
2945+
assert_eq!(3", stringify!($SelfT), ".wrapping_next_power_of_two(), 4);
2946+
assert_eq!(", stringify!($SelfT), "::max_value().wrapping_next_power_of_two(), 0);",
2947+
$EndFeature, "
2948+
```"),
2949+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
2950+
pub fn wrapping_next_power_of_two(self) -> Self {
2951+
self.one_less_than_next_power_of_two().wrapping_add(1)
2952+
}
2953+
}
29302954
}
29312955
}
29322956

src/libcore/num/wrapping.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,51 @@ macro_rules! wrapping_int_impl_signed {
672672

673673
wrapping_int_impl_signed! { isize i8 i16 i32 i64 i128 }
674674

675+
macro_rules! wrapping_int_impl_unsigned {
676+
($($t:ty)*) => ($(
677+
impl Wrapping<$t> {
678+
/// Returns `true` if and only if `self == 2^k` for some `k`.
679+
///
680+
/// # Examples
681+
///
682+
/// Basic usage:
683+
///
684+
/// ```
685+
/// assert!(Wrapping(16).is_power_of_two());
686+
/// assert!(!Wrapping(10).is_power_of_two());
687+
/// assert!(!Wrapping(0).is_power_of_two());
688+
/// ```
689+
#[inline]
690+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
691+
pub fn is_power_of_two(self) -> bool {
692+
self.0.is_power_of_two()
693+
}
694+
695+
/// Returns the smallest power of two greater than or equal to `n`. If
696+
/// the next power of two is greater than the type's maximum value,
697+
/// the return value is wrapped to `0`.
698+
///
699+
/// # Examples
700+
///
701+
/// Basic usage:
702+
///
703+
/// ```
704+
/// #![feature(wrapping_int_impl)]
705+
///
706+
/// assert_eq!(Wrapping(2).next_power_of_two(), Wrapping(2));
707+
/// assert_eq!(Wrapping(3).next_power_of_two(), Wrapping(4));
708+
/// assert_eq!(Wrapping(200_u8).next_power_of_two(), Wrapping(0));
709+
/// ```
710+
#[inline]
711+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
712+
pub fn next_power_of_two(self) -> Self {
713+
Wrapping(self.0.wrapping_next_power_of_two())
714+
}
715+
}
716+
)*)
717+
}
718+
719+
wrapping_int_impl_unsigned! { usize u8 u16 u32 u64 u128 }
675720

676721
mod shift_max {
677722
#![allow(non_upper_case_globals)]

0 commit comments

Comments
 (0)