Skip to content

Commit 82f963e

Browse files
committed
libcore: Change each_val to follow new for-loop protocol
1 parent 8ca64a4 commit 82f963e

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/libcore/old_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub trait CopyableNonstrictIter<A:Copy> {
9393
// Like "each", but copies out the value. If the receiver is mutated while
9494
// iterating over it, the semantics must not be memory-unsafe but are
9595
// otherwise undefined.
96-
fn each_val(&const self, f: &fn(A) -> bool);
96+
fn each_val(&const self, f: &fn(A) -> bool) -> bool;
9797
}
9898

9999
// A trait for sequences that can be built by imperatively pushing elements

src/libcore/vec.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,34 +2945,37 @@ impl<A:Copy + Ord> old_iter::CopyableOrderedIter<A> for @[A] {
29452945
}
29462946

29472947
impl<'self,A:Copy> old_iter::CopyableNonstrictIter<A> for &'self [A] {
2948-
fn each_val(&const self, f: &fn(A) -> bool) {
2948+
fn each_val(&const self, f: &fn(A) -> bool) -> bool {
29492949
let mut i = 0;
29502950
while i < self.len() {
2951-
if !f(copy self[i]) { break; }
2951+
if !f(copy self[i]) { return false; }
29522952
i += 1;
29532953
}
2954+
return true;
29542955
}
29552956
}
29562957

29572958
// FIXME(#4148): This should be redundant
29582959
impl<A:Copy> old_iter::CopyableNonstrictIter<A> for ~[A] {
2959-
fn each_val(&const self, f: &fn(A) -> bool) {
2960+
fn each_val(&const self, f: &fn(A) -> bool) -> bool {
29602961
let mut i = 0;
29612962
while i < uniq_len(self) {
2962-
if !f(copy self[i]) { break; }
2963+
if !f(copy self[i]) { return false; }
29632964
i += 1;
29642965
}
2966+
return true;
29652967
}
29662968
}
29672969

29682970
// FIXME(#4148): This should be redundant
29692971
impl<A:Copy> old_iter::CopyableNonstrictIter<A> for @[A] {
2970-
fn each_val(&const self, f: &fn(A) -> bool) {
2972+
fn each_val(&const self, f: &fn(A) -> bool) -> bool {
29712973
let mut i = 0;
29722974
while i < self.len() {
2973-
if !f(copy self[i]) { break; }
2975+
if !f(copy self[i]) { return false; }
29742976
i += 1;
29752977
}
2978+
return true;
29762979
}
29772980
}
29782981

@@ -4688,4 +4691,14 @@ mod tests {
46884691
i += 1;
46894692
}
46904693
}
4694+
4695+
#[test]
4696+
fn test_each_val() {
4697+
use old_iter::CopyableNonstrictIter;
4698+
let mut i = 0;
4699+
for [1, 2, 3].each_val |v| {
4700+
i += v;
4701+
}
4702+
assert!(i == 6);
4703+
}
46914704
}

0 commit comments

Comments
 (0)