summary refs log tree commit diff
path: root/src/librustc_data_structures
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2018-08-23 07:46:53 -0400
committerNiko Matsakis <niko@alum.mit.edu>2018-09-07 11:37:47 -0400
commitf702bd6a529ce9d2e311ea0254a04228a4a2f129 (patch)
tree0bb022359f5fd9b0ec5d81fe0fa4f5a5a97b011a /src/librustc_data_structures
parentc67d518b0d92785323f2c9c94dda25273821b47b (diff)
downloadrust-f702bd6a529ce9d2e311ea0254a04228a4a2f129.tar.gz
rust-f702bd6a529ce9d2e311ea0254a04228a4a2f129.zip
rewrite constants to use NewType::MAX instead of u32::MAX
Also, adjust the MAX to be `u32::MAX - 1`, leaving room for `u32::MAX`
to become a sentinel value in the future.
Diffstat (limited to 'src/librustc_data_structures')
-rw-r--r--src/librustc_data_structures/indexed_vec.rs14
-rw-r--r--src/librustc_data_structures/stable_hasher.rs8
2 files changed, 17 insertions, 5 deletions
diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs
index 571d00118a9..f7d0d7b8a36 100644
--- a/src/librustc_data_structures/indexed_vec.rs
+++ b/src/librustc_data_structures/indexed_vec.rs
@@ -72,7 +72,7 @@ macro_rules! newtype_index {
         newtype_index!(
             // Leave out derives marker so we can use its absence to ensure it comes first
             @type         [$name]
-            @max          [::std::u32::MAX]
+            @max          [::std::u32::MAX - 1]
             @vis          [$v]
             @debug_format ["{}"]);
     );
@@ -82,7 +82,7 @@ macro_rules! newtype_index {
         newtype_index!(
             // Leave out derives marker so we can use its absence to ensure it comes first
             @type         [$name]
-            @max          [::std::u32::MAX]
+            @max          [::std::u32::MAX - 1]
             @vis          [$v]
             @debug_format ["{}"]
                           $($tokens)+);
@@ -102,9 +102,13 @@ macro_rules! newtype_index {
         }
 
         impl $type {
+            $v const MAX_AS_U32: u32 = $max;
+
+            $v const MAX: $type = unsafe { $type::from_u32_unchecked($max) };
+
             #[inline]
             $v fn from_usize(value: usize) -> Self {
-                assert!(value < ($max as usize));
+                assert!(value <= ($max as usize));
                 unsafe {
                     $type::from_u32_unchecked(value as u32)
                 }
@@ -112,7 +116,7 @@ macro_rules! newtype_index {
 
             #[inline]
             $v fn from_u32(value: u32) -> Self {
-                assert!(value < $max);
+                assert!(value <= $max);
                 unsafe {
                     $type::from_u32_unchecked(value)
                 }
@@ -138,7 +142,7 @@ macro_rules! newtype_index {
             /// Extract value of this index as a u32.
             #[inline]
             $v const fn as_usize(self) -> usize {
-                self.private as usize
+                self.as_u32() as usize
             }
         }
 
diff --git a/src/librustc_data_structures/stable_hasher.rs b/src/librustc_data_structures/stable_hasher.rs
index c70a0abe8c7..215c44dec69 100644
--- a/src/librustc_data_structures/stable_hasher.rs
+++ b/src/librustc_data_structures/stable_hasher.rs
@@ -217,6 +217,14 @@ impl_stable_hash_via_hash!(i128);
 impl_stable_hash_via_hash!(char);
 impl_stable_hash_via_hash!(());
 
+impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
+    fn hash_stable<W: StableHasherResult>(&self,
+                                          ctx: &mut CTX,
+                                          hasher: &mut StableHasher<W>) {
+        self.get().hash_stable(ctx, hasher)
+    }
+}
+
 impl<CTX> HashStable<CTX> for f32 {
     fn hash_stable<W: StableHasherResult>(&self,
                                           ctx: &mut CTX,