Skip to content

Commit fc98b19

Browse files
committed
Auto merge of #23832 - petrochenkov:usize, r=aturon
These constants are small and can fit even in `u8`, but semantically they have type `usize` because they denote sizes and are almost always used in `usize` context. The change of their type to `u32` during the integer audit led only to the large amount of `as usize` noise (see the second commit, which removes this noise). This is a minor [breaking-change] to an unstable interface. r? @aturon
2 parents 5e30f05 + 883adc6 commit fc98b19

File tree

14 files changed

+86
-86
lines changed

14 files changed

+86
-86
lines changed

src/libcollections/bit.rs

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

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

206206
impl BitVec {
@@ -238,7 +238,7 @@ impl BitVec {
238238
/// An operation might screw up the unused bits in the last block of the
239239
/// `BitVec`. As per (3), it's assumed to be all 0s. This method fixes it up.
240240
fn fix_last_block(&mut self) {
241-
let extra_bits = self.len() % u32::BITS as usize;
241+
let extra_bits = self.len() % u32::BITS;
242242
if extra_bits > 0 {
243243
let mask = (1 << extra_bits) - 1;
244244
let storage_len = self.storage.len();
@@ -317,7 +317,7 @@ impl BitVec {
317317
/// false, false, true, false]));
318318
/// ```
319319
pub fn from_bytes(bytes: &[u8]) -> BitVec {
320-
let len = bytes.len().checked_mul(u8::BITS as usize).expect("capacity overflow");
320+
let len = bytes.len().checked_mul(u8::BITS).expect("capacity overflow");
321321
let mut bit_vec = BitVec::with_capacity(len);
322322
let complete_words = bytes.len() / 4;
323323
let extra_bytes = bytes.len() % 4;
@@ -386,8 +386,8 @@ impl BitVec {
386386
if i >= self.nbits {
387387
return None;
388388
}
389-
let w = i / u32::BITS as usize;
390-
let b = i % u32::BITS as usize;
389+
let w = i / u32::BITS;
390+
let b = i % u32::BITS;
391391
self.storage.get(w).map(|&block|
392392
(block & (1 << b)) != 0
393393
)
@@ -414,8 +414,8 @@ impl BitVec {
414414
reason = "panic semantics are likely to change in the future")]
415415
pub fn set(&mut self, i: usize, x: bool) {
416416
assert!(i < self.nbits);
417-
let w = i / u32::BITS as usize;
418-
let b = i % u32::BITS as usize;
417+
let w = i / u32::BITS;
418+
let b = i % u32::BITS;
419419
let flag = 1 << b;
420420
let val = if x { self.storage[w] | flag }
421421
else { self.storage[w] & !flag };
@@ -811,7 +811,7 @@ impl BitVec {
811811
#[inline]
812812
#[stable(feature = "rust1", since = "1.0.0")]
813813
pub fn capacity(&self) -> usize {
814-
self.storage.capacity().checked_mul(u32::BITS as usize).unwrap_or(usize::MAX)
814+
self.storage.capacity().checked_mul(u32::BITS).unwrap_or(usize::MAX)
815815
}
816816

817817
/// Grows the `BitVec` in-place, adding `n` copies of `value` to the `BitVec`.
@@ -842,7 +842,7 @@ impl BitVec {
842842

843843
// Correct the old tail word, setting or clearing formerly unused bits
844844
let num_cur_blocks = blocks_for_bits(self.nbits);
845-
if self.nbits % u32::BITS as usize > 0 {
845+
if self.nbits % u32::BITS > 0 {
846846
let mask = mask_for_bits(self.nbits);
847847
if value {
848848
self.storage[num_cur_blocks - 1] |= !mask;
@@ -892,7 +892,7 @@ impl BitVec {
892892
// (3)
893893
self.set(i, false);
894894
self.nbits = i;
895-
if self.nbits % u32::BITS as usize == 0 {
895+
if self.nbits % u32::BITS == 0 {
896896
// (2)
897897
self.storage.pop();
898898
}
@@ -915,7 +915,7 @@ impl BitVec {
915915
/// ```
916916
#[stable(feature = "rust1", since = "1.0.0")]
917917
pub fn push(&mut self, elem: bool) {
918-
if self.nbits % u32::BITS as usize == 0 {
918+
if self.nbits % u32::BITS == 0 {
919919
self.storage.push(0);
920920
}
921921
let insert_pos = self.nbits;
@@ -1433,7 +1433,7 @@ impl BitSet {
14331433
// Truncate
14341434
let trunc_len = cmp::max(old_len - n, 1);
14351435
bit_vec.storage.truncate(trunc_len);
1436-
bit_vec.nbits = trunc_len * u32::BITS as usize;
1436+
bit_vec.nbits = trunc_len * u32::BITS;
14371437
}
14381438

14391439
/// Iterator over each u32 stored in the `BitSet`.
@@ -1871,13 +1871,13 @@ impl<'a> Iterator for TwoBitPositions<'a> {
18711871
fn next(&mut self) -> Option<usize> {
18721872
while self.next_idx < self.set.bit_vec.len() ||
18731873
self.next_idx < self.other.bit_vec.len() {
1874-
let bit_idx = self.next_idx % u32::BITS as usize;
1874+
let bit_idx = self.next_idx % u32::BITS;
18751875
if bit_idx == 0 {
18761876
let s_bit_vec = &self.set.bit_vec;
18771877
let o_bit_vec = &self.other.bit_vec;
18781878
// Merging the two words is a bit of an awkward dance since
18791879
// one BitVec might be longer than the other
1880-
let word_idx = self.next_idx / u32::BITS as usize;
1880+
let word_idx = self.next_idx / u32::BITS;
18811881
let w1 = if word_idx < s_bit_vec.storage.len() {
18821882
s_bit_vec.storage[word_idx]
18831883
} else { 0 };

src/libcollections/enum_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub trait CLike {
8686
fn bit<E:CLike>(e: &E) -> usize {
8787
use core::usize;
8888
let value = e.to_usize();
89-
assert!(value < usize::BITS as usize,
89+
assert!(value < usize::BITS,
9090
"EnumSet only supports up to {} variants.", usize::BITS - 1);
9191
1 << value
9292
}

src/libcollectionstest/bit/set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ mod bench {
407407
let mut bit_vec = BitSet::new();
408408
b.iter(|| {
409409
for _ in 0..100 {
410-
bit_vec.insert((r.next_u32() as usize) % u32::BITS as usize);
410+
bit_vec.insert((r.next_u32() as usize) % u32::BITS);
411411
}
412412
black_box(&bit_vec);
413413
});

src/libcollectionstest/bit/vec.rs

+40-40
Original file line numberDiff line numberDiff line change
@@ -541,70 +541,70 @@ fn test_big_bit_vec_tests() {
541541

542542
#[test]
543543
fn test_bit_vec_push_pop() {
544-
let mut s = BitVec::from_elem(5 * u32::BITS as usize - 2, false);
545-
assert_eq!(s.len(), 5 * u32::BITS as usize - 2);
546-
assert_eq!(s[5 * u32::BITS as usize - 3], false);
544+
let mut s = BitVec::from_elem(5 * u32::BITS - 2, false);
545+
assert_eq!(s.len(), 5 * u32::BITS - 2);
546+
assert_eq!(s[5 * u32::BITS - 3], false);
547547
s.push(true);
548548
s.push(true);
549-
assert_eq!(s[5 * u32::BITS as usize - 2], true);
550-
assert_eq!(s[5 * u32::BITS as usize - 1], true);
549+
assert_eq!(s[5 * u32::BITS - 2], true);
550+
assert_eq!(s[5 * u32::BITS - 1], true);
551551
// Here the internal vector will need to be extended
552552
s.push(false);
553-
assert_eq!(s[5 * u32::BITS as usize], false);
553+
assert_eq!(s[5 * u32::BITS], false);
554554
s.push(false);
555-
assert_eq!(s[5 * u32::BITS as usize + 1], false);
556-
assert_eq!(s.len(), 5 * u32::BITS as usize + 2);
555+
assert_eq!(s[5 * u32::BITS + 1], false);
556+
assert_eq!(s.len(), 5 * u32::BITS + 2);
557557
// Pop it all off
558558
assert_eq!(s.pop(), Some(false));
559559
assert_eq!(s.pop(), Some(false));
560560
assert_eq!(s.pop(), Some(true));
561561
assert_eq!(s.pop(), Some(true));
562-
assert_eq!(s.len(), 5 * u32::BITS as usize - 2);
562+
assert_eq!(s.len(), 5 * u32::BITS - 2);
563563
}
564564

565565
#[test]
566566
fn test_bit_vec_truncate() {
567-
let mut s = BitVec::from_elem(5 * u32::BITS as usize, true);
567+
let mut s = BitVec::from_elem(5 * u32::BITS, true);
568568

569-
assert_eq!(s, BitVec::from_elem(5 * u32::BITS as usize, true));
570-
assert_eq!(s.len(), 5 * u32::BITS as usize);
571-
s.truncate(4 * u32::BITS as usize);
572-
assert_eq!(s, BitVec::from_elem(4 * u32::BITS as usize, true));
573-
assert_eq!(s.len(), 4 * u32::BITS as usize);
569+
assert_eq!(s, BitVec::from_elem(5 * u32::BITS, true));
570+
assert_eq!(s.len(), 5 * u32::BITS);
571+
s.truncate(4 * u32::BITS);
572+
assert_eq!(s, BitVec::from_elem(4 * u32::BITS, true));
573+
assert_eq!(s.len(), 4 * u32::BITS);
574574
// Truncating to a size > s.len() should be a noop
575-
s.truncate(5 * u32::BITS as usize);
576-
assert_eq!(s, BitVec::from_elem(4 * u32::BITS as usize, true));
577-
assert_eq!(s.len(), 4 * u32::BITS as usize);
578-
s.truncate(3 * u32::BITS as usize - 10);
579-
assert_eq!(s, BitVec::from_elem(3 * u32::BITS as usize - 10, true));
580-
assert_eq!(s.len(), 3 * u32::BITS as usize - 10);
575+
s.truncate(5 * u32::BITS);
576+
assert_eq!(s, BitVec::from_elem(4 * u32::BITS, true));
577+
assert_eq!(s.len(), 4 * u32::BITS);
578+
s.truncate(3 * u32::BITS - 10);
579+
assert_eq!(s, BitVec::from_elem(3 * u32::BITS - 10, true));
580+
assert_eq!(s.len(), 3 * u32::BITS - 10);
581581
s.truncate(0);
582582
assert_eq!(s, BitVec::from_elem(0, true));
583583
assert_eq!(s.len(), 0);
584584
}
585585

586586
#[test]
587587
fn test_bit_vec_reserve() {
588-
let mut s = BitVec::from_elem(5 * u32::BITS as usize, true);
588+
let mut s = BitVec::from_elem(5 * u32::BITS, true);
589589
// Check capacity
590-
assert!(s.capacity() >= 5 * u32::BITS as usize);
591-
s.reserve(2 * u32::BITS as usize);
592-
assert!(s.capacity() >= 7 * u32::BITS as usize);
593-
s.reserve(7 * u32::BITS as usize);
594-
assert!(s.capacity() >= 12 * u32::BITS as usize);
595-
s.reserve_exact(7 * u32::BITS as usize);
596-
assert!(s.capacity() >= 12 * u32::BITS as usize);
597-
s.reserve(7 * u32::BITS as usize + 1);
598-
assert!(s.capacity() >= 12 * u32::BITS as usize + 1);
590+
assert!(s.capacity() >= 5 * u32::BITS);
591+
s.reserve(2 * u32::BITS);
592+
assert!(s.capacity() >= 7 * u32::BITS);
593+
s.reserve(7 * u32::BITS);
594+
assert!(s.capacity() >= 12 * u32::BITS);
595+
s.reserve_exact(7 * u32::BITS);
596+
assert!(s.capacity() >= 12 * u32::BITS);
597+
s.reserve(7 * u32::BITS + 1);
598+
assert!(s.capacity() >= 12 * u32::BITS + 1);
599599
// Check that length hasn't changed
600-
assert_eq!(s.len(), 5 * u32::BITS as usize);
600+
assert_eq!(s.len(), 5 * u32::BITS);
601601
s.push(true);
602602
s.push(false);
603603
s.push(true);
604-
assert_eq!(s[5 * u32::BITS as usize - 1], true);
605-
assert_eq!(s[5 * u32::BITS as usize - 0], true);
606-
assert_eq!(s[5 * u32::BITS as usize + 1], false);
607-
assert_eq!(s[5 * u32::BITS as usize + 2], true);
604+
assert_eq!(s[5 * u32::BITS - 1], true);
605+
assert_eq!(s[5 * u32::BITS - 0], true);
606+
assert_eq!(s[5 * u32::BITS + 1], false);
607+
assert_eq!(s[5 * u32::BITS + 2], true);
608608
}
609609

610610
#[test]
@@ -650,7 +650,7 @@ mod bench {
650650
let mut bit_vec = 0 as usize;
651651
b.iter(|| {
652652
for _ in 0..100 {
653-
bit_vec |= 1 << ((r.next_u32() as usize) % u32::BITS as usize);
653+
bit_vec |= 1 << ((r.next_u32() as usize) % u32::BITS);
654654
}
655655
black_box(&bit_vec);
656656
});
@@ -683,10 +683,10 @@ mod bench {
683683
#[bench]
684684
fn bench_bit_set_small(b: &mut Bencher) {
685685
let mut r = rng();
686-
let mut bit_vec = BitVec::from_elem(u32::BITS as usize, false);
686+
let mut bit_vec = BitVec::from_elem(u32::BITS, false);
687687
b.iter(|| {
688688
for _ in 0..100 {
689-
bit_vec.set((r.next_u32() as usize) % u32::BITS as usize, true);
689+
bit_vec.set((r.next_u32() as usize) % u32::BITS, true);
690690
}
691691
black_box(&bit_vec);
692692
});
@@ -703,7 +703,7 @@ mod bench {
703703

704704
#[bench]
705705
fn bench_bit_vec_small_iter(b: &mut Bencher) {
706-
let bit_vec = BitVec::from_elem(u32::BITS as usize, false);
706+
let bit_vec = BitVec::from_elem(u32::BITS, false);
707707
b.iter(|| {
708708
let mut sum = 0;
709709
for _ in 0..10 {

src/libcore/hash/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ mod impls {
194194
fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
195195
// FIXME(#23542) Replace with type ascription.
196196
#![allow(trivial_casts)]
197-
let newlen = data.len() * ::$ty::BYTES as usize;
197+
let newlen = data.len() * ::$ty::BYTES;
198198
let ptr = data.as_ptr() as *const u8;
199199
state.write(unsafe { slice::from_raw_parts(ptr, newlen) })
200200
}

src/libcore/num/int_macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ macro_rules! int_module { ($T:ty, $bits:expr) => (
1515
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
1616
// calling the `mem::size_of` function.
1717
#[unstable(feature = "core")]
18-
pub const BITS : u32 = $bits;
18+
pub const BITS : usize = $bits;
1919
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
2020
// calling the `mem::size_of` function.
2121
#[unstable(feature = "core")]
22-
pub const BYTES : u32 = ($bits / 8);
22+
pub const BYTES : usize = ($bits / 8);
2323

2424
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
2525
// calling the `Bounded::min_value` function.

src/libcore/num/uint_macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
macro_rules! uint_module { ($T:ty, $T_SIGNED:ty, $bits:expr) => (
1414

1515
#[unstable(feature = "core")]
16-
pub const BITS : u32 = $bits;
16+
pub const BITS : usize = $bits;
1717
#[unstable(feature = "core")]
18-
pub const BYTES : u32 = ($bits / 8);
18+
pub const BYTES : usize = ($bits / 8);
1919

2020
#[stable(feature = "rust1", since = "1.0.0")]
2121
pub const MIN: $T = 0 as $T;

src/libcoretest/num/int_macros.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ mod tests {
8686

8787
#[test]
8888
fn test_count_zeros() {
89-
assert!(A.count_zeros() == BITS - 3);
90-
assert!(B.count_zeros() == BITS - 2);
91-
assert!(C.count_zeros() == BITS - 5);
89+
assert!(A.count_zeros() == BITS as u32 - 3);
90+
assert!(B.count_zeros() == BITS as u32 - 2);
91+
assert!(C.count_zeros() == BITS as u32 - 5);
9292
}
9393

9494
#[test]

src/libcoretest/num/uint_macros.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ mod tests {
5454

5555
#[test]
5656
fn test_count_zeros() {
57-
assert!(A.count_zeros() == BITS - 3);
58-
assert!(B.count_zeros() == BITS - 2);
59-
assert!(C.count_zeros() == BITS - 5);
57+
assert!(A.count_zeros() == BITS as u32 - 3);
58+
assert!(B.count_zeros() == BITS as u32 - 2);
59+
assert!(C.count_zeros() == BITS as u32 - 5);
6060
}
6161

6262
#[test]

src/librustc/middle/dataflow.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'a, 'tcx, O:DataFlowOperator> DataFlowContext<'a, 'tcx, O> {
195195
oper: O,
196196
id_range: IdRange,
197197
bits_per_id: usize) -> DataFlowContext<'a, 'tcx, O> {
198-
let words_per_id = (bits_per_id + usize::BITS as usize - 1) / usize::BITS as usize;
198+
let words_per_id = (bits_per_id + usize::BITS - 1) / usize::BITS;
199199
let num_nodes = cfg.graph.all_nodes().len();
200200

201201
debug!("DataFlowContext::new(analysis_name: {}, id_range={:?}, \
@@ -367,7 +367,7 @@ impl<'a, 'tcx, O:DataFlowOperator> DataFlowContext<'a, 'tcx, O> {
367367
368368
for (word_index, &word) in words.iter().enumerate() {
369369
if word != 0 {
370-
let base_index = word_index * usize::BITS as usize;
370+
let base_index = word_index * usize::BITS;
371371
for offset in 0..usize::BITS {
372372
let bit = 1 << offset;
373373
if (word & bit) != 0 {
@@ -601,8 +601,8 @@ fn bitwise<Op:BitwiseOperator>(out_vec: &mut [usize],
601601
fn set_bit(words: &mut [usize], bit: usize) -> bool {
602602
debug!("set_bit: words={} bit={}",
603603
mut_bits_to_string(words), bit_str(bit));
604-
let word = bit / usize::BITS as usize;
605-
let bit_in_word = bit % usize::BITS as usize;
604+
let word = bit / usize::BITS;
605+
let bit_in_word = bit % usize::BITS;
606606
let bit_mask = 1 << bit_in_word;
607607
debug!("word={} bit_in_word={} bit_mask={}", word, bit_in_word, word);
608608
let oldv = words[word];

0 commit comments

Comments
 (0)