Skip to content

Commit 669808e

Browse files
committed
---
yaml --- r: 65047 b: refs/heads/master c: d953a5c h: refs/heads/master i: 65045: b5579c2 65043: 3950e22 65039: 5cb55be v: v3
1 parent 5bd797c commit 669808e

27 files changed

+389
-334
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: fc656262a905a50eff2be54a87295d2952d29106
2+
refs/heads/master: d953a5ce43ba2adecb50780c7debd3fea6248996
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 18e3db7392d2d0697b7e27d6d986139960144d85
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9

trunk/doc/tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1977,7 +1977,7 @@ struct TimeBomb {
19771977
19781978
impl Drop for TimeBomb {
19791979
fn finalize(&self) {
1980-
for old_iter::repeat(self.explosivity) {
1980+
for self.explosivity.times {
19811981
println("blam!");
19821982
}
19831983
}

trunk/src/libcore/bool.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,10 @@ pub fn is_false(v: bool) -> bool { !v }
4949
/// Parse logic value from `s`
5050
impl FromStr for bool {
5151
fn from_str(s: &str) -> Option<bool> {
52-
if s == "true" {
53-
Some(true)
54-
} else if s == "false" {
55-
Some(false)
56-
} else {
57-
None
52+
match s {
53+
"true" => Some(true),
54+
"false" => Some(false),
55+
_ => None,
5856
}
5957
}
6058
}

trunk/src/libcore/cmp.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,11 @@ totalord_impl!(uint)
127127

128128
totalord_impl!(char)
129129

130+
/// Compares (a1, b1) against (a2, b2), where the a values are more significant.
130131
pub fn cmp2<A:TotalOrd,B:TotalOrd>(
131132
a1: &A, b1: &B,
132133
a2: &A, b2: &B) -> Ordering
133134
{
134-
//! Compares (a1, b1) against (a2, b2), where the a values are more significant.
135-
136135
match a1.cmp(a2) {
137136
Less => Less,
138137
Greater => Greater,

trunk/src/libcore/either.rs

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,22 @@ pub enum Either<T, U> {
2626
Right(U)
2727
}
2828

29+
/// Applies a function based on the given either value
30+
///
31+
/// If `value` is left(T) then `f_left` is applied to its contents, if
32+
/// `value` is right(U) then `f_right` is applied to its contents, and the
33+
/// result is returned.
2934
#[inline(always)]
3035
pub fn either<T, U, V>(f_left: &fn(&T) -> V,
3136
f_right: &fn(&U) -> V, value: &Either<T, U>) -> V {
32-
/*!
33-
* Applies a function based on the given either value
34-
*
35-
* If `value` is left(T) then `f_left` is applied to its contents, if
36-
* `value` is right(U) then `f_right` is applied to its contents, and the
37-
* result is returned.
38-
*/
39-
4037
match *value {
41-
Left(ref l) => f_left(l),
42-
Right(ref r) => f_right(r)
38+
Left(ref l) => f_left(l),
39+
Right(ref r) => f_right(r)
4340
}
4441
}
4542

43+
/// Extracts from a vector of either all the left values
4644
pub fn lefts<T:Copy,U>(eithers: &[Either<T, U>]) -> ~[T] {
47-
//! Extracts from a vector of either all the left values
48-
4945
do vec::build_sized(eithers.len()) |push| {
5046
for eithers.each |elt| {
5147
match *elt {
@@ -56,9 +52,8 @@ pub fn lefts<T:Copy,U>(eithers: &[Either<T, U>]) -> ~[T] {
5652
}
5753
}
5854

55+
/// Extracts from a vector of either all the right values
5956
pub fn rights<T, U: Copy>(eithers: &[Either<T, U>]) -> ~[U] {
60-
//! Extracts from a vector of either all the right values
61-
6257
do vec::build_sized(eithers.len()) |push| {
6358
for eithers.each |elt| {
6459
match *elt {
@@ -69,80 +64,73 @@ pub fn rights<T, U: Copy>(eithers: &[Either<T, U>]) -> ~[U] {
6964
}
7065
}
7166

72-
pub fn partition<T, U>(eithers: ~[Either<T, U>])
73-
-> (~[T], ~[U]) {
74-
/*!
75-
* Extracts from a vector of either all the left values and right values
76-
*
77-
* Returns a structure containing a vector of left values and a vector of
78-
* right values.
79-
*/
80-
67+
/// Extracts from a vector of either all the left values and right values
68+
///
69+
/// Returns a structure containing a vector of left values and a vector of
70+
/// right values.
71+
pub fn partition<T, U>(eithers: ~[Either<T, U>]) -> (~[T], ~[U]) {
8172
let mut lefts: ~[T] = ~[];
8273
let mut rights: ~[U] = ~[];
8374
do vec::consume(eithers) |_i, elt| {
8475
match elt {
85-
Left(l) => lefts.push(l),
86-
Right(r) => rights.push(r)
76+
Left(l) => lefts.push(l),
77+
Right(r) => rights.push(r)
8778
}
8879
}
8980
return (lefts, rights);
9081
}
9182

83+
/// Flips between left and right of a given either
9284
#[inline(always)]
9385
pub fn flip<T, U>(eith: Either<T, U>) -> Either<U, T> {
94-
//! Flips between left and right of a given either
95-
9686
match eith {
97-
Right(r) => Left(r),
98-
Left(l) => Right(l)
87+
Right(r) => Left(r),
88+
Left(l) => Right(l)
9989
}
10090
}
10191

92+
/// Converts either::t to a result::t
93+
///
94+
/// Converts an `either` type to a `result` type, making the "right" choice
95+
/// an ok result, and the "left" choice a fail
10296
#[inline(always)]
103-
pub fn to_result<T, U>(eith: Either<T, U>)
104-
-> Result<U, T> {
105-
/*!
106-
* Converts either::t to a result::t
107-
*
108-
* Converts an `either` type to a `result` type, making the "right" choice
109-
* an ok result, and the "left" choice a fail
110-
*/
111-
97+
pub fn to_result<T, U>(eith: Either<T, U>) -> Result<U, T> {
11298
match eith {
113-
Right(r) => result::Ok(r),
114-
Left(l) => result::Err(l)
99+
Right(r) => result::Ok(r),
100+
Left(l) => result::Err(l)
115101
}
116102
}
117103

104+
/// Checks whether the given value is a left
118105
#[inline(always)]
119106
pub fn is_left<T, U>(eith: &Either<T, U>) -> bool {
120-
//! Checks whether the given value is a left
121-
122-
match *eith { Left(_) => true, _ => false }
107+
match *eith {
108+
Left(_) => true,
109+
_ => false
110+
}
123111
}
124112

113+
/// Checks whether the given value is a right
125114
#[inline(always)]
126115
pub fn is_right<T, U>(eith: &Either<T, U>) -> bool {
127-
//! Checks whether the given value is a right
128-
129-
match *eith { Right(_) => true, _ => false }
116+
match *eith {
117+
Right(_) => true,
118+
_ => false
119+
}
130120
}
131121

122+
/// Retrieves the value in the left branch. Fails if the either is Right.
132123
#[inline(always)]
133124
pub fn unwrap_left<T,U>(eith: Either<T,U>) -> T {
134-
//! Retrieves the value in the left branch. Fails if the either is Right.
135-
136125
match eith {
137126
Left(x) => x,
138127
Right(_) => fail!("either::unwrap_left Right")
139128
}
140129
}
141130

131+
/// Retrieves the value in the right branch. Fails if the either is Left.
142132
#[inline(always)]
143133
pub fn unwrap_right<T,U>(eith: Either<T,U>) -> U {
144-
//! Retrieves the value in the right branch. Fails if the either is Left.
145-
146134
match eith {
147135
Right(x) => x,
148136
Left(_) => fail!("either::unwrap_right Left")

trunk/src/libcore/iterator.rs

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ pub trait IteratorUtil<A> {
4747
fn advance(&mut self, f: &fn(A) -> bool);
4848
#[cfg(not(stage0))]
4949
fn advance(&mut self, f: &fn(A) -> bool) -> bool;
50-
fn to_vec(&mut self) -> ~[A];
51-
fn nth(&mut self, n: uint) -> Option<A>;
52-
fn last(&mut self) -> Option<A>;
50+
fn to_vec(self) -> ~[A];
51+
fn nth(&mut self, n: uint) -> A;
52+
fn first(&mut self) -> A;
53+
fn last(&mut self) -> A;
5354
fn fold<B>(&mut self, start: B, f: &fn(B, A) -> B) -> B;
5455
fn count(&mut self) -> uint;
5556
fn all(&mut self, f: &fn(&A) -> bool) -> bool;
@@ -147,28 +148,46 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
147148
}
148149

149150
#[inline(always)]
150-
fn to_vec(&mut self) -> ~[A] {
151-
iter::to_vec::<A>(|f| self.advance(f))
151+
fn to_vec(self) -> ~[A] {
152+
let mut v = ~[];
153+
let mut it = self;
154+
for it.advance() |x| { v.push(x); }
155+
return v;
152156
}
153157

154-
/// Return the `n`th item yielded by an iterator.
158+
/// Get `n`th element of an iterator.
155159
#[inline(always)]
156-
fn nth(&mut self, mut n: uint) -> Option<A> {
160+
fn nth(&mut self, n: uint) -> A {
161+
let mut i = n;
157162
loop {
158163
match self.next() {
159-
Some(x) => if n == 0 { return Some(x) },
160-
None => return None
164+
Some(x) => { if i == 0 { return x; }}
165+
None => { fail!("cannot get %uth element", n) }
161166
}
162-
n -= 1;
167+
i -= 1;
163168
}
164169
}
165170

166-
/// Return the last item yielded by an iterator.
171+
// Get first elemet of an iterator.
167172
#[inline(always)]
168-
fn last(&mut self) -> Option<A> {
169-
let mut last = None;
170-
for self.advance |x| { last = Some(x); }
171-
last
173+
fn first(&mut self) -> A {
174+
match self.next() {
175+
Some(x) => x ,
176+
None => fail!("cannot get first element")
177+
}
178+
}
179+
180+
// Get last element of an iterator.
181+
//
182+
// If the iterator have an infinite length, this method won't return.
183+
#[inline(always)]
184+
fn last(&mut self) -> A {
185+
let mut elm = match self.next() {
186+
Some(x) => x,
187+
None => fail!("cannot get last element")
188+
};
189+
for self.advance |e| { elm = e; }
190+
return elm;
172191
}
173192

174193
/// Reduce an iterator to an accumulated value
@@ -184,7 +203,7 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
184203
return accum;
185204
}
186205

187-
/// Count the number of items yielded by an iterator
206+
/// Count the number of an iterator elemenrs
188207
#[inline(always)]
189208
fn count(&mut self) -> uint { self.fold(0, |cnt, _x| cnt + 1) }
190209

@@ -325,13 +344,17 @@ pub struct FilterMapIterator<'self, A, B, T> {
325344
impl<'self, A, B, T: Iterator<A>> Iterator<B> for FilterMapIterator<'self, A, B, T> {
326345
#[inline]
327346
fn next(&mut self) -> Option<B> {
328-
for self.iter.advance |x| {
329-
match (self.f)(x) {
330-
Some(y) => return Some(y),
331-
None => ()
347+
loop {
348+
match self.iter.next() {
349+
None => { return None; }
350+
Some(a) => {
351+
match (self.f)(a) {
352+
Some(b) => { return Some(b); }
353+
None => { loop; }
354+
}
355+
}
332356
}
333357
}
334-
None
335358
}
336359
}
337360

@@ -556,7 +579,7 @@ mod tests {
556579

557580
#[test]
558581
fn test_filter_map() {
559-
let mut it = Counter::new(0u, 1u).take(10)
582+
let it = Counter::new(0u, 1u).take(10)
560583
.filter_map(|x: uint| if x.is_even() { Some(x*x) } else { None });
561584
assert_eq!(it.to_vec(), ~[0*0, 2*2, 4*4, 6*6, 8*8]);
562585
}
@@ -666,15 +689,43 @@ mod tests {
666689
fn test_iterator_nth() {
667690
let v = &[0, 1, 2, 3, 4];
668691
for uint::range(0, v.len()) |i| {
669-
assert_eq!(v.iter().nth(i).unwrap(), &v[i]);
692+
assert_eq!(v.iter().nth(i), &v[i]);
670693
}
671694
}
672695

696+
#[test]
697+
#[should_fail]
698+
fn test_iterator_nth_fail() {
699+
let v = &[0, 1, 2, 3, 4];
700+
v.iter().nth(5);
701+
}
702+
703+
#[test]
704+
fn test_iterator_first() {
705+
let v = &[0, 1, 2, 3, 4];
706+
assert_eq!(v.iter().first(), &0);
707+
assert_eq!(v.slice(2, 5).iter().first(), &2);
708+
}
709+
710+
#[test]
711+
#[should_fail]
712+
fn test_iterator_first_fail() {
713+
let v: &[uint] = &[];
714+
v.iter().first();
715+
}
716+
673717
#[test]
674718
fn test_iterator_last() {
675719
let v = &[0, 1, 2, 3, 4];
676-
assert_eq!(v.iter().last().unwrap(), &4);
677-
assert_eq!(v.slice(0, 1).iter().last().unwrap(), &0);
720+
assert_eq!(v.iter().last(), &4);
721+
assert_eq!(v.slice(0, 1).iter().last(), &0);
722+
}
723+
724+
#[test]
725+
#[should_fail]
726+
fn test_iterator_last_fail() {
727+
let v: &[uint] = &[];
728+
v.iter().last();
678729
}
679730

680731
#[test]

trunk/src/libcore/managed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ pub mod raw {
3535

3636
}
3737

38+
/// Determine if two shared boxes point to the same object
3839
#[inline(always)]
3940
pub fn ptr_eq<T>(a: @T, b: @T) -> bool {
40-
//! Determine if two shared boxes point to the same object
4141
let a_ptr: *T = to_unsafe_ptr(&*a), b_ptr: *T = to_unsafe_ptr(&*b);
4242
a_ptr == b_ptr
4343
}
4444

45+
/// Determine if two mutable shared boxes point to the same object
4546
#[inline(always)]
4647
pub fn mut_ptr_eq<T>(a: @mut T, b: @mut T) -> bool {
47-
//! Determine if two mutable shared boxes point to the same object
4848
let a_ptr: *T = to_unsafe_ptr(&*a), b_ptr: *T = to_unsafe_ptr(&*b);
4949
a_ptr == b_ptr
5050
}

0 commit comments

Comments
 (0)