Skip to content

Commit

Permalink
newindex_type macro: make index private by default and allow pub thro…
Browse files Browse the repository at this point in the history
…ugh config
  • Loading branch information
Nashenas88 committed Nov 2, 2017
1 parent bf1198e commit 97692af
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/def_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use serialize::{self, Encoder, Decoder};
use std::fmt;
use std::u32;

newtype_index!(CrateNum nopub
newtype_index!(CrateNum
{
derive[Debug]
ENCODABLE = custom
Expand Down
6 changes: 5 additions & 1 deletion src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ pub struct BlockRemainder {
pub first_statement_index: FirstStatementIndex,
}

newtype_index!(FirstStatementIndex { MAX = SCOPE_DATA_REMAINDER_MAX });
newtype_index!(FirstStatementIndex
{
pub idx
MAX = SCOPE_DATA_REMAINDER_MAX
});

impl From<ScopeData> for Scope {
#[inline]
Expand Down
101 changes: 53 additions & 48 deletions src/librustc_data_structures/indexed_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ macro_rules! newtype_index {
newtype_index!(
// Leave out derives marker so we can use its absence to ensure it comes first
@type [$name]
@pub [pub]
@max [::std::u32::MAX]
@debug_format ["{}"]);
);

($name:ident nopub) => (
newtype_index!(
// Leave out derives marker so we can use its absence to ensure it comes first
@type [$name]
@pub []
@max [::std::u32::MAX]
@debug_format ["{}"]);
);
Expand All @@ -66,29 +56,17 @@ macro_rules! newtype_index {
newtype_index!(
// Leave out derives marker so we can use its absence to ensure it comes first
@type [$name]
@pub [pub]
@max [::std::u32::MAX]
@debug_format ["{}"]
$($tokens)+);
);

// Define any constants
($name:ident nopub { $($tokens:tt)+ }) => (
newtype_index!(
// Leave out derives marker so we can use its absence to ensure it comes first
@type [$name]
@pub []
@max [::std::u32::MAX]
@debug_format [unsafe {::std::intrinsics::type_name::<$name>() }]
$($tokens)+);
);

// ---- private rules ----

// Base case, user-defined constants (if any) have already been defined
(@derives [$($derives:ident,)*]
@type [$type:ident]
@pub [$($pub:tt)*]
@type [$type:ident]
@max [$max:expr]
@debug_format [$debug_format:expr]) => (
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]
Expand Down Expand Up @@ -148,16 +126,43 @@ macro_rules! newtype_index {
@debug_format [$debug_format]);
);

// Append comma to end of derives list if it's missing
// Handle the case where someone wants to make the internal field public
(@type [$type:ident]
@max [$max:expr]
@debug_format [$debug_format:expr]
pub idx
$($tokens:tt)*) => (
newtype_index!(
@pub [pub]
@type [$type]
@max [$max]
@debug_format [$debug_format]
$($tokens)*);
);

// The default case is that the internal field is private
(@type [$type:ident]
@pub [$($pub:tt)*]
@max [$max:expr]
@debug_format [$debug_format:expr]
derive [$($derives:ident),*]
$($tokens:tt)*) => (
newtype_index!(
@pub []
@type [$type]
@max [$max]
@debug_format [$debug_format]
$($tokens)*);
);

// Append comma to end of derives list if it's missing
(@pub [$($pub:tt)*]
@type [$type:ident]
@max [$max:expr]
@debug_format [$debug_format:expr]
derive [$($derives:ident),*]
$($tokens:tt)*) => (
newtype_index!(
@pub [$($pub)*]
@type [$type]
@max [$max]
@debug_format [$debug_format]
derive [$($derives,)*]
Expand All @@ -166,142 +171,142 @@ macro_rules! newtype_index {

// By not including the @derives marker in this list nor in the default args, we can force it
// to come first if it exists. When encodable is custom, just use the derives list as-is.
(@type [$type:ident]
@pub [$($pub:tt)*]
(@pub [$($pub:tt)*]
@type [$type:ident]
@max [$max:expr]
@debug_format [$debug_format:expr]
derive [$($derives:ident,)+]
ENCODABLE = custom
$($tokens:tt)*) => (
newtype_index!(
@derives [$($derives,)+]
@type [$type]
@pub [$($pub)*]
@type [$type]
@max [$max]
@debug_format [$debug_format]
$($tokens)*);
);

// By not including the @derives marker in this list nor in the default args, we can force it
// to come first if it exists. When encodable isn't custom, add serialization traits by default.
(@type [$type:ident]
@pub [$($pub:tt)*]
(@pub [$($pub:tt)*]
@type [$type:ident]
@max [$max:expr]
@debug_format [$debug_format:expr]
derive [$($derives:ident,)+]
$($tokens:tt)*) => (
newtype_index!(
@derives [$($derives,)+ RustcDecodable, RustcEncodable,]
@type [$type]
@pub [$($pub)*]
@type [$type]
@max [$max]
@debug_format [$debug_format]
$($tokens)*);
);

// The case where no derives are added, but encodable is overriden. Don't
// derive serialization traits
(@type [$type:ident]
@pub [$($pub:tt)*]
(@pub [$($pub:tt)*]
@type [$type:ident]
@max [$max:expr]
@debug_format [$debug_format:expr]
ENCODABLE = custom
$($tokens:tt)*) => (
newtype_index!(
@derives []
@type [$type]
@pub [$($pub)*]
@type [$type]
@max [$max]
@debug_format [$debug_format]
$($tokens)*);
);

// The case where no derives are added, add serialization derives by default
(@type [$type:ident]
@pub [$($pub:tt)*]
(@pub [$($pub:tt)*]
@type [$type:ident]
@max [$max:expr]
@debug_format [$debug_format:expr]
$($tokens:tt)*) => (
newtype_index!(
@derives [RustcDecodable, RustcEncodable,]
@type [$type]
@pub [$($pub)*]
@type [$type]
@max [$max]
@debug_format [$debug_format]
$($tokens)*);
);

// Rewrite final without comma to one that includes comma
(@derives [$($derives:ident,)*]
@type [$type:ident]
@pub [$($pub:tt)*]
@type [$type:ident]
@max [$max:expr]
@debug_format [$debug_format:expr]
$name:ident = $constant:expr) => (
newtype_index!(
@derives [$($derives,)*]
@type [$type]
@pub [$($pub)*]
@type [$type]
@max [$max]
@debug_format [$debug_format]
$name = $constant,);
);

// Rewrite final const without comma to one that includes comma
(@derives [$($derives:ident,)*]
@type [$type:ident]
@pub [$($pub:tt)*]
@type [$type:ident]
@max [$_max:expr]
@debug_format [$debug_format:expr]
$(#[doc = $doc:expr])*
const $name:ident = $constant:expr) => (
newtype_index!(
@derives [$($derives,)*]
@type [$type]
@pub [$($pub)*]
@type [$type]
@max [$max]
@debug_format [$debug_format]
$(#[doc = $doc])* const $name = $constant,);
);

// Replace existing default for max
(@derives [$($derives:ident,)*]
@type [$type:ident]
@pub [$($pub:tt)*]
@type [$type:ident]
@max [$_max:expr]
@debug_format [$debug_format:expr]
MAX = $max:expr,
$($tokens:tt)*) => (
newtype_index!(
@derives [$($derives,)*]
@type [$type]
@pub [$($pub)*]
@type [$type]
@max [$max]
@debug_format [$debug_format]
$($tokens)*);
);

// Replace existing default for debug_format
(@derives [$($derives:ident,)*]
@type [$type:ident]
@pub [$($pub:tt)*]
@type [$type:ident]
@max [$max:expr]
@debug_format [$_debug_format:expr]
DEBUG_FORMAT = $debug_format:expr,
$($tokens:tt)*) => (
newtype_index!(
@derives [$($derives,)*]
@type [$type]
@pub [$($pub)*]
@type [$type]
@max [$max]
@debug_format [$debug_format]
$($tokens)*);
);

// Assign a user-defined constant
(@derives [$($derives:ident,)*]
@type [$type:ident]
@pub [$($pub:tt)*]
@type [$type:ident]
@max [$max:expr]
@debug_format [$debug_format:expr]
$(#[doc = $doc:expr])*
Expand All @@ -311,8 +316,8 @@ macro_rules! newtype_index {
pub const $name: $type = $type($constant);
newtype_index!(
@derives [$($derives,)*]
@type [$type]
@pub [$($pub)*]
@type [$type]
@max [$max]
@debug_format [$debug_format]
$($tokens)*);
Expand Down

0 comments on commit 97692af

Please sign in to comment.