Skip to content

Commit f18bafd

Browse files
committed
Refactor bitslice: distinguish usize for indexing vs word type being indexed.
1 parent 59008cb commit f18bafd

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/librustc_borrowck/bitslice.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@
1010

1111
use std::mem;
1212

13-
/// `BitSlice` provides helper methods for treating a `[usize]`
13+
pub type Word = usize;
14+
15+
/// `BitSlice` provides helper methods for treating a `[Word]`
1416
/// as a bitvector.
1517
pub trait BitSlice {
1618
fn clear_bit(&mut self, idx: usize) -> bool;
1719
fn set_bit(&mut self, idx: usize) -> bool;
1820
fn get_bit(&self, idx: usize) -> bool;
1921
}
2022

21-
impl BitSlice for [usize] {
23+
impl BitSlice for [Word] {
2224
/// Clears bit at `idx` to 0; returns true iff this changed `self.`
2325
fn clear_bit(&mut self, idx: usize) -> bool {
2426
let words = self;
2527
debug!("clear_bit: words={} idx={}",
26-
bits_to_string(words, words.len() * mem::size_of::<usize>()), bit_str(idx));
28+
bits_to_string(words, words.len() * mem::size_of::<Word>()), bit_str(idx));
2729
let BitLookup { word, bit_in_word, bit_mask } = bit_lookup(idx);
2830
debug!("word={} bit_in_word={} bit_mask={}", word, bit_in_word, bit_mask);
2931
let oldv = words[word];
@@ -36,7 +38,7 @@ impl BitSlice for [usize] {
3638
fn set_bit(&mut self, idx: usize) -> bool {
3739
let words = self;
3840
debug!("set_bit: words={} idx={}",
39-
bits_to_string(words, words.len() * mem::size_of::<usize>()), bit_str(idx));
41+
bits_to_string(words, words.len() * mem::size_of::<Word>()), bit_str(idx));
4042
let BitLookup { word, bit_in_word, bit_mask } = bit_lookup(idx);
4143
debug!("word={} bit_in_word={} bit_mask={}", word, bit_in_word, bit_mask);
4244
let oldv = words[word];
@@ -54,31 +56,31 @@ impl BitSlice for [usize] {
5456
}
5557

5658
struct BitLookup {
57-
/// An index of the word holding the bit in original `[usize]` of query.
59+
/// An index of the word holding the bit in original `[Word]` of query.
5860
word: usize,
5961
/// Index of the particular bit within the word holding the bit.
6062
bit_in_word: usize,
6163
/// Word with single 1-bit set corresponding to where the bit is located.
62-
bit_mask: usize,
64+
bit_mask: Word,
6365
}
6466

6567
#[inline]
6668
fn bit_lookup(bit: usize) -> BitLookup {
67-
let usize_bits = mem::size_of::<usize>() * 8;
68-
let word = bit / usize_bits;
69-
let bit_in_word = bit % usize_bits;
69+
let word_bits = mem::size_of::<Word>() * 8;
70+
let word = bit / word_bits;
71+
let bit_in_word = bit % word_bits;
7072
let bit_mask = 1 << bit_in_word;
7173
BitLookup { word: word, bit_in_word: bit_in_word, bit_mask: bit_mask }
7274
}
7375

7476

75-
fn bit_str(bit: usize) -> String {
77+
fn bit_str(bit: Word) -> String {
7678
let byte = bit >> 3;
7779
let lobits = 1 << (bit & 0b111);
7880
format!("[{}:{}-{:02x}]", bit, byte, lobits)
7981
}
8082

81-
pub fn bits_to_string(words: &[usize], bits: usize) -> String {
83+
pub fn bits_to_string(words: &[Word], bits: usize) -> String {
8284
let mut result = String::new();
8385
let mut sep = '[';
8486

0 commit comments

Comments
 (0)