Skip to content

Commit f132401

Browse files
committed
auto merge of #7982 : thestinger/rust/iterator, r=thestinger
f0f4dcc r=huonw 25e9c4c r=graydon a87c2d1 r=brson 16f369d r=cmr 9f05cc8 r=bstrie e858055 r=huonw 5d80938 r=thestinger 05d03e7 r=cmr 8f86fa3 r=thestinger
2 parents 5102853 + 8f86fa3 commit f132401

File tree

104 files changed

+589
-372
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+589
-372
lines changed

doc/tutorial-container.md

+50-6
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,15 @@ let mut it = xs.iter().zip(ys.iter());
192192

193193
// print out the pairs of elements up to (&3, &"baz")
194194
for it.advance |(x, y)| {
195-
println(fmt!("%d %s", *x, *y));
195+
printfln!("%d %s", *x, *y);
196196

197197
if *x == 3 {
198198
break;
199199
}
200200
}
201201

202202
// yield and print the last pair from the iterator
203-
println(fmt!("last: %?", it.next()));
203+
printfln!("last: %?", it.next());
204204

205205
// the iterator is now fully consumed
206206
assert!(it.next().is_none());
@@ -294,15 +294,59 @@ another `DoubleEndedIterator` with `next` and `next_back` exchanged.
294294
~~~
295295
let xs = [1, 2, 3, 4, 5, 6];
296296
let mut it = xs.iter();
297-
println(fmt!("%?", it.next())); // prints `Some(&1)`
298-
println(fmt!("%?", it.next())); // prints `Some(&2)`
299-
println(fmt!("%?", it.next_back())); // prints `Some(&6)`
297+
printfln!("%?", it.next()); // prints `Some(&1)`
298+
printfln!("%?", it.next()); // prints `Some(&2)`
299+
printfln!("%?", it.next_back()); // prints `Some(&6)`
300300

301301
// prints `5`, `4` and `3`
302302
for it.invert().advance |&x| {
303-
println(fmt!("%?", x))
303+
printfln!("%?", x)
304304
}
305305
~~~
306306
307307
The `rev_iter` and `mut_rev_iter` methods on vectors just return an inverted
308308
version of the standard immutable and mutable vector iterators.
309+
310+
The `chain_`, `transform`, `filter`, `filter_map` and `peek` adaptors are
311+
`DoubleEndedIterator` implementations if the underlying iterators are.
312+
313+
~~~
314+
let xs = [1, 2, 3, 4];
315+
let ys = [5, 6, 7, 8];
316+
let mut it = xs.iter().chain_(ys.iter()).transform(|&x| x * 2);
317+
318+
printfln!("%?", it.next()); // prints `Some(2)`
319+
320+
// prints `16`, `14`, `12`, `10`, `8`, `6`, `4`
321+
for it.invert().advance |x| {
322+
printfln!("%?", x);
323+
}
324+
~~~
325+
326+
## Random-access iterators
327+
328+
The `RandomAccessIterator` trait represents an iterator offering random access
329+
to the whole range. The `indexable` method retrieves the number of elements
330+
accessible with the `idx` method.
331+
332+
The `chain_` adaptor is an implementation of `RandomAccessIterator` if the
333+
underlying iterators are.
334+
335+
~~~
336+
let xs = [1, 2, 3, 4, 5];
337+
let ys = ~[7, 9, 11];
338+
let mut it = xs.iter().chain_(ys.iter());
339+
printfln!("%?", it.idx(0)); // prints `Some(&1)`
340+
printfln!("%?", it.idx(5)); // prints `Some(&7)`
341+
printfln!("%?", it.idx(7)); // prints `Some(&11)`
342+
printfln!("%?", it.idx(8)); // prints `None`
343+
344+
// yield two elements from the beginning, and one from the end
345+
it.next();
346+
it.next();
347+
it.next_back();
348+
349+
printfln!("%?", it.idx(0)); // prints `Some(&3)`
350+
printfln!("%?", it.idx(4)); // prints `Some(&9)`
351+
printfln!("%?", it.idx(6)); // prints `None`
352+
~~~

