about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2018-09-26 13:55:54 -0400
committerNiko Matsakis <niko@alum.mit.edu>2018-10-15 11:42:07 -0400
commitda76b4d4e112bb9b81072a027d84ca57b5173129 (patch)
tree292e4b0c92f41eb086d996365b3a2547a591ffcf /src
parent460915be73a95c0cc3c1bcf74059bee9a7e0199a (diff)
downloadrust-da76b4d4e112bb9b81072a027d84ca57b5173129.tar.gz
rust-da76b4d4e112bb9b81072a027d84ca57b5173129.zip
convert `UniverseIndex` into a `newtype_index!`
Diffstat (limited to 'src')
-rw-r--r--src/librustc/ty/mod.rs45
1 files changed, 7 insertions, 38 deletions
diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs
index 3cccac1bedc..853b49098b9 100644
--- a/src/librustc/ty/mod.rs
+++ b/src/librustc/ty/mod.rs
@@ -57,6 +57,7 @@ use syntax::symbol::{keywords, Symbol, LocalInternedString, InternedString};
 use syntax_pos::{DUMMY_SP, Span};
 
 use smallvec;
+use rustc_data_structures::indexed_vec::Idx;
 use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
                                            HashStable};
 
@@ -1488,28 +1489,16 @@ impl<'tcx> InstantiatedPredicates<'tcx> {
 /// declared, but a type name in a non-zero universe is a placeholder
 /// type -- an idealized representative of "types in general" that we
 /// use for checking generic functions.
-#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
-pub struct UniverseIndex { private: u32 }
+newtype_index! {
+    pub struct UniverseIndex {
+        DEBUG_FORMAT = "U{}",
+    }
+}
 
 impl_stable_hash_for!(struct UniverseIndex { private });
 
 impl UniverseIndex {
-    /// The root universe, where things that the user defined are
-    /// visible.
-    pub const ROOT: Self = UniverseIndex { private: 0 };
-
-    /// The "max universe" -- this isn't really a valid universe, but
-    /// it's useful sometimes as a "starting value" when you are
-    /// taking the minimum of a (non-empty!) set of universes.
-    pub const MAX: Self = UniverseIndex { private: ::std::u32::MAX };
-
-    /// Creates a universe index from the given integer.  Not to be
-    /// used lightly lest you pick a bad value. But sometimes we
-    /// convert universe indices into integers and back for various
-    /// reasons.
-    pub fn from_u32(index: u32) -> Self {
-        UniverseIndex { private: index }
-    }
+    pub const ROOT: UniverseIndex = UniverseIndex::from_u32_const(0);
 
     /// A "superuniverse" corresponds to being inside a `forall` quantifier.
     /// So, for example, suppose we have this type in universe `U`:
@@ -1530,26 +1519,6 @@ impl UniverseIndex {
     pub fn is_subset_of(self, other: UniverseIndex) -> bool {
         self.private <= other.private
     }
-
-    pub fn as_u32(&self) -> u32 {
-        self.private
-    }
-
-    pub fn as_usize(&self) -> usize {
-        self.private as usize
-    }
-}
-
-impl fmt::Debug for UniverseIndex {
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(fmt, "U{}", self.as_u32())
-    }
-}
-
-impl From<u32> for UniverseIndex {
-    fn from(index: u32) -> Self {
-        UniverseIndex::from_u32(index)
-    }
 }
 
 /// The "placeholder index" fully defines a placeholder region.