about summary refs log tree commit diff
path: root/src/librustc_data_structures
diff options
context:
space:
mode:
authorSantiago Pastorino <spastorino@gmail.com>2017-09-27 19:47:41 -0300
committerSantiago Pastorino <spastorino@gmail.com>2017-10-04 23:50:53 -0300
commit3d230af80b01ebe62d27de8436832d0e7ab9ed94 (patch)
tree9ccd8e970d32be3baa66a3cd8c687e7dedb3198a /src/librustc_data_structures
parentf5cef21569115d41171114ec07bb144a3875afc3 (diff)
downloadrust-3d230af80b01ebe62d27de8436832d0e7ab9ed94.tar.gz
rust-3d230af80b01ebe62d27de8436832d0e7ab9ed94.zip
Move newtype_index to rustc_data_structures
Diffstat (limited to 'src/librustc_data_structures')
-rw-r--r--src/librustc_data_structures/indexed_vec.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs
index 1d0e88ee328..7674018075c 100644
--- a/src/librustc_data_structures/indexed_vec.rs
+++ b/src/librustc_data_structures/indexed_vec.rs
@@ -38,6 +38,31 @@ impl Idx for u32 {
     fn index(self) -> usize { self as usize }
 }
 
+#[macro_export]
+macro_rules! newtype_index {
+    ($name:ident, $debug_name:expr) => (
+        #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord,
+         RustcEncodable, RustcDecodable)]
+        pub struct $name(u32);
+
+        impl Idx for $name {
+            fn new(value: usize) -> Self {
+                assert!(value < (u32::MAX) as usize);
+                $name(value as u32)
+            }
+            fn index(self) -> usize {
+                self.0 as usize
+            }
+        }
+
+        impl Debug for $name {
+            fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
+                write!(fmt, "{}{}", $debug_name, self.0)
+            }
+        }
+    )
+}
+
 #[derive(Clone, PartialEq, Eq)]
 pub struct IndexVec<I: Idx, T> {
     pub raw: Vec<T>,