src/compiletest/runtest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
292292
}
293293
}
294294
if i != num_check_lines {
295-
fatal_ProcRes(fmt!("line not found in debugger output: %s"
295+
fatal_ProcRes(fmt!("line not found in debugger output: %s",
296296
check_lines[i]), &ProcRes);
297297
}
298298
}

src/libextra/base64.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'self> ToBase64 for &'self [u8] {
7575
*
7676
* fn main () {
7777
* let str = [52,32].to_base64(standard);
78-
* println(fmt!("%s", str));
78+
* printfln!("%s", str);
7979
* }
8080
* ~~~
8181
*/
@@ -164,7 +164,7 @@ impl<'self> ToBase64 for &'self str {
164164
*
165165
* fn main () {
166166
* let str = "Hello, World".to_base64(standard);
167-
* println(fmt!("%s",str));
167+
* printfln!("%s", str);
168168
* }
169169
* ~~~
170170
*
@@ -194,9 +194,9 @@ impl<'self> FromBase64 for &'self [u8] {
194194
*
195195
* fn main () {
196196
* let str = [52,32].to_base64(standard);
197-
* println(fmt!("%s", str));
197+
* printfln!("%s", str);
198198
* let bytes = str.from_base64();
199-
* println(fmt!("%?",bytes));
199+
* printfln!("%?", bytes);
200200
* }
201201
* ~~~
202202
*/
@@ -271,11 +271,11 @@ impl<'self> FromBase64 for &'self str {
271271
*
272272
* fn main () {
273273
* let hello_str = "Hello, World".to_base64(standard);
274-
* println(fmt!("%s",hello_str));
274+
* printfln!("%s", hello_str);
275275
* let bytes = hello_str.from_base64();
276-
* println(fmt!("%?",bytes));
276+
* printfln!("%?", bytes);
277277
* let result_str = str::from_bytes(bytes);
278-
* println(fmt!("%s",result_str));
278+
* printfln!("%s", result_str);
279279
* }
280280
* ~~~
281281
*/

src/libextra/future.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* # fn make_a_sandwich() {};
2020
* let mut delayed_fib = extra::future::spawn (|| fib(5000) );
2121
* make_a_sandwich();
22-
* println(fmt!("fib(5000) = %?", delayed_fib.get()))
22+
* printfln!("fib(5000) = %?", delayed_fib.get())
2323
* ~~~
2424
*/
2525

src/libextra/getopts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* }
4545
*
4646
* fn print_usage(program: &str, _opts: &[Opt]) {
47-
* println(fmt!("Usage: %s [options]", program));
47+
* printfln!("Usage: %s [options]", program);
4848
* println("-o\t\tOutput");
4949
* println("-h --help\tUsage");
5050
* }

src/libextra/rc.rs

+43-47
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@ cycle cannot be created with `Rc<T>` because there is no way to modify it after
2323

2424

2525
use std::cast;
26-
use std::libc::{c_void, size_t, malloc, free};
2726
use std::ptr;
28-
use std::sys;
2927
use std::unstable::intrinsics;
3028

29+
// Convert ~T into *mut T without dropping it
30+
#[inline]
31+
unsafe fn owned_to_raw<T>(mut box: ~T) -> *mut T {
32+
let ptr = ptr::to_mut_unsafe_ptr(box);
33+
intrinsics::forget(box);
34+
ptr
35+
}
36+
3137
struct RcBox<T> {
3238
value: T,
3339
count: uint
@@ -42,21 +48,20 @@ pub struct Rc<T> {
4248

4349
impl<T> Rc<T> {
4450
unsafe fn new(value: T) -> Rc<T> {
45-
let ptr = malloc(sys::size_of::<RcBox<T>>() as size_t) as *mut RcBox<T>;
46-
assert!(!ptr::is_null(ptr));
47-
intrinsics::move_val_init(&mut *ptr, RcBox{value: value, count: 1});
48-
Rc{ptr: ptr}
51+
Rc{ptr: owned_to_raw(~RcBox{value: value, count: 1})}
4952
}
5053
}
5154

52-
// FIXME: #6516: should be a static method
53-
pub fn rc_from_owned<T: Send>(value: T) -> Rc<T> {
54-
unsafe { Rc::new(value) }
55+
impl<T: Send> Rc<T> {
56+
pub fn from_owned(value: T) -> Rc<T> {
57+
unsafe { Rc::new(value) }
58+
}
5559
}
5660

57-
// FIXME: #6516: should be a static method
58-
pub fn rc_from_const<T: Freeze>(value: T) -> Rc<T> {
59-
unsafe { Rc::new(value) }
61+
impl<T: Freeze> Rc<T> {
62+
pub fn from_const(value: T) -> Rc<T> {
63+
unsafe { Rc::new(value) }
64+
}
6065
}
6166

6267
impl<T> Rc<T> {
@@ -73,8 +78,7 @@ impl<T> Drop for Rc<T> {
7378
if self.ptr.is_not_null() {
7479
(*self.ptr).count -= 1;
7580
if (*self.ptr).count == 0 {
76-
ptr::read_ptr(self.ptr);
77-
free(self.ptr as *c_void)
81+
let _: ~T = cast::transmute(self.ptr);
7882
}
7983
}
8084
}
@@ -107,7 +111,7 @@ mod test_rc {
107111

108112
#[test]
109113
fn test_clone() {
110-
let x = rc_from_owned(Cell::new(5));
114+
let x = Rc::from_owned(Cell::new(5));
111115
let y = x.clone();
112116
do x.borrow().with_mut_ref |inner| {
113117
*inner = 20;
@@ -117,7 +121,7 @@ mod test_rc {
117121

118122
#[test]
119123
fn test_deep_clone() {
120-
let x = rc_from_owned(Cell::new(5));
124+
let x = Rc::from_owned(Cell::new(5));
121125
let y = x.deep_clone();
122126
do x.borrow().with_mut_ref |inner| {
123127
*inner = 20;
@@ -127,31 +131,25 @@ mod test_rc {
127131

128132
#[test]
129133
fn test_simple() {
130-
let x = rc_from_const(5);
134+
let x = Rc::from_const(5);
131135
assert_eq!(*x.borrow(), 5);
132136
}
133137

134138
#[test]
135139
fn test_simple_clone() {
136-
let x = rc_from_const(5);
140+
let x = Rc::from_const(5);
137141
let y = x.clone();
138142
assert_eq!(*x.borrow(), 5);
139143
assert_eq!(*y.borrow(), 5);
140144
}
141145

142146
#[test]
143147
fn test_destructor() {
144-
let x = rc_from_owned(~5);
148+
let x = Rc::from_owned(~5);
145149
assert_eq!(**x.borrow(), 5);
146150
}
147151
}
148152

149-
#[abi = "rust-intrinsic"]
150-
extern "rust-intrinsic" {
151-
fn init<T>() -> T;
152-
fn uninit<T>() -> T;
153-
}
154-
155153
#[deriving(Eq)]
156154
enum Borrow {
157155
Mutable,
@@ -175,21 +173,20 @@ pub struct RcMut<T> {
175173

176174
impl<T> RcMut<T> {
177175
unsafe fn new(value: T) -> RcMut<T> {
178-
let ptr = malloc(sys::size_of::<RcMutBox<T>>() as size_t) as *mut RcMutBox<T>;
179-
assert!(!ptr::is_null(ptr));
180-
intrinsics::move_val_init(&mut *ptr, RcMutBox{value: value, count: 1, borrow: Nothing});
181-
RcMut{ptr: ptr}
176+
RcMut{ptr: owned_to_raw(~RcMutBox{value: value, count: 1, borrow: Nothing})}
182177
}
183178
}
184179

185-
// FIXME: #6516: should be a static method
186-
pub fn rc_mut_from_owned<T: Send>(value: T) -> RcMut<T> {
187-
unsafe { RcMut::new(value) }
180+
impl<T: Send> RcMut<T> {
181+
pub fn from_owned(value: T) -> RcMut<T> {
182+
unsafe { RcMut::new(value) }
183+
}
188184
}
189185

190-
// FIXME: #6516: should be a static method
191-
pub fn rc_mut_from_const<T: Freeze>(value: T) -> RcMut<T> {
192-
unsafe { RcMut::new(value) }
186+
impl<T: Freeze> RcMut<T> {
187+
pub fn from_const(value: T) -> RcMut<T> {
188+
unsafe { RcMut::new(value) }
189+
}
193190
}
194191

195192
impl<T> RcMut<T> {
@@ -226,8 +223,7 @@ impl<T> Drop for RcMut<T> {
226223
if self.ptr.is_not_null() {
227224
(*self.ptr).count -= 1;
228225
if (*self.ptr).count == 0 {
229-
ptr::replace_ptr(self.ptr, uninit());
230-
free(self.ptr as *c_void)
226+
let _: ~T = cast::transmute(self.ptr);
231227
}
232228
}
233229
}
@@ -262,7 +258,7 @@ mod test_rc_mut {
262258

263259
#[test]
264260
fn test_clone() {
265-
let x = rc_mut_from_owned(5);
261+
let x = RcMut::from_owned(5);
266262
let y = x.clone();
267263
do x.with_mut_borrow |value| {
268264
*value = 20;
@@ -274,7 +270,7 @@ mod test_rc_mut {
274270

275271
#[test]
276272
fn test_deep_clone() {
277-
let x = rc_mut_from_const(5);
273+
let x = RcMut::from_const(5);
278274
let y = x.deep_clone();
279275
do x.with_mut_borrow |value| {
280276
*value = 20;
@@ -286,7 +282,7 @@ mod test_rc_mut {
286282

287283
#[test]
288284
fn borrow_many() {
289-
let x = rc_mut_from_owned(5);
285+
let x = RcMut::from_owned(5);
290286
let y = x.clone();
291287

292288
do x.with_borrow |a| {
@@ -302,7 +298,7 @@ mod test_rc_mut {
302298

303299
#[test]
304300
fn modify() {
305-
let x = rc_mut_from_const(5);
301+
let x = RcMut::from_const(5);
306302
let y = x.clone();
307303

308304
do y.with_mut_borrow |a| {
@@ -317,22 +313,22 @@ mod test_rc_mut {
317313

318314
#[test]
319315
fn release_immutable() {
320-
let x = rc_mut_from_owned(5);
316+
let x = RcMut::from_owned(5);
321317
do x.with_borrow |_| {}
322318
do x.with_mut_borrow |_| {}
323319
}
324320

325321
#[test]
326322
fn release_mutable() {
327-
let x = rc_mut_from_const(5);
323+
let x = RcMut::from_const(5);
328324
do x.with_mut_borrow |_| {}
329325
do x.with_borrow |_| {}
330326
}
331327

332328
#[test]
333329
#[should_fail]
334330
fn frozen() {
335-
let x = rc_mut_from_owned(5);
331+
let x = RcMut::from_owned(5);
336332
let y = x.clone();
337333

338334
do x.with_borrow |_| {
@@ -344,7 +340,7 @@ mod test_rc_mut {
344340
#[test]
345341
#[should_fail]
346342
fn mutable_dupe() {
347-
let x = rc_mut_from_const(5);
343+
let x = RcMut::from_const(5);
348344
let y = x.clone();
349345

350346
do x.with_mut_borrow |_| {
@@ -356,7 +352,7 @@ mod test_rc_mut {
356352
#[test]
357353
#[should_fail]
358354
fn mutable_freeze() {
359-
let x = rc_mut_from_owned(5);
355+
let x = RcMut::from_owned(5);
360356
let y = x.clone();
361357

362358
do x.with_mut_borrow |_| {
@@ -368,7 +364,7 @@ mod test_rc_mut {
368364
#[test]
369365
#[should_fail]
370366
fn restore_freeze() {
371-
let x = rc_mut_from_const(5);
367+
let x = RcMut::from_const(5);
372368
let y = x.clone();
373369

374370
do x.with_borrow |_| {

0 commit comments

Comments
 (0)