mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-08 21:28:33 +00:00
46 lines
940 B
Rust
46 lines
940 B
Rust
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
|
|
}
|
|
}
|