Skip to content

Commit 8adcba4

Browse files
committed
auto merge of #8054 : sammykim/rust/move-EnumSet, r=alexcrichton
Fix #8004
2 parents f8cf234 + a76943b commit 8adcba4

File tree

7 files changed

+30
-66
lines changed

7 files changed

+30
-66
lines changed

src/librustc/util/enum_set.rs renamed to src/libextra/enum_set.rs

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
use std::iterator::Iterator;
1212

1313
#[deriving(Clone, Eq, IterBytes, ToStr)]
14+
/// A specialized Set implementation to use enum types.
1415
pub struct EnumSet<E> {
1516
// We must maintain the invariant that no bits are set
1617
// for which no variant exists
1718
priv bits: uint
1819
}
1920

21+
/// An iterface for casting C-like enum to uint and back.
2022
pub trait CLike {
23+
/// Converts C-like enum to uint.
2124
pub fn to_uint(&self) -> uint;
25+
/// Converts uint to C-like enum.
2226
pub fn from_uint(uint) -> Self;
2327
}
2428

@@ -27,54 +31,47 @@ fn bit<E:CLike>(e: E) -> uint {
2731
}
2832

2933
impl<E:CLike> EnumSet<E> {
34+
/// Returns an empty EnumSet.
3035
pub fn empty() -> EnumSet<E> {
3136
EnumSet {bits: 0}
3237
}
3338

39+
/// Returns true if an EnumSet is empty.
3440
pub fn is_empty(&self) -> bool {
3541
self.bits == 0
3642
}
3743

44+
/// Returns true if an EnumSet contains any enum of a given EnumSet
3845
pub fn intersects(&self, e: EnumSet<E>) -> bool {
3946
(self.bits & e.bits) != 0
4047
}
4148

49+
/// Returns an intersection of both EnumSets.
4250
pub fn intersection(&self, e: EnumSet<E>) -> EnumSet<E> {
4351
EnumSet {bits: self.bits & e.bits}
4452
}
4553

54+
/// Returns true if a given EnumSet is included in an EnumSet.
4655
pub fn contains(&self, e: EnumSet<E>) -> bool {
4756
(self.bits & e.bits) == e.bits
4857
}
4958

59+
/// Returns a union of both EnumSets.
5060
pub fn union(&self, e: EnumSet<E>) -> EnumSet<E> {
5161
EnumSet {bits: self.bits | e.bits}
5262
}
5363

64+
/// Add an enum to an EnumSet
5465
pub fn add(&mut self, e: E) {
5566
self.bits |= bit(e);
5667
}
5768

69+
/// Returns true if an EnumSet contains a given enum
5870
pub fn contains_elem(&self, e: E) -> bool {
5971
(self.bits & bit(e)) != 0
6072
}
6173

62-
pub fn each(&self, f: &fn(E) -> bool) -> bool {
63-
let mut bits = self.bits;
64-
let mut index = 0;
65-
while bits != 0 {
66-
if (bits & 1) != 0 {
67-
let e = CLike::from_uint(index);
68-
if !f(e) {
69-
return false;
70-
}
71-
}
72-
index += 1;
73-
bits >>= 1;
74-
}
75-
return true;
76-
}
77-
74+
/// Returns an iterator over an EnumSet
7875
pub fn iter(&self) -> EnumSetIterator<E> {
7976
EnumSetIterator::new(self.bits)
8077
}
@@ -98,6 +95,7 @@ impl<E:CLike> BitAnd<EnumSet<E>, EnumSet<E>> for EnumSet<E> {
9895
}
9996
}
10097

