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