]> git.lizzy.rs Git - rust.git/blob - src/librustc_session/node_id.rs
Rollup merge of #67845 - jumbatm:also-unconst-hack-abs, r=oli-obk
[rust.git] / src / librustc_session / node_id.rs
1 use rustc_index::vec::Idx;
2 use rustc_serialize::{Decoder, Encoder};
3 use rustc_span::ExpnId;
4 use std::fmt;
5
6 rustc_index::newtype_index! {
7     pub struct NodeId {
8         ENCODABLE = custom
9         DEBUG_FORMAT = "NodeId({})"
10     }
11 }
12
13 rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeId);
14
15 impl NodeId {
16     pub fn placeholder_from_expn_id(expn_id: ExpnId) -> Self {
17         NodeId::from_u32(expn_id.as_u32())
18     }
19
20     pub fn placeholder_to_expn_id(self) -> ExpnId {
21         ExpnId::from_u32(self.as_u32())
22     }
23 }
24
25 impl fmt::Display for NodeId {
26     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27         fmt::Display::fmt(&self.as_u32(), f)
28     }
29 }
30
31 impl rustc_serialize::UseSpecializedEncodable for NodeId {
32     fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
33         s.emit_u32(self.as_u32())
34     }
35 }
36
37 impl rustc_serialize::UseSpecializedDecodable for NodeId {
38     fn default_decode<D: Decoder>(d: &mut D) -> Result<NodeId, D::Error> {
39         d.read_u32().map(NodeId::from_u32)
40     }
41 }