Skip to content

Commit 34cda49

Browse files
committed
---
yaml --- r: 65783 b: refs/heads/master c: 4f2f545 h: refs/heads/master i: 65781: bacf2f6 65779: 91adc33 65775: 5f379ac v: v3
1 parent 5fbc980 commit 34cda49

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
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: d0b197900485c98cf2a09298823ea274db50c4d9
2+
refs/heads/master: 4f2f545ac2ce19034b138006e7ac76e502b3188b
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 18e3db7392d2d0697b7e27d6d986139960144d85
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9

trunk/src/libextra/getopts.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,21 @@
3131
* file name following -o, and accepts both -h and --help as optional flags.
3232
*
3333
* ```
34-
* extern mod extra;
35-
* use extra::getopts::*;
36-
* use std::os;
34+
* extern mod std;
35+
* use std::getopts::*;
3736
*
3837
* fn do_work(in: &str, out: Option<~str>) {
39-
* println(in);
40-
* println(match out {
41-
* Some(x) => x,
42-
* None => ~"No Output"
43-
* });
38+
* io::println(in);
39+
* io::println(match out {
40+
* Some(x) => x,
41+
* None => ~"No Output"
42+
* });
4443
* }
4544
*
46-
* fn print_usage(program: &str, _opts: &[Opt]) {
47-
* println(fmt!("Usage: %s [options]", program));
48-
* println("-o\t\tOutput");
49-
* println("-h --help\tUsage");
45+
* fn print_usage(program: &str, _opts: &[std::getopts::Opt]) {
46+
* io::println(fmt!("Usage: %s [options]", program));
47+
* io::println("-o\t\tOutput");
48+
* io::println("-h --help\tUsage");
5049
* }
5150
*
5251
* fn main() {
@@ -59,9 +58,9 @@
5958
* optflag("h"),
6059
* optflag("help")
6160
* ];
62-
* let matches = match getopts(args.tail(), opts) {
63-
* Ok(m) => { m }
64-
* Err(f) => { fail!(fail_str(f)) }
61+
* let matches = match getopts(vec::tail(args), opts) {
62+
* result::Ok(m) => { m }
63+
* result::Err(f) => { fail!(fail_str(f)) }
6564
* };
6665
* if opt_present(&matches, "h") || opt_present(&matches, "help") {
6766
* print_usage(program, opts);

trunk/src/libstd/option.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use util;
4848
use num::Zero;
4949
use old_iter::{BaseIter, MutableIter, ExtendedIter};
5050
use old_iter;
51+
use iterator::Iterator;
5152
use str::StrSlice;
5253
use clone::DeepClone;
5354

@@ -146,7 +147,24 @@ impl<A> ExtendedIter<A> for Option<A> {
146147
}
147148

148149
impl<T> Option<T> {
150+
#[inline]
151+
pub fn iter<'r>(&'r self) -> OptionIterator<'r, T> {
152+
match *self {
153+
Some(ref x) => OptionIterator{opt: Some(x)},
154+
None => OptionIterator{opt: None}
155+
}
156+
}
157+
158+
#[inline]
159+
pub fn mut_iter<'r>(&'r mut self) -> OptionMutIterator<'r, T> {
160+
match *self {
161+
Some(ref mut x) => OptionMutIterator{opt: Some(x)},
162+
None => OptionMutIterator{opt: None}
163+
}
164+
}
165+
149166
/// Returns true if the option equals `none`
167+
#[inline]
150168
pub fn is_none(&const self) -> bool {
151169
match *self { None => true, Some(_) => false }
152170
}
@@ -376,6 +394,26 @@ impl<T:Copy + Zero> Option<T> {
376394
}
377395
}
378396

397+
pub struct OptionIterator<'self, A> {
398+
priv opt: Option<&'self A>
399+
}
400+
401+
impl<'self, A> Iterator<&'self A> for OptionIterator<'self, A> {
402+
fn next(&mut self) -> Option<&'self A> {
403+
util::replace(&mut self.opt, None)
404+
}
405+
}
406+
407+
pub struct OptionMutIterator<'self, A> {
408+
priv opt: Option<&'self mut A>
409+
}
410+
411+
impl<'self, A> Iterator<&'self mut A> for OptionMutIterator<'self, A> {
412+
fn next(&mut self) -> Option<&'self mut A> {
413+
util::replace(&mut self.opt, None)
414+
}
415+
}
416+
379417
#[test]
380418
fn test_unwrap_ptr() {
381419
unsafe {

0 commit comments

Comments
 (0)