@@ -36,6 +36,44 @@ macro_rules! expand_group {
36
36
} ;
37
37
}
38
38
39
+ /// All of the language items, defined or not.
40
+ /// Defined lang items can come from the current crate or its dependencies.
41
+ #[ derive( HashStable_Generic , Debug ) ]
42
+ pub struct LanguageItems {
43
+ /// Mappings from lang items to their possibly found [`DefId`]s.
44
+ /// The index corresponds to the order in [`LangItem`].
45
+ pub items : Vec < Option < DefId > > ,
46
+ /// Lang items that were not found during collection.
47
+ pub missing : Vec < LangItem > ,
48
+ /// Mapping from [`LangItemGroup`] discriminants to all
49
+ /// [`DefId`]s of lang items in that group.
50
+ pub groups : [ Vec < DefId > ; NUM_GROUPS ] ,
51
+ }
52
+
53
+ impl LanguageItems {
54
+ pub fn get ( & self , item : LangItem ) -> Option < DefId > {
55
+ self . items [ item as usize ]
56
+ }
57
+
58
+ pub fn set ( & mut self , item : LangItem , def_id : DefId ) {
59
+ self . items [ item as usize ] = Some ( def_id) ;
60
+ }
61
+
62
+ /// Requires that a given `LangItem` was bound and returns the corresponding `DefId`.
63
+ /// If it wasn't bound, e.g. due to a missing `#[lang = "<it.name()>"]`,
64
+ /// returns an error encapsulating the `LangItem`.
65
+ pub fn require ( & self , it : LangItem ) -> Result < DefId , LangItemError > {
66
+ self . get ( it) . ok_or_else ( || LangItemError ( it) )
67
+ }
68
+
69
+ pub fn iter < ' a > ( & ' a self ) -> impl Iterator < Item = ( LangItem , DefId ) > + ' a {
70
+ self . items
71
+ . iter ( )
72
+ . enumerate ( )
73
+ . filter_map ( |( i, id) | id. map ( |id| ( LangItem :: from_u32 ( i as u32 ) . unwrap ( ) , id) ) )
74
+ }
75
+ }
76
+
39
77
// The actual lang items defined come at the end of this file in one handy table.
40
78
// So you probably just want to nip down to the end.
41
79
macro_rules! language_item_table {
@@ -82,20 +120,6 @@ macro_rules! language_item_table {
82
120
}
83
121
}
84
122
85
- /// All of the language items, defined or not.
86
- /// Defined lang items can come from the current crate or its dependencies.
87
- #[ derive( HashStable_Generic , Debug ) ]
88
- pub struct LanguageItems {
89
- /// Mappings from lang items to their possibly found [`DefId`]s.
90
- /// The index corresponds to the order in [`LangItem`].
91
- pub items: Vec <Option <DefId >>,
92
- /// Lang items that were not found during collection.
93
- pub missing: Vec <LangItem >,
94
- /// Mapping from [`LangItemGroup`] discriminants to all
95
- /// [`DefId`]s of lang items in that group.
96
- pub groups: [ Vec <DefId >; NUM_GROUPS ] ,
97
- }
98
-
99
123
impl LanguageItems {
100
124
/// Construct an empty collection of lang items and no missing ones.
101
125
pub fn new( ) -> Self {
@@ -114,13 +138,6 @@ macro_rules! language_item_table {
114
138
& * self . items
115
139
}
116
140
117
- /// Requires that a given `LangItem` was bound and returns the corresponding `DefId`.
118
- /// If it wasn't bound, e.g. due to a missing `#[lang = "<it.name()>"]`,
119
- /// returns an error encapsulating the `LangItem`.
120
- pub fn require( & self , it: LangItem ) -> Result <DefId , LangItemError > {
121
- self . items[ it as usize ] . ok_or_else( || LangItemError ( it) )
122
- }
123
-
124
141
/// Returns the [`DefId`]s of all lang items in a group.
125
142
pub fn group( & self , group: LangItemGroup ) -> & [ DefId ] {
126
143
self . groups[ group as usize ] . as_ref( )
0 commit comments