Skip to content

Commit 5606fc0

Browse files
committed
Revert map.each to something which takes two parameters
rather than a tuple. The current setup iterates over `BaseIter<(&'self K, &'self V)>` where 'self is a lifetime declared *in the each method*. You can't place such a type in the impl declaration. The compiler currently allows it, but this will not be legal under #5656 and I'm pretty sure it's not sound now.
1 parent 7222801 commit 5606fc0

19 files changed

+132
-158
lines changed

src/libcore/container.rs

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pub trait Map<K, V>: Mutable {
2929
/// Return true if the map contains a value for the specified key
3030
fn contains_key(&self, key: &K) -> bool;
3131

32+
// Visits all keys and values
33+
fn each(&self, f: &fn(&K, &V) -> bool);
34+
3235
/// Visit all keys
3336
fn each_key(&self, f: &fn(&K) -> bool);
3437

src/libcore/hashmap.rs

+16-23
Original file line numberDiff line numberDiff line change
@@ -279,24 +279,6 @@ priv impl<K:Hash + IterBytes + Eq,V> HashMap<K, V> {
279279
}
280280
}
281281
282-
impl<'self,K:Hash + IterBytes + Eq,V>
283-
BaseIter<(&'self K, &'self V)> for HashMap<K, V> {
284-
/// Visit all key-value pairs
285-
fn each(&self, blk: &fn(&(&'self K, &'self V)) -> bool) {
286-
for uint::range(0, self.buckets.len()) |i| {
287-
let mut broke = false;
288-
do self.buckets[i].map |bucket| {
289-
if !blk(&(&bucket.key, &bucket.value)) {
290-
broke = true; // FIXME(#3064) just write "break;"
291-
}
292-
};
293-
if broke { break; }
294-
}
295-
}
296-
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
297-
}
298-
299-
300282
impl<K:Hash + IterBytes + Eq,V> Container for HashMap<K, V> {
301283
/// Return the number of elements in the map
302284
fn len(&const self) -> uint { self.size }
@@ -315,7 +297,7 @@ impl<K:Hash + IterBytes + Eq,V> Mutable for HashMap<K, V> {
315297
}
316298
}
317299
318-
impl<'self,K:Hash + IterBytes + Eq,V> Map<K, V> for HashMap<K, V> {
300+
impl<K:Hash + IterBytes + Eq,V> Map<K, V> for HashMap<K, V> {
319301
/// Return true if the map contains a value for the specified key
320302
fn contains_key(&self, k: &K) -> bool {
321303
match self.bucket_for_key(k) {
@@ -324,14 +306,25 @@ impl<'self,K:Hash + IterBytes + Eq,V> Map<K, V> for HashMap<K, V> {
324306
}
325307
}
326308
309+
/// Visit all key-value pairs
310+
fn each(&self, blk: &fn(&'self K, &'self V) -> bool) {
311+
for uint::range(0, self.buckets.len()) |i| {
312+
for self.buckets[i].each |bucket| {
313+
if !blk(&bucket.key, &bucket.value) {
314+
return;
315+
}
316+
}
317+
}
318+
}
319+
327320
/// Visit all keys
328321
fn each_key(&self, blk: &fn(k: &K) -> bool) {
329-
self.each(|&(k, _)| blk(k))
322+
self.each(|k, _| blk(k))
330323
}
331324
332325
/// Visit all values
333326
fn each_value(&self, blk: &fn(v: &V) -> bool) {
334-
self.each(|&(_, v)| blk(v))
327+
self.each(|_, v| blk(v))
335328
}
336329
337330
/// Iterate over the map and mutate the contained values
@@ -545,7 +538,7 @@ impl<K:Hash + IterBytes + Eq,V:Eq> Eq for HashMap<K, V> {
545538
fn eq(&self, other: &HashMap<K, V>) -> bool {
546539
if self.len() != other.len() { return false; }
547540

548-
for self.each |&(key, value)| {
541+
for self.each |key, value| {
549542
match other.find(key) {
550543
None => return false,
551544
Some(v) => if value != v { return false },
@@ -798,7 +791,7 @@ mod test_map {
798791
assert!(m.insert(i, i*2));
799792
}
800793
let mut observed = 0;
801-
for m.each |&(k, v)| {
794+
for m.each |k, v| {
802795
assert!(*v == *k * 2);
803796
observed |= (1 << *k);
804797
}

src/libcore/trie.rs

+28-34
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,6 @@ pub struct TrieMap<T> {
2828
priv length: uint
2929
}
3030

31-
impl<'self,T> BaseIter<(uint, &'self T)> for TrieMap<T> {
32-
/// Visit all key-value pairs in order
33-
#[inline(always)]
34-
fn each(&self, f: &fn(&(uint, &'self T)) -> bool) {
35-
self.root.each(f);
36-
}
37-
#[inline(always)]
38-
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
39-
}
40-
41-
impl<'self,T> ReverseIter<(uint, &'self T)> for TrieMap<T> {
42-
/// Visit all key-value pairs in reverse order
43-
#[inline(always)]
44-
fn each_reverse(&self, f: &fn(&(uint, &'self T)) -> bool) {
45-
self.root.each_reverse(f);
46-
}
47-
}
48-
4931
impl<T> Container for TrieMap<T> {
5032
/// Return the number of elements in the map
5133
#[inline(always)]
@@ -72,16 +54,22 @@ impl<T> Map<uint, T> for TrieMap<T> {
7254
self.find(key).is_some()
7355
}
7456

57+
/// Visit all key-value pairs in order
58+
#[inline(always)]
59+
fn each(&self, f: &fn(&uint, &'self T) -> bool) {
60+
self.root.each(f);
61+
}
62+
7563
/// Visit all keys in order
7664
#[inline(always)]
7765
fn each_key(&self, f: &fn(&uint) -> bool) {
78-
self.each(|&(k, _)| f(&k))
66+
self.each(|k, _| f(k))
7967
}
8068

8169
/// Visit all values in order
8270
#[inline(always)]
8371
fn each_value(&self, f: &fn(&T) -> bool) {
84-
self.each(|&(_, v)| f(v))
72+
self.each(|_, v| f(v))
8573
}
8674

8775
/// Iterate over the map and mutate the contained values
@@ -148,16 +136,22 @@ pub impl<T> TrieMap<T> {
148136
TrieMap{root: TrieNode::new(), length: 0}
149137
}
150138

139+
/// Visit all key-value pairs in reverse order
140+
#[inline(always)]
141+
fn each_reverse(&self, f: &fn(&uint, &'self T) -> bool) {
142+
self.root.each_reverse(f);
143+
}
144+
151145
/// Visit all keys in reverse order
152146
#[inline(always)]
153147
fn each_key_reverse(&self, f: &fn(&uint) -> bool) {
154-
self.each_reverse(|&(k, _)| f(&k))
148+
self.each_reverse(|k, _| f(k))
155149
}
156150

157151
/// Visit all values in reverse order
158152
#[inline(always)]
159153
fn each_value_reverse(&self, f: &fn(&T) -> bool) {
160-
self.each_reverse(|&(_, v)| f(v))
154+
self.each_reverse(|_, v| f(v))
161155
}
162156
}
163157

@@ -239,22 +233,22 @@ impl<T> TrieNode<T> {
239233
}
240234

241235
impl<T> TrieNode<T> {
242-
fn each(&self, f: &fn(&(uint, &'self T)) -> bool) -> bool {
236+
fn each(&self, f: &fn(&uint, &'self T) -> bool) -> bool {
243237
for uint::range(0, self.children.len()) |idx| {
244238
match self.children[idx] {
245239
Internal(ref x) => if !x.each(f) { return false },
246-
External(k, ref v) => if !f(&(k, v)) { return false },
240+
External(k, ref v) => if !f(&k, v) { return false },
247241
Nothing => ()
248242
}
249243
}
250244
true
251245
}
252246

253-
fn each_reverse(&self, f: &fn(&(uint, &'self T)) -> bool) -> bool {
247+
fn each_reverse(&self, f: &fn(&uint, &'self T) -> bool) -> bool {
254248
for uint::range_rev(self.children.len(), 0) |idx| {
255249
match self.children[idx - 1] {
256250
Internal(ref x) => if !x.each_reverse(f) { return false },
257-
External(k, ref v) => if !f(&(k, v)) { return false },
251+
External(k, ref v) => if !f(&k, v) { return false },
258252
Nothing => ()
259253
}
260254
}
@@ -438,8 +432,8 @@ mod tests {
438432
assert!(m.insert(1, 2));
439433

440434
let mut n = 0;
441-
for m.each |&(k, v)| {
442-
assert!(k == n);
435+
for m.each |k, v| {
436+
assert!(*k == n);
443437
assert!(*v == n * 2);
444438
n += 1;
445439
}
@@ -454,11 +448,11 @@ mod tests {
454448
}
455449

456450
let mut n = uint::max_value - 9999;
457-
for m.each |&(k, v)| {
451+
for m.each |k, v| {
458452
if n == uint::max_value - 5000 { break }
459453
assert!(n < uint::max_value - 5000);
460454

461-
assert!(k == n);
455+
assert!(*k == n);
462456
assert!(*v == n / 2);
463457
n += 1;
464458
}
@@ -475,8 +469,8 @@ mod tests {
475469
assert!(m.insert(1, 2));
476470

477471
let mut n = 4;
478-
for m.each_reverse |&(k, v)| {
479-
assert!(k == n);
472+
for m.each_reverse |k, v| {
473+
assert!(*k == n);
480474
assert!(*v == n * 2);
481475
n -= 1;
482476
}
@@ -491,11 +485,11 @@ mod tests {
491485
}
492486

493487
let mut n = uint::max_value;
494-
for m.each_reverse |&(k, v)| {
488+
for m.each_reverse |k, v| {
495489
if n == uint::max_value - 5000 { break }
496490
assert!(n > uint::max_value - 5000);
497491

498-
assert!(k == n);
492+
assert!(*k == n);
499493
assert!(*v == n / 2);
500494
n -= 1;
501495
}

src/librustc/metadata/cstore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub fn have_crate_data(cstore: &CStore, cnum: ast::crate_num) -> bool {
8686

8787
pub fn iter_crate_data(cstore: &CStore,
8888
i: &fn(ast::crate_num, @crate_metadata)) {
89-
for cstore.metas.each |&(&k, &v)| {
89+
for cstore.metas.each |&k, &v| {
9090
i(k, v);
9191
}
9292
}

src/librustc/middle/lang_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ pub impl<'self> LanguageItemCollector<'self> {
397397
}
398398

399399
fn check_completeness(&self) {
400-
for self.item_refs.each |&(&key, &item_ref)| {
400+
for self.item_refs.each |&key, &item_ref| {
401401
match self.items.items[item_ref] {
402402
None => {
403403
self.session.err(fmt!("no item found for `%s`", *key));

src/librustc/middle/lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ pub fn build_settings_crate(sess: session::Session, crate: @ast::crate) {
460460

461461
do cx.with_lint_attrs(/*bad*/copy crate.node.attrs) |cx| {
462462
// Copy out the default settings
463-
for cx.curr.each |&(k, &v)| {
463+
for cx.curr.each |&k, &v| {
464464
sess.lint_settings.default_settings.insert(k, v);
465465
}
466466

src/librustc/middle/region.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ pub fn determine_rp_in_crate(sess: Session,
842842
debug!("%s", {
843843
debug!("Region variance results:");
844844
let region_paramd_items = cx.region_paramd_items;
845-
for region_paramd_items.each |&(&key, &value)| {
845+
for region_paramd_items.each |&key, &value| {
846846
debug!("item %? (%s) is parameterized with variance %?",
847847
key,
848848
ast_map::node_id_to_str(ast_map, key,

src/librustc/middle/resolve.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2369,7 +2369,7 @@ pub impl Resolver {
23692369
23702370
// Add all resolved imports from the containing module.
23712371
for containing_module.import_resolutions.each
2372-
|&(ident, target_import_resolution)| {
2372+
|ident, target_import_resolution| {
23732373
23742374
debug!("(resolving glob import) writing module resolution \
23752375
%? into `%s`",
@@ -2457,13 +2457,13 @@ pub impl Resolver {
24572457
};
24582458

24592459
// Add all children from the containing module.
2460-
for containing_module.children.each |&(ident, name_bindings)| {
2460+
for containing_module.children.each |ident, name_bindings| {
24612461
merge_import_resolution(ident, *name_bindings);
24622462
}
24632463

24642464
// Add external module children from the containing module.
24652465
for containing_module.external_module_children.each
2466-
|&(ident, module)| {
2466+
|ident, module| {
24672467
let name_bindings =
24682468
@mut Resolver::create_name_bindings_from_module(*module);
24692469
merge_import_resolution(ident, name_bindings);
@@ -3111,7 +3111,7 @@ pub impl Resolver {
31113111
fn add_exports_for_module(@mut self,
31123112
exports2: &mut ~[Export2],
31133113
module_: @mut Module) {
3114-
for module_.children.each |&(ident, namebindings)| {
3114+
for module_.children.each |ident, namebindings| {
31153115
debug!("(computing exports) maybe export '%s'",
31163116
*self.session.str_of(*ident));
31173117
self.add_exports_of_namebindings(&mut *exports2,
@@ -3126,7 +3126,7 @@ pub impl Resolver {
31263126
false);
31273127
}
31283128
3129-
for module_.import_resolutions.each |&(ident, importresolution)| {
3129+
for module_.import_resolutions.each |ident, importresolution| {
31303130
if importresolution.privacy != Public {
31313131
debug!("(computing exports) not reexporting private `%s`",
31323132
*self.session.str_of(*ident));
@@ -3934,7 +3934,7 @@ pub impl Resolver {
39343934
for arm.pats.eachi() |i, p| {
39353935
let map_i = self.binding_mode_map(*p);
39363936

3937-
for map_0.each |&(&key, &binding_0)| {
3937+
for map_0.each |&key, &binding_0| {
39383938
match map_i.find(&key) {
39393939
None => {
39403940
self.session.span_err(
@@ -3955,7 +3955,7 @@ pub impl Resolver {
39553955
}
39563956
}
39573957

3958-
for map_i.each |&(&key, &binding)| {
3958+
for map_i.each |&key, &binding| {
39593959
if !map_0.contains_key(&key) {
39603960
self.session.span_err(
39613961
binding.span,
@@ -5248,7 +5248,7 @@ pub impl Resolver {
52485248
}
52495249

52505250
debug!("Import resolutions:");
5251-
for module_.import_resolutions.each |&(name, import_resolution)| {
5251+
for module_.import_resolutions.each |name, import_resolution| {
52525252
let mut value_repr;
52535253
match import_resolution.target_for_namespace(ValueNS) {
52545254
None => { value_repr = ~""; }

src/librustc/middle/trans/base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2848,7 +2848,7 @@ pub fn create_module_map(ccx: @CrateContext) -> ValueRef {
28482848
lib::llvm::SetLinkage(map, lib::llvm::InternalLinkage);
28492849
}
28502850
let mut elts: ~[ValueRef] = ~[];
2851-
for ccx.module_data.each |&(key, &val)| {
2851+
for ccx.module_data.each |key, &val| {
28522852
let elt = C_struct(~[p2i(ccx, C_cstr(ccx, @/*bad*/ copy *key)),
28532853
p2i(ccx, val)]);
28542854
elts.push(elt);
@@ -3139,7 +3139,7 @@ pub fn trans_crate(sess: session::Session,
31393139
}
31403140
31413141
if ccx.sess.count_llvm_insns() {
3142-
for ccx.stats.llvm_insns.each |&(&k, &v)| {
3142+
for ccx.stats.llvm_insns.each |&k, &v| {
31433143
io::println(fmt!("%-7u %s", v, k));
31443144
}
31453145
}

src/librustc/middle/typeck/infer/region_inference.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ pub impl RegionVarBindings {
12231223

12241224
// It would be nice to write this using map():
12251225
let mut edges = vec::with_capacity(num_edges);
1226-
for self.constraints.each |&(constraint, span)| {
1226+
for self.constraints.each |constraint, span| {
12271227
edges.push(GraphEdge {
12281228
next_edge: [uint::max_value, uint::max_value],
12291229
constraint: *constraint,

src/librustc/rustc.rc

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Available lint options:
177177
padded(max_key, ~"name"), ~"default", ~"meaning"));
178178
io::println(fmt!(" %s %7.7s %s\n",
179179
padded(max_key, ~"----"), ~"-------", ~"-------"));
180-
for lint_dict.each |&(k, v)| {
180+
for lint_dict.each |k, v| {
181181
let k = str::replace(*k, ~"_", ~"-");
182182
io::println(fmt!(" %s %7.7s %s",
183183
padded(max_key, k),

0 commit comments

Comments
 (0)