Split {Idx, IndexVec, IndexSlice}
into their own modules
This commit is contained in:
parent
64bcb32651
commit
e496fbec92
150 changed files with 472 additions and 454 deletions
45
compiler/rustc_index/src/idx.rs
Normal file
45
compiler/rustc_index/src/idx.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
use std::fmt::Debug;
|
||||
use std::hash::Hash;
|
||||
|
||||
/// Represents some newtyped `usize` wrapper.
|
||||
///
|
||||
/// Purpose: avoid mixing indexes for different bitvector domains.
|
||||
pub trait Idx: Copy + 'static + Eq + PartialEq + Debug + Hash {
|
||||
fn new(idx: usize) -> Self;
|
||||
|
||||
fn index(self) -> usize;
|
||||
|
||||
#[inline]
|
||||
fn increment_by(&mut self, amount: usize) {
|
||||
*self = self.plus(amount);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use = "Use `increment_by` if you wanted to update the index in-place"]
|
||||
fn plus(self, amount: usize) -> Self {
|
||||
Self::new(self.index() + amount)
|
||||
}
|
||||
}
|
||||
|
||||
impl Idx for usize {
|
||||
#[inline]
|
||||
fn new(idx: usize) -> Self {
|
||||
idx
|
||||
}
|
||||
#[inline]
|
||||
fn index(self) -> usize {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Idx for u32 {
|
||||
#[inline]
|
||||
fn new(idx: usize) -> Self {
|
||||
assert!(idx <= u32::MAX as usize);
|
||||
idx as u32
|
||||
}
|
||||
#[inline]
|
||||
fn index(self) -> usize {
|
||||
self as usize
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue