Skip to content

Commit dce442f

Browse files
committed
core: Audit num module for int/uint
* count_ones/zeros, trailing_ones/zeros return u32, not usize * rotate_left/right take u32, not usize * RADIX, MANTISSA_DIGITS, DIGITS, BITS, BYTES are u32, not usize Doesn't touch pow because there's another PR for it. [breaking-change]
1 parent 522d09d commit dce442f

File tree

18 files changed

+216
-156
lines changed

18 files changed

+216
-156
lines changed

src/libcollections/bit.rs

+59-59
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,17 @@ fn blocks_for_bits(bits: usize) -> usize {
189189
//
190190
// Note that we can technically avoid this branch with the expression
191191
// `(nbits + u32::BITS - 1) / 32::BITS`, but if nbits is almost usize::MAX this will overflow.
192-
if bits % u32::BITS == 0 {
193-
bits / u32::BITS
192+
if bits % u32::BITS as usize == 0 {
193+
bits / u32::BITS as usize
194194
} else {
195-
bits / u32::BITS + 1
195+
bits / u32::BITS as usize + 1
196196
}
197197
}
198198

199199
/// Computes the bitmask for the final word of the vector
200200
fn mask_for_bits(bits: usize) -> u32 {
201201
// Note especially that a perfect multiple of u32::BITS should mask all 1s.
202-
!0u32 >> (u32::BITS - bits % u32::BITS) % u32::BITS
202+
!0u32 >> (u32::BITS as usize - bits % u32::BITS as usize) % u32::BITS as usize
203203
}
204204

205205
impl BitVec {
@@ -237,7 +237,7 @@ impl BitVec {
237237
/// An operation might screw up the unused bits in the last block of the
238238
/// `BitVec`. As per (3), it's assumed to be all 0s. This method fixes it up.
239239
fn fix_last_block(&mut self) {
240-
let extra_bits = self.len() % u32::BITS;
240+
let extra_bits = self.len() % u32::BITS as usize;
241241
if extra_bits > 0 {
242242
let mask = (1 << extra_bits) - 1;
243243
let storage_len = self.storage.len();
@@ -313,7 +313,7 @@ impl BitVec {
313313
/// false, false, true, false]));
314314
/// ```
315315
pub fn from_bytes(bytes: &[u8]) -> BitVec {
316-
let len = bytes.len().checked_mul(u8::BITS).expect("capacity overflow");
316+
let len = bytes.len().checked_mul(u8::BITS as usize).expect("capacity overflow");
317317
let mut bit_vec = BitVec::with_capacity(len);
318318
let complete_words = bytes.len() / 4;
319319
let extra_bytes = bytes.len() % 4;
@@ -380,8 +380,8 @@ impl BitVec {
380380
if i >= self.nbits {
381381
return None;
382382
}
383-
let w = i / u32::BITS;
384-
let b = i % u32::BITS;
383+
let w = i / u32::BITS as usize;
384+
let b = i % u32::BITS as usize;
385385
self.storage.get(w).map(|&block|
386386
(block & (1 << b)) != 0
387387
)
@@ -407,8 +407,8 @@ impl BitVec {
407407
reason = "panic semantics are likely to change in the future")]
408408
pub fn set(&mut self, i: usize, x: bool) {
409409
assert!(i < self.nbits);
410-
let w = i / u32::BITS;
411-
let b = i % u32::BITS;
410+
let w = i / u32::BITS as usize;
411+
let b = i % u32::BITS as usize;
412412
let flag = 1 << b;
413413
let val = if x { self.storage[w] | flag }
414414
else { self.storage[w] & !flag };
@@ -789,7 +789,7 @@ impl BitVec {
789789
#[inline]
790790
#[stable(feature = "rust1", since = "1.0.0")]
791791
pub fn capacity(&self) -> usize {
792-
self.storage.capacity().checked_mul(u32::BITS).unwrap_or(usize::MAX)
792+
self.storage.capacity().checked_mul(u32::BITS as usize).unwrap_or(usize::MAX)
793793
}
794794

795795
/// Grows the `BitVec` in-place, adding `n` copies of `value` to the `BitVec`.
@@ -819,7 +819,7 @@ impl BitVec {
819819

820820
// Correct the old tail word, setting or clearing formerly unused bits
821821
let old_last_word = blocks_for_bits(self.nbits) - 1;
822-
if self.nbits % u32::BITS > 0 {
822+
if self.nbits % u32::BITS as usize > 0 {
823823
let mask = mask_for_bits(self.nbits);
824824
if value {
825825
self.storage[old_last_word] |= !mask;
@@ -868,7 +868,7 @@ impl BitVec {
868868
// (3)
869869
self.set(i, false);
870870
self.nbits = i;
871-
if self.nbits % u32::BITS == 0 {
871+
if self.nbits % u32::BITS as usize == 0 {
872872
// (2)
873873
self.storage.pop();
874874
}
@@ -890,7 +890,7 @@ impl BitVec {
890890
/// ```
891891
#[stable(feature = "rust1", since = "1.0.0")]
892892
pub fn push(&mut self, elem: bool) {
893-
if self.nbits % u32::BITS == 0 {
893+
if self.nbits % u32::BITS as usize == 0 {
894894
self.storage.push(0);
895895
}
896896
let insert_pos = self.nbits;
@@ -1417,7 +1417,7 @@ impl BitSet {
14171417
// Truncate
14181418
let trunc_len = cmp::max(old_len - n, 1);
14191419
bit_vec.storage.truncate(trunc_len);
1420-
bit_vec.nbits = trunc_len * u32::BITS;
1420+
bit_vec.nbits = trunc_len * u32::BITS as usize;
14211421
}
14221422

14231423
/// Iterator over each u32 stored in the `BitSet`.
@@ -1674,7 +1674,7 @@ impl BitSet {
16741674
#[inline]
16751675
#[stable(feature = "rust1", since = "1.0.0")]
16761676
pub fn len(&self) -> usize {
1677-
self.bit_vec.blocks().fold(0, |acc, n| acc + n.count_ones())
1677+
self.bit_vec.blocks().fold(0, |acc, n| acc + n.count_ones() as usize)
16781678
}
16791679

16801680
/// Returns whether there are no bits set in this set
@@ -1851,13 +1851,13 @@ impl<'a> Iterator for TwoBitPositions<'a> {
18511851
fn next(&mut self) -> Option<usize> {
18521852
while self.next_idx < self.set.bit_vec.len() ||
18531853
self.next_idx < self.other.bit_vec.len() {
1854-
let bit_idx = self.next_idx % u32::BITS;
1854+
let bit_idx = self.next_idx % u32::BITS as usize;
18551855
if bit_idx == 0 {
18561856
let s_bit_vec = &self.set.bit_vec;
18571857
let o_bit_vec = &self.other.bit_vec;
18581858
// Merging the two words is a bit of an awkward dance since
18591859
// one BitVec might be longer than the other
1860-
let word_idx = self.next_idx / u32::BITS;
1860+
let word_idx = self.next_idx / u32::BITS as usize;
18611861
let w1 = if word_idx < s_bit_vec.storage.len() {
18621862
s_bit_vec.storage[word_idx]
18631863
} else { 0 };
@@ -2461,70 +2461,70 @@ mod tests {
24612461

24622462
#[test]
24632463
fn test_bit_vec_push_pop() {
2464-
let mut s = BitVec::from_elem(5 * u32::BITS - 2, false);
2465-
assert_eq!(s.len(), 5 * u32::BITS - 2);
2466-
assert_eq!(s[5 * u32::BITS - 3], false);
2464+
let mut s = BitVec::from_elem(5 * u32::BITS as usize - 2, false);
2465+
assert_eq!(s.len(), 5 * u32::BITS as usize - 2);
2466+
assert_eq!(s[5 * u32::BITS as usize - 3], false);
24672467
s.push(true);
24682468
s.push(true);
2469-
assert_eq!(s[5 * u32::BITS - 2], true);
2470-
assert_eq!(s[5 * u32::BITS - 1], true);
2469+
assert_eq!(s[5 * u32::BITS as usize - 2], true);
2470+
assert_eq!(s[5 * u32::BITS as usize - 1], true);
24712471
// Here the internal vector will need to be extended
24722472
s.push(false);
2473-
assert_eq!(s[5 * u32::BITS], false);
2473+
assert_eq!(s[5 * u32::BITS as usize], false);
24742474
s.push(false);
2475-
assert_eq!(s[5 * u32::BITS + 1], false);
2476-
assert_eq!(s.len(), 5 * u32::BITS + 2);
2475+
assert_eq!(s[5 * u32::BITS as usize + 1], false);
2476+
assert_eq!(s.len(), 5 * u32::BITS as usize + 2);
24772477
// Pop it all off
24782478
assert_eq!(s.pop(), Some(false));
24792479
assert_eq!(s.pop(), Some(false));
24802480
assert_eq!(s.pop(), Some(true));
24812481
assert_eq!(s.pop(), Some(true));
2482-
assert_eq!(s.len(), 5 * u32::BITS - 2);
2482+
assert_eq!(s.len(), 5 * u32::BITS as usize - 2);
24832483
}
24842484

24852485
#[test]
24862486
fn test_bit_vec_truncate() {
2487-
let mut s = BitVec::from_elem(5 * u32::BITS, true);
2487+
let mut s = BitVec::from_elem(5 * u32::BITS as usize, true);
24882488

2489-
assert_eq!(s, BitVec::from_elem(5 * u32::BITS, true));
2490-
assert_eq!(s.len(), 5 * u32::BITS);
2491-
s.truncate(4 * u32::BITS);
2492-
assert_eq!(s, BitVec::from_elem(4 * u32::BITS, true));
2493-
assert_eq!(s.len(), 4 * u32::BITS);
2489+
assert_eq!(s, BitVec::from_elem(5 * u32::BITS as usize, true));
2490+
assert_eq!(s.len(), 5 * u32::BITS as usize);
2491+
s.truncate(4 * u32::BITS as usize);
2492+
assert_eq!(s, BitVec::from_elem(4 * u32::BITS as usize, true));
2493+
assert_eq!(s.len(), 4 * u32::BITS as usize);
24942494
// Truncating to a size > s.len() should be a noop
2495-
s.truncate(5 * u32::BITS);
2496-
assert_eq!(s, BitVec::from_elem(4 * u32::BITS, true));
2497-
assert_eq!(s.len(), 4 * u32::BITS);
2498-
s.truncate(3 * u32::BITS - 10);
2499-
assert_eq!(s, BitVec::from_elem(3 * u32::BITS - 10, true));
2500-
assert_eq!(s.len(), 3 * u32::BITS - 10);
2495+
s.truncate(5 * u32::BITS as usize);
2496+
assert_eq!(s, BitVec::from_elem(4 * u32::BITS as usize, true));
2497+
assert_eq!(s.len(), 4 * u32::BITS as usize);
2498+
s.truncate(3 * u32::BITS as usize - 10);
2499+
assert_eq!(s, BitVec::from_elem(3 * u32::BITS as usize - 10, true));
2500+
assert_eq!(s.len(), 3 * u32::BITS as usize - 10);
25012501
s.truncate(0);
25022502
assert_eq!(s, BitVec::from_elem(0, true));
25032503
assert_eq!(s.len(), 0);
25042504
}
25052505

25062506
#[test]
25072507
fn test_bit_vec_reserve() {
2508-
let mut s = BitVec::from_elem(5 * u32::BITS, true);
2508+
let mut s = BitVec::from_elem(5 * u32::BITS as usize, true);
25092509
// Check capacity
2510-
assert!(s.capacity() >= 5 * u32::BITS);
2511-
s.reserve(2 * u32::BITS);
2512-
assert!(s.capacity() >= 7 * u32::BITS);
2513-
s.reserve(7 * u32::BITS);
2514-
assert!(s.capacity() >= 12 * u32::BITS);
2515-
s.reserve_exact(7 * u32::BITS);
2516-
assert!(s.capacity() >= 12 * u32::BITS);
2517-
s.reserve(7 * u32::BITS + 1);
2518-
assert!(s.capacity() >= 12 * u32::BITS + 1);
2510+
assert!(s.capacity() >= 5 * u32::BITS as usize);
2511+
s.reserve(2 * u32::BITS as usize);
2512+
assert!(s.capacity() >= 7 * u32::BITS as usize);
2513+
s.reserve(7 * u32::BITS as usize);
2514+
assert!(s.capacity() >= 12 * u32::BITS as usize);
2515+
s.reserve_exact(7 * u32::BITS as usize);
2516+
assert!(s.capacity() >= 12 * u32::BITS as usize);
2517+
s.reserve(7 * u32::BITS as usize + 1);
2518+
assert!(s.capacity() >= 12 * u32::BITS as usize + 1);
25192519
// Check that length hasn't changed
2520-
assert_eq!(s.len(), 5 * u32::BITS);
2520+
assert_eq!(s.len(), 5 * u32::BITS as usize);
25212521
s.push(true);
25222522
s.push(false);
25232523
s.push(true);
2524-
assert_eq!(s[5 * u32::BITS - 1], true);
2525-
assert_eq!(s[5 * u32::BITS - 0], true);
2526-
assert_eq!(s[5 * u32::BITS + 1], false);
2527-
assert_eq!(s[5 * u32::BITS + 2], true);
2524+
assert_eq!(s[5 * u32::BITS as usize - 1], true);
2525+
assert_eq!(s[5 * u32::BITS as usize - 0], true);
2526+
assert_eq!(s[5 * u32::BITS as usize + 1], false);
2527+
assert_eq!(s[5 * u32::BITS as usize + 2], true);
25282528
}
25292529

25302530
#[test]
@@ -2577,7 +2577,7 @@ mod bit_vec_bench {
25772577
let mut bit_vec = 0 as usize;
25782578
b.iter(|| {
25792579
for _ in 0..100 {
2580-
bit_vec |= 1 << ((r.next_u32() as usize) % u32::BITS);
2580+
bit_vec |= 1 << ((r.next_u32() as usize) % u32::BITS as usize);
25812581
}
25822582
black_box(&bit_vec);
25832583
});
@@ -2610,10 +2610,10 @@ mod bit_vec_bench {
26102610
#[bench]
26112611
fn bench_bit_set_small(b: &mut Bencher) {
26122612
let mut r = rng();
2613-
let mut bit_vec = BitVec::from_elem(u32::BITS, false);
2613+
let mut bit_vec = BitVec::from_elem(u32::BITS as usize, false);
26142614
b.iter(|| {
26152615
for _ in 0..100 {
2616-
bit_vec.set((r.next_u32() as usize) % u32::BITS, true);
2616+
bit_vec.set((r.next_u32() as usize) % u32::BITS as usize, true);
26172617
}
26182618
black_box(&bit_vec);
26192619
});
@@ -2630,7 +2630,7 @@ mod bit_vec_bench {
26302630

26312631
#[bench]
26322632
fn bench_bit_vec_small_iter(b: &mut Bencher) {
2633-
let bit_vec = BitVec::from_elem(u32::BITS, false);
2633+
let bit_vec = BitVec::from_elem(u32::BITS as usize, false);
26342634
b.iter(|| {
26352635
let mut sum = 0;
26362636
for _ in 0..10 {
@@ -3072,7 +3072,7 @@ mod bit_set_bench {
30723072
let mut bit_vec = BitSet::new();
30733073
b.iter(|| {
30743074
for _ in 0..100 {
3075-
bit_vec.insert((r.next_u32() as usize) % u32::BITS);
3075+
bit_vec.insert((r.next_u32() as usize) % u32::BITS as usize);
30763076
}
30773077
black_box(&bit_vec);
30783078
});

src/libcollections/enum_set.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub trait CLike {
7878
fn bit<E:CLike>(e: &E) -> usize {
7979
use core::usize;
8080
let value = e.to_usize();
81-
assert!(value < usize::BITS,
81+
assert!(value < usize::BITS as usize,
8282
"EnumSet only supports up to {} variants.", usize::BITS - 1);
8383
1 << value
8484
}
@@ -95,7 +95,7 @@ impl<E:CLike> EnumSet<E> {
9595
#[unstable(feature = "collections",
9696
reason = "matches collection reform specification, waiting for dust to settle")]
9797
pub fn len(&self) -> usize {
98-
self.bits.count_ones()
98+
self.bits.count_ones() as usize
9999
}
100100

101101
/// Returns true if the `EnumSet` is empty.
@@ -250,7 +250,7 @@ impl<E:CLike> Iterator for Iter<E> {
250250
}
251251

252252
fn size_hint(&self) -> (usize, Option<usize>) {
253-
let exact = self.bits.count_ones();
253+
let exact = self.bits.count_ones() as usize;
254254
(exact, Some(exact))
255255
}
256256
}

src/libcore/fmt/float.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
156156
deccum = deccum / radix_gen;
157157
deccum = deccum.trunc();
158158

159-
let c = char::from_digit(current_digit.to_int().unwrap() as u32, radix);
159+
let c = char::from_digit(current_digit.to_isize().unwrap() as u32, radix);
160160
buf[end] = c.unwrap() as u8;
161161
end += 1;
162162

@@ -211,7 +211,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
211211
// See note in first loop.
212212
let current_digit = deccum.trunc().abs();
213213

214-
let c = char::from_digit(current_digit.to_int().unwrap() as u32,
214+
let c = char::from_digit(current_digit.to_isize().unwrap() as u32,
215215
radix);
216216
buf[end] = c.unwrap() as u8;
217217
end += 1;

src/libcore/hash/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ mod impls {
240240
impl<S: Writer + Hasher> Hash<S> for $ty {
241241
#[inline]
242242
fn hash(&self, state: &mut S) {
243-
let a: [u8; ::$ty::BYTES] = unsafe {
243+
let a: [u8; ::$ty::BYTES as usize] = unsafe {
244244
mem::transmute(*self)
245245
};
246246
state.write(&a)
@@ -381,7 +381,7 @@ mod impls {
381381
}
382382

383383
fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
384-
let newlen = data.len() * ::$ty::BYTES;
384+
let newlen = data.len() * ::$ty::BYTES as usize;
385385
let ptr = data.as_ptr() as *const u8;
386386
state.write(unsafe { slice::from_raw_parts(ptr, newlen) })
387387
}

src/libcore/iter.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2467,15 +2467,15 @@ impl<A: Int + ToPrimitive> Iterator for Range<A> {
24672467
Some(a) => {
24682468
let sz = self.stop.to_i64().map(|b| b.checked_sub(a));
24692469
match sz {
2470-
Some(Some(bound)) => bound.to_uint(),
2470+
Some(Some(bound)) => bound.to_usize(),
24712471
_ => None,
24722472
}
24732473
},
24742474
None => match self.state.to_u64() {
24752475
Some(a) => {
24762476
let sz = self.stop.to_u64().map(|b| b.checked_sub(a));
24772477
match sz {
2478-
Some(Some(bound)) => bound.to_uint(),
2478+
Some(Some(bound)) => bound.to_usize(),
24792479
_ => None
24802480
}
24812481
},
@@ -2696,7 +2696,7 @@ impl<A: Int> Iterator for ::ops::Range<A> {
26962696
if self.start >= self.end {
26972697
(0, Some(0))
26982698
} else {
2699-
let length = (self.end - self.start).to_uint();
2699+
let length = (self.end - self.start).to_usize();
27002700
(length.unwrap_or(0), length)
27012701
}
27022702
}

0 commit comments

Comments
 (0)