ast: Compress AttrId from usize to u32

Also stop encoding/decoding it entirely
This commit is contained in:
Vadim Petrochenkov 2020-03-21 02:16:27 +03:00
parent 5f13820478
commit 13dd9aff64
2 changed files with 12 additions and 18 deletions

View File

@ -31,7 +31,6 @@ use crate::tokenstream::{DelimSpan, TokenStream, TokenTree};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::thin_vec::ThinVec;
use rustc_index::vec::Idx;
use rustc_macros::HashStable_Generic;
use rustc_serialize::{self, Decoder, Encoder};
use rustc_span::source_map::{respan, Spanned};
@ -2251,27 +2250,22 @@ pub enum AttrStyle {
Inner,
}
#[derive(Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, Copy)]
pub struct AttrId(pub usize);
impl Idx for AttrId {
fn new(idx: usize) -> Self {
AttrId(idx)
}
fn index(self) -> usize {
self.0
rustc_index::newtype_index! {
pub struct AttrId {
ENCODABLE = custom
DEBUG_FORMAT = "AttrId({})"
}
}
impl rustc_serialize::Encodable for AttrId {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_unit()
fn encode<S: Encoder>(&self, _: &mut S) -> Result<(), S::Error> {
Ok(())
}
}
impl rustc_serialize::Decodable for AttrId {
fn decode<D: Decoder>(d: &mut D) -> Result<AttrId, D::Error> {
d.read_nil().map(|_| crate::attr::mk_attr_id())
fn decode<D: Decoder>(_: &mut D) -> Result<AttrId, D::Error> {
Ok(crate::attr::mk_attr_id())
}
}

View File

@ -366,14 +366,14 @@ pub fn mk_nested_word_item(ident: Ident) -> NestedMetaItem {
}
crate fn mk_attr_id() -> AttrId {
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::AtomicU32;
use std::sync::atomic::Ordering;
static NEXT_ATTR_ID: AtomicUsize = AtomicUsize::new(0);
static NEXT_ATTR_ID: AtomicU32 = AtomicU32::new(0);
let id = NEXT_ATTR_ID.fetch_add(1, Ordering::SeqCst);
assert!(id != ::std::usize::MAX);
AttrId(id)
assert!(id != u32::MAX);
AttrId::from_u32(id)
}
pub fn mk_attr(style: AttrStyle, path: Path, args: MacArgs, span: Span) -> Attribute {