98+
/// An iterator over an EnumSet
10199
pub struct EnumSetIterator<E> {
102100
priv index: uint,
103101
priv bits: uint,
@@ -136,7 +134,7 @@ mod test {
136134

137135
use std::cast;
138136

139-
use util::enum_set::*;
137+
use enum_set::*;
140138

141139
#[deriving(Eq)]
142140
enum Foo {
@@ -236,7 +234,7 @@ mod test {
236234
}
237235

238236
///////////////////////////////////////////////////////////////////////////
239-
// iter / each
237+
// iter
240238

241239
#[test]
242240
fn test_iterator() {
@@ -262,34 +260,6 @@ mod test {
262260
assert_eq!(~[A,B,C], elems)
263261
}
264262

265-
#[test]
266-
fn test_each() {
267-
let mut e1: EnumSet<Foo> = EnumSet::empty();
268-
269-
assert_eq!(~[], collect(e1))
270-
271-
e1.add(A);
272-
assert_eq!(~[A], collect(e1))
273-
274-
e1.add(C);
275-
assert_eq!(~[A,C], collect(e1))
276-
277-
e1.add(C);
278-
assert_eq!(~[A,C], collect(e1))
279-
280-
e1.add(B);
281-
assert_eq!(~[A,B,C], collect(e1))
282-
}
283-
284-
fn collect(e: EnumSet<Foo>) -> ~[Foo] {
285-
let mut elems = ~[];
286-
e.each(|elem| {
287-
elems.push(elem);
288-
true
289-
});
290-
elems
291-
}
292-
293263
///////////////////////////////////////////////////////////////////////////
294264
// operators
295265

src/libextra/extra.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub mod par;
9191
pub mod base64;
9292
pub mod rl;
9393
pub mod workcache;
94+
pub mod enum_set;
9495
#[path="num/bigint.rs"]
9596
pub mod bigint;
9697
#[path="num/rational.rs"]

src/librustc/metadata/tyencode.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,15 +413,14 @@ fn enc_fn_sig(w: @io::Writer, cx: @ctxt, fsig: &ty::FnSig) {
413413
}
414414

415415
fn enc_bounds(w: @io::Writer, cx: @ctxt, bs: &ty::ParamBounds) {
416-
do bs.builtin_bounds.each |bound| {
416+
for bound in bs.builtin_bounds.iter() {
417417
match bound {
418418
ty::BoundSend => w.write_char('S'),
419419
ty::BoundFreeze => w.write_char('K'),
420420
ty::BoundStatic => w.write_char('O'),
421421
ty::BoundSized => w.write_char('Z'),
422422
}
423-
true
424-
};
423+
}
425424

426425
for &tp in bs.trait_bounds.iter() {
427426
w.write_char('I');

src/librustc/middle/kind.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,11 @@ pub fn check_builtin_bounds(cx: Context, ty: ty::t, bounds: ty::BuiltinBounds,
338338
{
339339
let kind = ty::type_contents(cx.tcx, ty);
340340
let mut missing = ty::EmptyBuiltinBounds();
341-
do bounds.each |bound| {
341+
for bound in bounds.iter() {
342342
if !kind.meets_bound(cx.tcx, bound) {
343343
missing.add(bound);
344344
}
345-
true
346-
};
345+
}
347346
if !missing.is_empty() {
348347
any_missing(missing);
349348
}

src/librustc/middle/ty.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use util::ppaux::{note_and_explain_region, bound_region_ptr_to_str};
2525
use util::ppaux::{trait_store_to_str, ty_to_str, vstore_to_str};
2626
use util::ppaux::{Repr, UserString};
2727
use util::common::{indenter};
28-
use util::enum_set::{EnumSet, CLike};
2928

3029
use std::cast;
3130
use std::cmp;
@@ -48,6 +47,7 @@ use syntax::opt_vec::OptVec;
4847
use syntax::opt_vec;
4948
use syntax::abi::AbiSet;
5049
use syntax;
50+
use extra::enum_set::{EnumSet, CLike};
5151

5252
pub static INITIAL_DISCRIMINANT_VALUE: uint = 0;
5353

@@ -2287,7 +2287,7 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
22872287
// This is like with typarams below, but less "pessimistic" and also
22882288
// dependent on the trait store.
22892289
let mut bt = TC_NONE;
2290-
do (AllBuiltinBounds() - bounds).each |bound| {
2290+
for bound in (AllBuiltinBounds() - bounds).iter() {
22912291
bt = bt + match bound {
22922292
BoundStatic if bounds.contains_elem(BoundSend)
22932293
=> TC_NONE, // Send bound implies static bound.
@@ -2296,8 +2296,7 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
22962296
BoundFreeze => TC_MUTABLE,
22972297
BoundSized => TC_NONE, // don't care if interior is sized
22982298
};
2299-
true
2300-
};
2299+
}
23012300
st + mt + bt
23022301
}
23032302

@@ -2308,7 +2307,7 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
23082307
let _i = indenter();
23092308

23102309
let mut tc = TC_ALL;
2311-
do type_param_def.bounds.builtin_bounds.each |bound| {
2310+
for bound in type_param_def.bounds.builtin_bounds.iter() {
23122311
debug!("tc = %s, bound = %?", tc.to_str(), bound);
23132312
tc = tc - match bound {
23142313
BoundStatic => TypeContents::nonstatic(cx),
@@ -2317,8 +2316,7 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
23172316
// The dynamic-size bit can be removed at pointer-level, etc.
23182317
BoundSized => TypeContents::dynamically_sized(cx),
23192318
};
2320-
true
2321-
};
2319+
}
23222320

23232321
debug!("result = %s", tc.to_str());
23242322
return tc;

src/librustc/rustc.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ pub mod driver;
9696
pub mod util {
9797
pub mod common;
9898
pub mod ppaux;
99-
pub mod enum_set;
10099
}
101100

102101
pub mod lib {

src/librustc/util/ppaux.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -589,15 +589,14 @@ impl Repr for ty::RegionSubsts {
589589
impl Repr for ty::ParamBounds {
590590
fn repr(&self, tcx: ctxt) -> ~str {
591591
let mut res = ~[];
592-
do self.builtin_bounds.each |b| {
592+
for b in self.builtin_bounds.iter() {
593593
res.push(match b {
594594
ty::BoundStatic => ~"'static",
595595
ty::BoundSend => ~"Send",
596596
ty::BoundFreeze => ~"Freeze",
597597
ty::BoundSized => ~"Sized",
598598
});
599-
true
600-
};
599+
}
601600
for t in self.trait_bounds.iter() {
602601
res.push(t.repr(tcx));
603602
}
@@ -833,10 +832,9 @@ impl UserString for ty::BuiltinBounds {
833832
fn user_string(&self, tcx: ctxt) -> ~str {
834833
if self.is_empty() { ~"<no-bounds>" } else {
835834
let mut result = ~[];
836-
do self.each |bb| {
835+
for bb in self.iter() {
837836
result.push(bb.user_string(tcx));
838-
true
839-
};
837+
}
840838
result.connect("+")
841839
}
842840
}

0 commit comments

Comments
 (0)