Skip to content

libcore: Change each_val to follow new for-loop protocol #6426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libcore/old_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub trait CopyableNonstrictIter<A:Copy> {
// 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
Expand Down
25 changes: 19 additions & 6 deletions src/libcore/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2945,34 +2945,37 @@ impl<A:Copy + Ord> old_iter::CopyableOrderedIter<A> for @[A] {
}

impl<'self,A:Copy> old_iter::CopyableNonstrictIter<A> 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<A:Copy> old_iter::CopyableNonstrictIter<A> 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<A:Copy> old_iter::CopyableNonstrictIter<A> 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;
}
}

Expand Down Expand Up @@ -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);
}
}