From c64a5d2d37a91e2151da41324a7f5dfc2b9c05d3 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Wed, 13 Mar 2013 20:55:42 -0400 Subject: [PATCH] add a trait for mutable iterators --- src/libcore/iter.rs | 4 ++++ src/libcore/prelude.rs | 5 +++-- src/libcore/vec.rs | 31 ++++++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 8931b4088263e..e461cb2c65f18 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -31,6 +31,10 @@ pub trait ReverseIter: BaseIter { pure fn each_reverse(&self, blk: &fn(&A) -> bool); } +pub trait MutableIter: BaseIter { + fn each_mut(&mut self, blk: &fn(&mut A) -> bool); +} + pub trait ExtendedIter { pure fn eachi(&self, blk: &fn(uint, v: &A) -> bool); pure fn all(&self, blk: &fn(&A) -> bool) -> bool; diff --git a/src/libcore/prelude.rs b/src/libcore/prelude.rs index c5b1b9eb87593..9b1148c9586d1 100644 --- a/src/libcore/prelude.rs +++ b/src/libcore/prelude.rs @@ -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; diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index 68dca608a480d..b513f91cde9ab 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -1358,7 +1358,7 @@ pub pure fn each(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(v: &mut [T], f: &fn(elem: &mut T) -> bool) { +pub fn each_mut(v: &'r mut [T], f: &fn(elem: &'r mut T) -> bool) { let mut i = 0; let n = v.len(); while i < n { @@ -2282,11 +2282,9 @@ pub mod bytes { // ___________________________________________________________________________ // ITERATION TRAIT METHODS -impl iter::BaseIter for &self/[A] { +impl iter::BaseIter 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 { Some(self.len()) } } @@ -2307,6 +2305,29 @@ impl iter::BaseIter for @[A] { pure fn size_hint(&self) -> Option { Some(self.len()) } } +impl iter::MutableIter 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 iter::MutableIter 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 iter::MutableIter for @mut [A] { + #[inline(always)] + fn each_mut(&mut self, blk: &fn(v: &mut A) -> bool) { + each_mut(*self, blk) + } +} + impl iter::ExtendedIter for &self/[A] { pub pure fn eachi(&self, blk: &fn(uint, v: &A) -> bool) { iter::eachi(self, blk)