-
-
Save anonymous/369cc785baa07a54ca69e67b236878b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 5e1ba2cd5f401efb5bb674bf8347a7ea8a8bc890 Mon Sep 17 00:00:00 2001 | |
From: Santiago Pastorino <spastorino@gmail.com> | |
Date: Thu, 28 Sep 2017 20:20:08 -0300 | |
Subject: [PATCH] Generate CrateNum using newtype_index macro | |
--- | |
src/librustc/hir/def_id.rs | 25 ++++--------------------- | |
src/librustc_data_structures/indexed_vec.rs | 4 ++-- | |
2 files changed, 6 insertions(+), 23 deletions(-) | |
diff --git a/src/librustc/hir/def_id.rs b/src/librustc/hir/def_id.rs | |
index 8e48352007..6552b3d450 100644 | |
--- a/src/librustc/hir/def_id.rs | |
+++ b/src/librustc/hir/def_id.rs | |
@@ -16,37 +16,20 @@ use serialize::{self, Encoder, Decoder}; | |
use std::fmt; | |
use std::u32; | |
-#[derive(Clone, Copy, Eq, Ord, PartialOrd, PartialEq, Hash, Debug)] | |
-pub struct CrateNum(u32); | |
- | |
-impl Idx for CrateNum { | |
- fn new(value: usize) -> Self { | |
- assert!(value < (u32::MAX) as usize); | |
- CrateNum(value as u32) | |
- } | |
- | |
- fn index(self) -> usize { | |
- self.0 as usize | |
- } | |
-} | |
+newtype_index!(CrateNum); | |
/// Item definitions in the currently-compiled crate would have the CrateNum | |
/// LOCAL_CRATE in their DefId. | |
-pub const LOCAL_CRATE: CrateNum = CrateNum(0); | |
+pub const LOCAL_CRATE: CrateNum = CrateNum::const_new(0); | |
/// Virtual crate for builtin macros | |
// FIXME(jseyfried): this is also used for custom derives until proc-macro crates get `CrateNum`s. | |
-pub const BUILTIN_MACROS_CRATE: CrateNum = CrateNum(u32::MAX); | |
+pub const BUILTIN_MACROS_CRATE: CrateNum = CrateNum::const_new(u32::MAX); | |
/// A CrateNum value that indicates that something is wrong. | |
-pub const INVALID_CRATE: CrateNum = CrateNum(u32::MAX - 1); | |
+pub const INVALID_CRATE: CrateNum = CrateNum::const_new(u32::MAX - 1); | |
impl CrateNum { | |
- pub fn new(x: usize) -> CrateNum { | |
- assert!(x < (u32::MAX as usize)); | |
- CrateNum(x as u32) | |
- } | |
- | |
pub fn from_u32(x: u32) -> CrateNum { | |
CrateNum(x) | |
} | |
diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs | |
index dfdd23a369..d627fa85eb 100644 | |
--- a/src/librustc_data_structures/indexed_vec.rs | |
+++ b/src/librustc_data_structures/indexed_vec.rs | |
@@ -54,11 +54,11 @@ macro_rules! newtype_index { | |
} | |
impl Idx for $name { | |
- fn new(value: usize) -> Self { | |
+ pub fn new(value: usize) -> Self { | |
assert!(value < (::std::u32::MAX) as usize); | |
$name(value as u32) | |
} | |
- fn index(self) -> usize { | |
+ pub fn index(self) -> usize { | |
self.0 as usize | |
} | |
} | |
-- | |
2.14.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment