@@ -48,6 +48,7 @@ use util;
48
48
use num:: Zero ;
49
49
use old_iter:: { BaseIter , MutableIter , ExtendedIter } ;
50
50
use old_iter;
51
+ use iterator:: Iterator ;
51
52
use str:: StrSlice ;
52
53
use clone:: DeepClone ;
53
54
@@ -146,7 +147,24 @@ impl<A> ExtendedIter<A> for Option<A> {
146
147
}
147
148
148
149
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
+
149
166
/// Returns true if the option equals `none`
167
+ #[ inline]
150
168
pub fn is_none ( & const self ) -> bool {
151
169
match * self { None => true , Some ( _) => false }
152
170
}
@@ -376,6 +394,26 @@ impl<T:Copy + Zero> Option<T> {
376
394
}
377
395
}
378
396
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
+
379
417
#[ test]
380
418
fn test_unwrap_ptr ( ) {
381
419
unsafe {
0 commit comments