14
14
//! representation to hold C-like enum variants.
15
15
16
16
use core:: prelude:: * ;
17
+ use core:: marker;
17
18
use core:: fmt;
18
19
use core:: num:: Int ;
19
20
use core:: iter:: { FromIterator , IntoIterator } ;
@@ -26,7 +27,8 @@ use core::ops::{Sub, BitOr, BitAnd, BitXor};
26
27
pub struct EnumSet < E > {
27
28
// We must maintain the invariant that no bits are set
28
29
// for which no variant exists
29
- bits : usize
30
+ bits : usize ,
31
+ marker : marker:: PhantomData < E > ,
30
32
}
31
33
32
34
impl < E > Copy for EnumSet < E > { }
@@ -86,7 +88,7 @@ impl<E:CLike> EnumSet<E> {
86
88
#[ unstable( feature = "collections" ,
87
89
reason = "matches collection reform specification, waiting for dust to settle" ) ]
88
90
pub fn new ( ) -> EnumSet < E > {
89
- EnumSet { bits : 0 }
91
+ EnumSet { bits : 0 , marker : marker :: PhantomData }
90
92
}
91
93
92
94
/// Returns the number of elements in the given `EnumSet`.
@@ -130,12 +132,14 @@ impl<E:CLike> EnumSet<E> {
130
132
131
133
/// Returns the union of both `EnumSets`.
132
134
pub fn union ( & self , e : EnumSet < E > ) -> EnumSet < E > {
133
- EnumSet { bits : self . bits | e. bits }
135
+ EnumSet { bits : self . bits | e. bits ,
136
+ marker : marker:: PhantomData }
134
137
}
135
138
136
139
/// Returns the intersection of both `EnumSets`.
137
140
pub fn intersection ( & self , e : EnumSet < E > ) -> EnumSet < E > {
138
- EnumSet { bits : self . bits & e. bits }
141
+ EnumSet { bits : self . bits & e. bits ,
142
+ marker : marker:: PhantomData }
139
143
}
140
144
141
145
/// Adds an enum to the `EnumSet`, and returns `true` if it wasn't there before
@@ -175,38 +179,39 @@ impl<E:CLike> Sub for EnumSet<E> {
175
179
type Output = EnumSet < E > ;
176
180
177
181
fn sub ( self , e : EnumSet < E > ) -> EnumSet < E > {
178
- EnumSet { bits : self . bits & !e. bits }
182
+ EnumSet { bits : self . bits & !e. bits , marker : marker :: PhantomData }
179
183
}
180
184
}
181
185
182
186
impl < E : CLike > BitOr for EnumSet < E > {
183
187
type Output = EnumSet < E > ;
184
188
185
189
fn bitor ( self , e : EnumSet < E > ) -> EnumSet < E > {
186
- EnumSet { bits : self . bits | e. bits }
190
+ EnumSet { bits : self . bits | e. bits , marker : marker :: PhantomData }
187
191
}
188
192
}
189
193
190
194
impl < E : CLike > BitAnd for EnumSet < E > {
191
195
type Output = EnumSet < E > ;
192
196
193
197
fn bitand ( self , e : EnumSet < E > ) -> EnumSet < E > {
194
- EnumSet { bits : self . bits & e. bits }
198
+ EnumSet { bits : self . bits & e. bits , marker : marker :: PhantomData }
195
199
}
196
200
}
197
201
198
202
impl < E : CLike > BitXor for EnumSet < E > {
199
203
type Output = EnumSet < E > ;
200
204
201
205
fn bitxor ( self , e : EnumSet < E > ) -> EnumSet < E > {
202
- EnumSet { bits : self . bits ^ e. bits }
206
+ EnumSet { bits : self . bits ^ e. bits , marker : marker :: PhantomData }
203
207
}
204
208
}
205
209
206
210
/// An iterator over an EnumSet
207
211
pub struct Iter < E > {
208
212
index : usize ,
209
213
bits : usize ,
214
+ marker : marker:: PhantomData < E > ,
210
215
}
211
216
212
217
// FIXME(#19839) Remove in favor of `#[derive(Clone)]`
@@ -215,13 +220,14 @@ impl<E> Clone for Iter<E> {
215
220
Iter {
216
221
index : self . index ,
217
222
bits : self . bits ,
223
+ marker : marker:: PhantomData ,
218
224
}
219
225
}
220
226
}
221
227
222
228
impl < E : CLike > Iter < E > {
223
229
fn new ( bits : usize ) -> Iter < E > {
224
- Iter { index : 0 , bits : bits }
230
+ Iter { index : 0 , bits : bits, marker : marker :: PhantomData }
225
231
}
226
232
}
227
233
0 commit comments