From 07f19480438e290cab41c3b61478ba993c284824 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 31 Oct 2022 16:06:56 +0000 Subject: [PATCH] Implement Idx for OwnerId. --- compiler/rustc_hir/src/hir_id.rs | 14 +++++++++++++- compiler/rustc_index/src/vec.rs | 2 ++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_hir/src/hir_id.rs b/compiler/rustc_hir/src/hir_id.rs index 752f760ea97..33f02a115ef 100644 --- a/compiler/rustc_hir/src/hir_id.rs +++ b/compiler/rustc_hir/src/hir_id.rs @@ -1,4 +1,4 @@ -use crate::def_id::{DefId, LocalDefId, CRATE_DEF_ID}; +use crate::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_ID}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; use rustc_span::{def_id::DefPathHash, HashStableContext}; use std::fmt; @@ -22,6 +22,18 @@ pub fn to_def_id(self) -> DefId { } } +impl rustc_index::vec::Idx for OwnerId { + #[inline] + fn new(idx: usize) -> Self { + OwnerId { def_id: LocalDefId { local_def_index: DefIndex::from_usize(idx) } } + } + + #[inline] + fn index(self) -> usize { + self.def_id.local_def_index.as_usize() + } +} + impl HashStable for OwnerId { #[inline] fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { diff --git a/compiler/rustc_index/src/vec.rs b/compiler/rustc_index/src/vec.rs index 4172ce3bb30..1519258c794 100644 --- a/compiler/rustc_index/src/vec.rs +++ b/compiler/rustc_index/src/vec.rs @@ -17,10 +17,12 @@ pub trait Idx: Copy + 'static + Eq + PartialEq + Debug + Hash { fn index(self) -> usize; + #[inline] fn increment_by(&mut self, amount: usize) { *self = self.plus(amount); } + #[inline] fn plus(self, amount: usize) -> Self { Self::new(self.index() + amount) }