ra_ap_rustc_index/
idx.rs

1use std::fmt::Debug;
2use std::hash::Hash;
3
4/// Represents some newtyped `usize` wrapper.
5///
6/// Purpose: avoid mixing indexes for different bitvector domains.
7pub trait Idx: Copy + 'static + Eq + PartialEq + Debug + Hash {
8    fn new(idx: usize) -> Self;
9
10    fn index(self) -> usize;
11
12    #[inline]
13    fn increment_by(&mut self, amount: usize) {
14        *self = self.plus(amount);
15    }
16
17    #[inline]
18    #[must_use = "Use `increment_by` if you wanted to update the index in-place"]
19    fn plus(self, amount: usize) -> Self {
20        Self::new(self.index() + amount)
21    }
22}
23
24impl Idx for usize {
25    #[inline]
26    fn new(idx: usize) -> Self {
27        idx
28    }
29    #[inline]
30    fn index(self) -> usize {
31        self
32    }
33}
34
35impl Idx for u32 {
36    #[inline]
37    fn new(idx: usize) -> Self {
38        assert!(idx <= u32::MAX as usize);
39        idx as u32
40    }
41    #[inline]
42    fn index(self) -> usize {
43        self as usize
44    }
45}