From 5b287522818245d118477b0dcfe1ec11da511a9c Mon Sep 17 00:00:00 2001 From: Makai Date: Wed, 10 Sep 2025 17:07:15 +0800 Subject: [PATCH] add `Cacheable` trait alias --- compiler/rustc_public_bridge/src/lib.rs | 26 +++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_public_bridge/src/lib.rs b/compiler/rustc_public_bridge/src/lib.rs index dec3be70baff4..1c38cb6c4bdfc 100644 --- a/compiler/rustc_public_bridge/src/lib.rs +++ b/compiler/rustc_public_bridge/src/lib.rs @@ -21,6 +21,7 @@ #![doc(rust_logo)] #![feature(rustdoc_internals)] #![feature(sized_hierarchy)] +#![feature(trait_alias)] // tidy-alphabetical-end use std::cell::RefCell; @@ -45,6 +46,9 @@ pub mod context; #[deprecated(note = "please use `rustc_public::rustc_internal` instead")] pub mod rustc_internal {} +/// Trait alias for types that can be cached in [`Tables`]. +pub trait Cacheable = Copy + Debug + PartialEq + IndexedVal; + /// A container which is used for TLS. pub struct Container<'tcx, B: Bridge> { pub tables: RefCell>, @@ -213,14 +217,14 @@ impl<'tcx, B: Bridge> Tables<'tcx, B> { /// A trait defining types that are used to emulate rustc_public components, which is really /// useful when programming in rustc_public-agnostic settings. pub trait Bridge: Sized { - type DefId: Copy + Debug + PartialEq + IndexedVal; - type AllocId: Copy + Debug + PartialEq + IndexedVal; - type Span: Copy + Debug + PartialEq + IndexedVal; - type Ty: Copy + Debug + PartialEq + IndexedVal; - type InstanceDef: Copy + Debug + PartialEq + IndexedVal; - type TyConstId: Copy + Debug + PartialEq + IndexedVal; - type MirConstId: Copy + Debug + PartialEq + IndexedVal; - type Layout: Copy + Debug + PartialEq + IndexedVal; + type DefId: Cacheable; + type AllocId: Cacheable; + type Span: Cacheable; + type Ty: Cacheable; + type InstanceDef: Cacheable; + type TyConstId: Cacheable; + type MirConstId: Cacheable; + type Layout: Cacheable; type Error: Error; type CrateItem: CrateItem; @@ -266,7 +270,7 @@ impl Default for IndexMap { } } -impl IndexMap { +impl IndexMap { pub fn create_or_fetch(&mut self, key: K) -> V { let len = self.index_map.len(); let v = self.index_map.entry(key).or_insert(V::to_val(len)); @@ -274,9 +278,7 @@ impl IndexMa } } -impl Index - for IndexMap -{ +impl Index for IndexMap { type Output = K; fn index(&self, index: V) -> &Self::Output {