From 7ed24f0fa31a4ab0f210bbbcb8ab44a13deb26bb Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 13 Mar 2019 05:18:23 -0400 Subject: [PATCH] use `InternIndex` also to represent indices from the user --- src/interned.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/interned.rs b/src/interned.rs index 572181eb..7141006c 100644 --- a/src/interned.rs +++ b/src/interned.rs @@ -90,6 +90,9 @@ impl InternKey for usize { } /// Newtype indicating an index into the intern table. +/// +/// NB. In some cases, `InternIndex` values come directly from the +/// user and hence they are not 'trusted' to be valid or in-bounds. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] struct InternIndex { index: u32, @@ -103,8 +106,16 @@ impl InternIndex { impl From for InternIndex { fn from(v: usize) -> Self { - assert!(v < (std::u32::MAX as usize)); - InternIndex { index: v as u32 } + InternIndex { index: v.as_u32() } + } +} + +impl From<&T> for InternIndex +where + T: InternKey, +{ + fn from(v: &T) -> Self { + InternIndex { index: v.as_u32() } } } @@ -345,10 +356,10 @@ where fn lookup_value( &self, db: &DB, - index: u32, + index: InternIndex, op: impl FnOnce(&Q::Key) -> R, ) -> StampedValue { - let index = index as usize; + let index = index.index(); let revision_now = db.salsa_runtime().current_revision(); { @@ -514,7 +525,7 @@ where key: &Q::Key, database_key: &DB::DatabaseKey, ) -> Result { - let index: u32 = key.as_u32(); + let index = InternIndex::from(key); let group_storage = >::group_storage(db); let interned_storage = IQ::query_storage(group_storage); @@ -534,7 +545,7 @@ where key: &Q::Key, _database_key: &DB::DatabaseKey, ) -> bool { - let index: u32 = key.as_u32(); + let index = InternIndex::from(key); // FIXME -- This seems maybe not quite right, as it will panic // if `key` has been removed from the map since, but it should