From 82f963e347fa16ac3824e81c53f5d15155fe319b Mon Sep 17 00:00:00 2001 From: gifnksm Date: Sun, 12 May 2013 07:56:08 +0900 Subject: [PATCH] libcore: Change `each_val` to follow new for-loop protocol --- src/libcore/old_iter.rs | 2 +- src/libcore/vec.rs | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/libcore/old_iter.rs b/src/libcore/old_iter.rs index 8e31bbfd878..a596b07dc78 100644 --- a/src/libcore/old_iter.rs +++ b/src/libcore/old_iter.rs @@ -93,7 +93,7 @@ pub trait CopyableNonstrictIter { // Like "each", but copies out the value. If the receiver is mutated while // iterating over it, the semantics must not be memory-unsafe but are // otherwise undefined. - fn each_val(&const self, f: &fn(A) -> bool); + fn each_val(&const self, f: &fn(A) -> bool) -> bool; } // A trait for sequences that can be built by imperatively pushing elements diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index 741644af4aa..604f0297b64 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -2945,34 +2945,37 @@ impl old_iter::CopyableOrderedIter for @[A] { } impl<'self,A:Copy> old_iter::CopyableNonstrictIter for &'self [A] { - fn each_val(&const self, f: &fn(A) -> bool) { + fn each_val(&const self, f: &fn(A) -> bool) -> bool { let mut i = 0; while i < self.len() { - if !f(copy self[i]) { break; } + if !f(copy self[i]) { return false; } i += 1; } + return true; } } // FIXME(#4148): This should be redundant impl old_iter::CopyableNonstrictIter for ~[A] { - fn each_val(&const self, f: &fn(A) -> bool) { + fn each_val(&const self, f: &fn(A) -> bool) -> bool { let mut i = 0; while i < uniq_len(self) { - if !f(copy self[i]) { break; } + if !f(copy self[i]) { return false; } i += 1; } + return true; } } // FIXME(#4148): This should be redundant impl old_iter::CopyableNonstrictIter for @[A] { - fn each_val(&const self, f: &fn(A) -> bool) { + fn each_val(&const self, f: &fn(A) -> bool) -> bool { let mut i = 0; while i < self.len() { - if !f(copy self[i]) { break; } + if !f(copy self[i]) { return false; } i += 1; } + return true; } } @@ -4688,4 +4691,14 @@ mod tests { i += 1; } } + + #[test] + fn test_each_val() { + use old_iter::CopyableNonstrictIter; + let mut i = 0; + for [1, 2, 3].each_val |v| { + i += v; + } + assert!(i == 6); + } }