Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions compiler/rustc_public_bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#![doc(rust_logo)]
#![feature(rustdoc_internals)]
#![feature(sized_hierarchy)]
#![feature(trait_alias)]
// tidy-alphabetical-end

use std::cell::RefCell;
Expand All @@ -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<Tables<'tcx, B>>,
Expand Down Expand Up @@ -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<Self>;
Expand Down Expand Up @@ -266,17 +270,15 @@ impl<K, V> Default for IndexMap<K, V> {
}
}

impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> IndexMap<K, V> {
impl<K: PartialEq + Hash + Eq, V: Cacheable> IndexMap<K, V> {
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));
*v
}
}

impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> Index<V>
for IndexMap<K, V>
{
impl<K: PartialEq + Hash + Eq, V: Cacheable> Index<V> for IndexMap<K, V> {
type Output = K;

fn index(&self, index: V) -> &Self::Output {
Expand Down
Loading