File tree Expand file tree Collapse file tree 1 file changed +5
-4
lines changed Expand file tree Collapse file tree 1 file changed +5
-4
lines changed Original file line number Diff line number Diff line change @@ -119,26 +119,26 @@ pub fn format_visibility(vis: Visibility) -> &'static str {
119
119
// Based on the trick layed out at
120
120
// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
121
121
pub fn round_up_to_power_of_two ( mut x : usize ) -> usize {
122
- x -= 1 ;
122
+ x = x . wrapping_sub ( 1 ) ;
123
123
x |= x >> 1 ;
124
124
x |= x >> 2 ;
125
125
x |= x >> 4 ;
126
126
x |= x >> 8 ;
127
127
x |= x >> 16 ;
128
128
x |= x >> 32 ;
129
- x + 1
129
+ x. wrapping_add ( 1 )
130
130
}
131
131
132
132
#[ inline]
133
133
#[ cfg( target_pointer_width="32" ) ]
134
134
pub fn round_up_to_power_of_two ( mut x : usize ) -> usize {
135
- x -= 1 ;
135
+ x = x . wrapping_sub ( 1 ) ;
136
136
x |= x >> 1 ;
137
137
x |= x >> 2 ;
138
138
x |= x >> 4 ;
139
139
x |= x >> 8 ;
140
140
x |= x >> 16 ;
141
- x + 1
141
+ x. wrapping_add ( 1 )
142
142
}
143
143
144
144
// Macro for deriving implementations of Decodable for enums
@@ -161,6 +161,7 @@ macro_rules! impl_enum_decodable {
161
161
162
162
#[ test]
163
163
fn power_rounding ( ) {
164
+ assert_eq ! ( 0 , round_up_to_power_of_two( 0 ) ) ;
164
165
assert_eq ! ( 1 , round_up_to_power_of_two( 1 ) ) ;
165
166
assert_eq ! ( 64 , round_up_to_power_of_two( 33 ) ) ;
166
167
assert_eq ! ( 256 , round_up_to_power_of_two( 256 ) ) ;
You can’t perform that action at this time.
0 commit comments