Skip to content

Commit 88962ee

Browse files
committed
core: Add &self to core::iter methods
1 parent 3fc74df commit 88962ee

File tree

5 files changed

+83
-70
lines changed

5 files changed

+83
-70
lines changed

src/libcore/int-template.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ impl T: iter::Times {
9898
will execute the given function exactly x times. If we assume that \
9999
`x` is an int, this is functionally equivalent to \
100100
`for int::range(0, x) |_i| { /* anything */ }`."]
101-
pure fn times(it: fn() -> bool) {
102-
if self < 0 {
101+
pure fn times(&self, it: fn() -> bool) {
102+
if *self < 0 {
103103
fail fmt!("The .times method expects a nonnegative number, \
104104
but found %?", self);
105105
}
106-
let mut i = self;
106+
let mut i = *self;
107107
while i > 0 {
108108
if !it() { break }
109109
i -= 1;

src/libcore/iter-trait.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,53 @@ use cmp::{Eq, Ord};
2020
use self::inst::{IMPL_T, EACH, SIZE_HINT};
2121

2222
impl<A> IMPL_T<A>: iter::BaseIter<A> {
23-
pure fn each(blk: fn(v: &A) -> bool) { EACH(&self, blk) }
24-
pure fn size_hint() -> Option<uint> { SIZE_HINT(&self) }
23+
pure fn each(&self, blk: fn(v: &A) -> bool) { EACH(self, blk) }
24+
pure fn size_hint(&self) -> Option<uint> { SIZE_HINT(self) }
2525
}
2626

2727
impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
28-
pure fn eachi(blk: fn(uint, v: &A) -> bool) { iter::eachi(&self, blk) }
29-
pure fn all(blk: fn(&A) -> bool) -> bool { iter::all(&self, blk) }
30-
pure fn any(blk: fn(&A) -> bool) -> bool { iter::any(&self, blk) }
31-
pure fn foldl<B>(b0: B, blk: fn(&B, &A) -> B) -> B {
32-
iter::foldl(&self, move b0, blk)
28+
pure fn eachi(&self, blk: fn(uint, v: &A) -> bool) {
29+
iter::eachi(self, blk)
3330
}
34-
pure fn position(f: fn(&A) -> bool) -> Option<uint> {
35-
iter::position(&self, f)
31+
pure fn all(&self, blk: fn(&A) -> bool) -> bool {
32+
iter::all(self, blk)
33+
}
34+
pure fn any(&self, blk: fn(&A) -> bool) -> bool {
35+
iter::any(self, blk)
36+
}
37+
pure fn foldl<B>(&self, b0: B, blk: fn(&B, &A) -> B) -> B {
38+
iter::foldl(self, move b0, blk)
39+
}
40+
pure fn position(&self, f: fn(&A) -> bool) -> Option<uint> {
41+
iter::position(self, f)
3642
}
3743
}
3844

3945
impl<A: Eq> IMPL_T<A>: iter::EqIter<A> {
40-
pure fn contains(x: &A) -> bool { iter::contains(&self, x) }
41-
pure fn count(x: &A) -> uint { iter::count(&self, x) }
46+
pure fn contains(&self, x: &A) -> bool { iter::contains(self, x) }
47+
pure fn count(&self, x: &A) -> uint { iter::count(self, x) }
4248
}
4349

4450
impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {
45-
pure fn filter_to_vec(pred: fn(a: A) -> bool) -> ~[A] {
46-
iter::filter_to_vec(&self, pred)
51+
pure fn filter_to_vec(&self, pred: fn(a: A) -> bool) -> ~[A] {
52+
iter::filter_to_vec(self, pred)
4753
}
48-
pure fn map_to_vec<B>(op: fn(v: A) -> B) -> ~[B] {
49-
iter::map_to_vec(&self, op)
54+
pure fn map_to_vec<B>(&self, op: fn(v: A) -> B) -> ~[B] {
55+
iter::map_to_vec(self, op)
5056
}
51-
pure fn to_vec() -> ~[A] { iter::to_vec(&self) }
57+
pure fn to_vec(&self) -> ~[A] { iter::to_vec(self) }
5258

53-
pure fn flat_map_to_vec<B:Copy,IB:BaseIter<B>>(op: fn(a: A) -> IB)
59+
pure fn flat_map_to_vec<B:Copy,IB:BaseIter<B>>(&self, op: fn(a: A) -> IB)
5460
-> ~[B] {
55-
iter::flat_map_to_vec(&self, op)
61+
iter::flat_map_to_vec(self, op)
62+
}
63+
pure fn find(&self, f: fn(A) -> bool) -> Option<A> {
64+
iter::find(self, f)
5665
}
57-
58-
pure fn find(p: fn(a: A) -> bool) -> Option<A> { iter::find(&self, p) }
5966
}
6067

6168
impl<A: Copy Ord> IMPL_T<A>: iter::CopyableOrderedIter<A> {
62-
pure fn min() -> A { iter::min(&self) }
63-
pure fn max() -> A { iter::max(&self) }
69+
pure fn min(&self) -> A { iter::min(self) }
70+
pure fn max(&self) -> A { iter::max(self) }
6471
}
6572

src/libcore/iter.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,39 @@ use cmp::{Eq, Ord};
2323
pub type InitOp<T> = &fn(uint) -> T;
2424

2525
pub trait BaseIter<A> {
26-
pure fn each(blk: fn(v: &A) -> bool);
27-
pure fn size_hint() -> Option<uint>;
26+
pure fn each(&self, blk: fn(v: &A) -> bool);
27+
pure fn size_hint(&self) -> Option<uint>;
2828
}
2929

3030
pub trait ExtendedIter<A> {
31-
pure fn eachi(blk: fn(uint, v: &A) -> bool);
32-
pure fn all(blk: fn(&A) -> bool) -> bool;
33-
pure fn any(blk: fn(&A) -> bool) -> bool;
34-
pure fn foldl<B>(b0: B, blk: fn(&B, &A) -> B) -> B;
35-
pure fn position(f: fn(&A) -> bool) -> Option<uint>;
31+
pure fn eachi(&self, blk: fn(uint, v: &A) -> bool);
32+
pure fn all(&self, blk: fn(&A) -> bool) -> bool;
33+
pure fn any(&self, blk: fn(&A) -> bool) -> bool;
34+
pure fn foldl<B>(&self, b0: B, blk: fn(&B, &A) -> B) -> B;
35+
pure fn position(&self, f: fn(&A) -> bool) -> Option<uint>;
3636
}
3737

3838
pub trait EqIter<A:Eq> {
39-
pure fn contains(x: &A) -> bool;
40-
pure fn count(x: &A) -> uint;
39+
pure fn contains(&self, x: &A) -> bool;
40+
pure fn count(&self, x: &A) -> uint;
4141
}
4242

4343
pub trait Times {
44-
pure fn times(it: fn() -> bool);
44+
pure fn times(&self, it: fn() -> bool);
4545
}
4646

4747
pub trait CopyableIter<A:Copy> {
48-
pure fn filter_to_vec(pred: fn(a: A) -> bool) -> ~[A];
49-
pure fn map_to_vec<B>(op: fn(v: A) -> B) -> ~[B];
50-
pure fn flat_map_to_vec<B:Copy,IB: BaseIter<B>>(op: fn(A) -> IB) -> ~[B];
51-
pure fn to_vec() -> ~[A];
52-
pure fn find(p: fn(a: A) -> bool) -> Option<A>;
48+
pure fn filter_to_vec(&self, pred: fn(a: A) -> bool) -> ~[A];
49+
pure fn map_to_vec<B>(&self, op: fn(v: A) -> B) -> ~[B];
50+
pure fn flat_map_to_vec<B:Copy,IB: BaseIter<B>>(&self, op: fn(A) -> IB)
51+
-> ~[B];
52+
pure fn to_vec(&self) -> ~[A];
53+
pure fn find(&self, p: fn(A) -> bool) -> Option<A>;
5354
}
5455

5556
pub trait CopyableOrderedIter<A:Copy Ord> {
56-
pure fn min() -> A;
57-
pure fn max() -> A;
57+
pure fn min(&self) -> A;
58+
pure fn max(&self) -> A;
5859
}
5960

6061
pub trait CopyableNonstrictIter<A:Copy> {
@@ -222,9 +223,9 @@ pub pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
222223
}
223224

224225
pub pure fn find<A: Copy,IA:BaseIter<A>>(self: &IA,
225-
p: fn(a: A) -> bool) -> Option<A> {
226+
f: fn(A) -> bool) -> Option<A> {
226227
for self.each |i| {
227-
if p(*i) { return Some(*i) }
228+
if f(*i) { return Some(*i) }
228229
}
229230
return None;
230231
}

src/libcore/uint-template.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ impl T: iter::Times {
9292
will execute the given function exactly x times. If we assume that \
9393
`x` is an int, this is functionally equivalent to \
9494
`for int::range(0, x) |_i| { /* anything */ }`."]
95-
pure fn times(it: fn() -> bool) {
96-
let mut i = self;
95+
pure fn times(&self, it: fn() -> bool) {
96+
let mut i = *self;
9797
while i > 0 {
9898
if !it() { break }
9999
i -= 1;

src/libcore/vec.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,57 +2013,62 @@ pub mod bytes {
20132013
// required in the slice.
20142014

20152015
impl<A> &[A]: iter::BaseIter<A> {
2016-
pub pure fn each(blk: fn(v: &A) -> bool) {
2016+
pub pure fn each(&self, blk: fn(v: &A) -> bool) {
20172017
// FIXME(#2263)---should be able to call each(self, blk)
2018-
for each(self) |e| {
2018+
for each(*self) |e| {
20192019
if (!blk(e)) {
20202020
return;
20212021
}
20222022
}
20232023
}
2024-
pure fn size_hint() -> Option<uint> { Some(len(self)) }
2024+
pure fn size_hint(&self) -> Option<uint> { Some(len(*self)) }
20252025
}
20262026

20272027
impl<A> &[A]: iter::ExtendedIter<A> {
2028-
pub pure fn eachi(blk: fn(uint, v: &A) -> bool) {
2029-
iter::eachi(&self, blk)
2028+
pub pure fn eachi(&self, blk: fn(uint, v: &A) -> bool) {
2029+
iter::eachi(self, blk)
20302030
}
2031-
pub pure fn all(blk: fn(&A) -> bool) -> bool { iter::all(&self, blk) }
2032-
pub pure fn any(blk: fn(&A) -> bool) -> bool { iter::any(&self, blk) }
2033-
pub pure fn foldl<B>(b0: B, blk: fn(&B, &A) -> B) -> B {
2034-
iter::foldl(&self, b0, blk)
2031+
pub pure fn all(&self, blk: fn(&A) -> bool) -> bool {
2032+
iter::all(self, blk)
20352033
}
2036-
pub pure fn position(f: fn(&A) -> bool) -> Option<uint> {
2037-
iter::position(&self, f)
2034+
pub pure fn any(&self, blk: fn(&A) -> bool) -> bool {
2035+
iter::any(self, blk)
2036+
}
2037+
pub pure fn foldl<B>(&self, b0: B, blk: fn(&B, &A) -> B) -> B {
2038+
iter::foldl(self, b0, blk)
2039+
}
2040+
pub pure fn position(&self, f: fn(&A) -> bool) -> Option<uint> {
2041+
iter::position(self, f)
20382042
}
20392043
}
20402044

20412045
impl<A: Eq> &[A]: iter::EqIter<A> {
2042-
pub pure fn contains(x: &A) -> bool { iter::contains(&self, x) }
2043-
pub pure fn count(x: &A) -> uint { iter::count(&self, x) }
2046+
pub pure fn contains(&self, x: &A) -> bool { iter::contains(self, x) }
2047+
pub pure fn count(&self, x: &A) -> uint { iter::count(self, x) }
20442048
}
20452049

20462050
impl<A: Copy> &[A]: iter::CopyableIter<A> {
2047-
pure fn filter_to_vec(pred: fn(a: A) -> bool) -> ~[A] {
2048-
iter::filter_to_vec(&self, pred)
2051+
pure fn filter_to_vec(&self, pred: fn(a: A) -> bool) -> ~[A] {
2052+
iter::filter_to_vec(self, pred)
20492053
}
2050-
pure fn map_to_vec<B>(op: fn(v: A) -> B) -> ~[B] {
2051-
iter::map_to_vec(&self, op)
2054+
pure fn map_to_vec<B>(&self, op: fn(v: A) -> B) -> ~[B] {
2055+
iter::map_to_vec(self, op)
20522056
}
2053-
pure fn to_vec() -> ~[A] { iter::to_vec(&self) }
2057+
pure fn to_vec(&self) -> ~[A] { iter::to_vec(self) }
20542058

2055-
pure fn flat_map_to_vec<B:Copy,IB:BaseIter<B>>(op: fn(A) -> IB) -> ~[B] {
2056-
iter::flat_map_to_vec(&self, op)
2059+
pure fn flat_map_to_vec<B:Copy,IB:BaseIter<B>>(&self, op: fn(A) -> IB)
2060+
-> ~[B] {
2061+
iter::flat_map_to_vec(self, op)
20572062
}
20582063

2059-
pub pure fn find(p: fn(a: A) -> bool) -> Option<A> {
2060-
iter::find(&self, p)
2064+
pub pure fn find(&self, f: fn(A) -> bool) -> Option<A> {
2065+
iter::find(self, f)
20612066
}
20622067
}
20632068

20642069
impl<A: Copy Ord> &[A]: iter::CopyableOrderedIter<A> {
2065-
pure fn min() -> A { iter::min(&self) }
2066-
pure fn max() -> A { iter::max(&self) }
2070+
pure fn min(&self) -> A { iter::min(self) }
2071+
pure fn max(&self) -> A { iter::max(self) }
20672072
}
20682073

20692074
impl<A:Copy> &[A] : iter::CopyableNonstrictIter<A> {

0 commit comments

Comments
 (0)