Skip to content

add a trait for mutable iterators #5369

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

Merged
merged 1 commit into from
Mar 15, 2013
Merged
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
4 changes: 4 additions & 0 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ pub trait ReverseIter<A>: BaseIter<A> {
pure fn each_reverse(&self, blk: &fn(&A) -> bool);
}

pub trait MutableIter<A>: BaseIter<A> {
fn each_mut(&mut self, blk: &fn(&mut A) -> bool);
}

pub trait ExtendedIter<A> {
pure fn eachi(&self, blk: &fn(uint, v: &A) -> bool);
pure fn all(&self, blk: &fn(&A) -> bool) -> bool;
Expand Down
5 changes: 3 additions & 2 deletions src/libcore/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ pub use clone::Clone;
pub use cmp::{Eq, Ord, TotalOrd, Ordering, Less, Equal, Greater};
pub use container::{Container, Mutable, Map, Set};
pub use hash::Hash;
pub use iter::{BaseIter, ReverseIter, ExtendedIter, EqIter, CopyableIter};
pub use iter::{CopyableOrderedIter, CopyableNonstrictIter, Times};
pub use iter::{BaseIter, ReverseIter, MutableIter, ExtendedIter, EqIter};
pub use iter::{CopyableIter, CopyableOrderedIter, CopyableNonstrictIter};
pub use iter::Times;
pub use num::NumCast;
pub use path::GenericPath;
pub use path::Path;
Expand Down
31 changes: 26 additions & 5 deletions src/libcore/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ pub pure fn each<T>(v: &r/[T], f: &fn(&r/T) -> bool) {
/// a vector with mutable contents and you would like
/// to mutate the contents as you iterate.
#[inline(always)]
pub fn each_mut<T>(v: &mut [T], f: &fn(elem: &mut T) -> bool) {
pub fn each_mut<T>(v: &'r mut [T], f: &fn(elem: &'r mut T) -> bool) {
let mut i = 0;
let n = v.len();
while i < n {
Expand Down Expand Up @@ -2282,11 +2282,9 @@ pub mod bytes {
// ___________________________________________________________________________
// ITERATION TRAIT METHODS

impl<A> iter::BaseIter<A> for &self/[A] {
impl<A> iter::BaseIter<A> for &'self [A] {
#[inline(always)]
pub pure fn each(&self, blk: &fn(v: &'self A) -> bool) {
each(*self, blk)
}
pure fn each(&self, blk: &fn(v: &'self A) -> bool) { each(*self, blk) }
#[inline(always)]
pure fn size_hint(&self) -> Option<uint> { Some(self.len()) }
}
Expand All @@ -2307,6 +2305,29 @@ impl<A> iter::BaseIter<A> for @[A] {
pure fn size_hint(&self) -> Option<uint> { Some(self.len()) }
}

impl<A> iter::MutableIter<A> for &'self mut [A] {
#[inline(always)]
fn each_mut(&mut self, blk: &fn(v: &'self mut A) -> bool) {
each_mut(*self, blk)
}
}

// FIXME(#4148): This should be redundant
impl<A> iter::MutableIter<A> for ~[A] {
#[inline(always)]
fn each_mut(&mut self, blk: &fn(v: &'self mut A) -> bool) {
each_mut(*self, blk)
}
}

// FIXME(#4148): This should be redundant
impl<A> iter::MutableIter<A> for @mut [A] {
#[inline(always)]
fn each_mut(&mut self, blk: &fn(v: &mut A) -> bool) {
each_mut(*self, blk)
}
}

impl<A> iter::ExtendedIter<A> for &self/[A] {
pub pure fn eachi(&self, blk: &fn(uint, v: &A) -> bool) {
iter::eachi(self, blk)
Expand Down