@@ -168,8 +168,10 @@ use ops::{Deref, FnOnce};
168
168
#[ stable]
169
169
pub enum Option < T > {
170
170
/// No value
171
+ #[ stable]
171
172
None ,
172
173
/// Some value `T`
174
+ #[ stable]
173
175
Some ( T )
174
176
}
175
177
@@ -261,7 +263,7 @@ impl<T> Option<T> {
261
263
/// assert_eq!(x, Some(42u));
262
264
/// ```
263
265
#[ inline]
264
- #[ unstable = "waiting for mut conventions" ]
266
+ #[ stable ]
265
267
pub fn as_mut < ' r > ( & ' r mut self ) -> Option < & ' r mut T > {
266
268
match * self {
267
269
Some ( ref mut x) => Some ( x) ,
@@ -321,7 +323,7 @@ impl<T> Option<T> {
321
323
/// x.expect("the world is ending"); // panics with `world is ending`
322
324
/// ```
323
325
#[ inline]
324
- #[ unstable = "waiting for conventions" ]
326
+ #[ stable ]
325
327
pub fn expect ( self , msg : & str ) -> T {
326
328
match self {
327
329
Some ( val) => val,
@@ -353,7 +355,7 @@ impl<T> Option<T> {
353
355
/// assert_eq!(x.unwrap(), "air"); // fails
354
356
/// ```
355
357
#[ inline]
356
- #[ unstable = "waiting for conventions" ]
358
+ #[ stable ]
357
359
pub fn unwrap ( self ) -> T {
358
360
match self {
359
361
Some ( val) => val,
@@ -370,7 +372,7 @@ impl<T> Option<T> {
370
372
/// assert_eq!(None.unwrap_or("bike"), "bike");
371
373
/// ```
372
374
#[ inline]
373
- #[ unstable = "waiting for conventions" ]
375
+ #[ stable ]
374
376
pub fn unwrap_or ( self , def : T ) -> T {
375
377
match self {
376
378
Some ( x) => x,
@@ -388,7 +390,7 @@ impl<T> Option<T> {
388
390
/// assert_eq!(None.unwrap_or_else(|| 2 * k), 20u);
389
391
/// ```
390
392
#[ inline]
391
- #[ unstable = "waiting for conventions" ]
393
+ #[ stable ]
392
394
pub fn unwrap_or_else < F : FnOnce ( ) -> T > ( self , f : F ) -> T {
393
395
match self {
394
396
Some ( x) => x,
@@ -412,7 +414,7 @@ impl<T> Option<T> {
412
414
/// let num_as_int: Option<uint> = num_as_str.map(|n| n.len());
413
415
/// ```
414
416
#[ inline]
415
- #[ unstable = "waiting for unboxed closures" ]
417
+ #[ stable ]
416
418
pub fn map < U , F : FnOnce ( T ) -> U > ( self , f : F ) -> Option < U > {
417
419
match self {
418
420
Some ( x) => Some ( f ( x) ) ,
@@ -432,7 +434,7 @@ impl<T> Option<T> {
432
434
/// assert_eq!(x.map_or(42u, |v| v.len()), 42u);
433
435
/// ```
434
436
#[ inline]
435
- #[ unstable = "waiting for unboxed closures" ]
437
+ #[ stable ]
436
438
pub fn map_or < U , F : FnOnce ( T ) -> U > ( self , def : U , f : F ) -> U {
437
439
match self {
438
440
Some ( t) => f ( t) ,
@@ -454,7 +456,7 @@ impl<T> Option<T> {
454
456
/// assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42u);
455
457
/// ```
456
458
#[ inline]
457
- #[ unstable = "waiting for unboxed closures" ]
459
+ #[ stable ]
458
460
pub fn map_or_else < U , D : FnOnce ( ) -> U , F : FnOnce ( T ) -> U > ( self , def : D , f : F ) -> U {
459
461
match self {
460
462
Some ( t) => f ( t) ,
@@ -520,9 +522,9 @@ impl<T> Option<T> {
520
522
/// assert_eq!(x.iter().next(), None);
521
523
/// ```
522
524
#[ inline]
523
- #[ unstable = "waiting for iterator conventions" ]
524
- pub fn iter < ' r > ( & ' r self ) -> Item < & ' r T > {
525
- Item { opt : self . as_ref ( ) }
525
+ #[ stable ]
526
+ pub fn iter ( & self ) -> Iter < T > {
527
+ Iter { inner : Item { opt : self . as_ref ( ) } }
526
528
}
527
529
528
530
/// Returns a mutable iterator over the possibly contained value.
@@ -542,8 +544,8 @@ impl<T> Option<T> {
542
544
/// ```
543
545
#[ inline]
544
546
#[ unstable = "waiting for iterator conventions" ]
545
- pub fn iter_mut < ' r > ( & ' r mut self ) -> Item < & ' r mut T > {
546
- Item { opt : self . as_mut ( ) }
547
+ pub fn iter_mut ( & mut self ) -> IterMut < T > {
548
+ IterMut { inner : Item { opt : self . as_mut ( ) } }
547
549
}
548
550
549
551
/// Returns a consuming iterator over the possibly contained value.
@@ -560,9 +562,9 @@ impl<T> Option<T> {
560
562
/// assert!(v.is_empty());
561
563
/// ```
562
564
#[ inline]
563
- #[ unstable = "waiting for iterator conventions" ]
564
- pub fn into_iter ( self ) -> Item < T > {
565
- Item { opt : self }
565
+ #[ stable ]
566
+ pub fn into_iter ( self ) -> IntoIter < T > {
567
+ IntoIter { inner : Item { opt : self } }
566
568
}
567
569
568
570
/////////////////////////////////////////////////////////////////////////
@@ -614,7 +616,7 @@ impl<T> Option<T> {
614
616
/// assert_eq!(None.and_then(sq).and_then(sq), None);
615
617
/// ```
616
618
#[ inline]
617
- #[ unstable = "waiting for unboxed closures" ]
619
+ #[ stable ]
618
620
pub fn and_then < U , F : FnOnce ( T ) -> Option < U > > ( self , f : F ) -> Option < U > {
619
621
match self {
620
622
Some ( x) => f ( x) ,
@@ -666,7 +668,7 @@ impl<T> Option<T> {
666
668
/// assert_eq!(None.or_else(nobody), None);
667
669
/// ```
668
670
#[ inline]
669
- #[ unstable = "waiting for unboxed closures" ]
671
+ #[ stable ]
670
672
pub fn or_else < F : FnOnce ( ) -> Option < T > > ( self , f : F ) -> Option < T > {
671
673
match self {
672
674
Some ( _) => self ,
@@ -731,7 +733,7 @@ impl<T: Default> Option<T> {
731
733
/// assert_eq!(0i, bad_year);
732
734
/// ```
733
735
#[ inline]
734
- #[ unstable = "waiting for conventions" ]
736
+ #[ stable ]
735
737
pub fn unwrap_or_default ( self ) -> T {
736
738
match self {
737
739
Some ( x) => x,
@@ -744,6 +746,7 @@ impl<T: Default> Option<T> {
744
746
// Trait implementations
745
747
/////////////////////////////////////////////////////////////////////////////
746
748
749
+ #[ unstable = "waiting on the stability of the trait itself" ]
747
750
impl < T > AsSlice < T > for Option < T > {
748
751
/// Convert from `Option<T>` to `&[T]` (without copying)
749
752
#[ inline]
@@ -761,20 +764,16 @@ impl<T> AsSlice<T> for Option<T> {
761
764
#[ stable]
762
765
impl < T > Default for Option < T > {
763
766
#[ inline]
767
+ #[ stable]
764
768
fn default ( ) -> Option < T > { None }
765
769
}
766
770
767
771
/////////////////////////////////////////////////////////////////////////////
768
- // The Option Iterator
772
+ // The Option Iterators
769
773
/////////////////////////////////////////////////////////////////////////////
770
774
771
- /// An `Option` iterator that yields either one or zero elements
772
- ///
773
- /// The `Item` iterator is returned by the `iter`, `iter_mut` and `into_iter`
774
- /// methods on `Option`.
775
775
#[ deriving( Clone ) ]
776
- #[ unstable = "waiting for iterator conventions" ]
777
- pub struct Item < A > {
776
+ struct Item < A > {
778
777
opt : Option < A >
779
778
}
780
779
@@ -802,6 +801,66 @@ impl<A> DoubleEndedIterator<A> for Item<A> {
802
801
803
802
impl < A > ExactSizeIterator < A > for Item < A > { }
804
803
804
+ /// An iterator over a reference of the contained item in an Option.
805
+ #[ stable]
806
+ pub struct Iter < ' a , A : ' a > { inner : Item < & ' a A > }
807
+
808
+ impl < ' a , A > Iterator < & ' a A > for Iter < ' a , A > {
809
+ #[ inline]
810
+ fn next ( & mut self ) -> Option < & ' a A > { self . inner . next ( ) }
811
+ #[ inline]
812
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . inner . size_hint ( ) }
813
+ }
814
+
815
+ impl < ' a , A > DoubleEndedIterator < & ' a A > for Iter < ' a , A > {
816
+ #[ inline]
817
+ fn next_back ( & mut self ) -> Option < & ' a A > { self . inner . next_back ( ) }
818
+ }
819
+
820
+ impl < ' a , A > ExactSizeIterator < & ' a A > for Iter < ' a , A > { }
821
+
822
+ impl < ' a , A > Clone for Iter < ' a , A > {
823
+ fn clone ( & self ) -> Iter < ' a , A > {
824
+ Iter { inner : self . inner . clone ( ) }
825
+ }
826
+ }
827
+
828
+ /// An iterator over a mutable reference of the contained item in an Option.
829
+ #[ stable]
830
+ pub struct IterMut < ' a , A : ' a > { inner : Item < & ' a mut A > }
831
+
832
+ impl < ' a , A > Iterator < & ' a mut A > for IterMut < ' a , A > {
833
+ #[ inline]
834
+ fn next ( & mut self ) -> Option < & ' a mut A > { self . inner . next ( ) }
835
+ #[ inline]
836
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . inner . size_hint ( ) }
837
+ }
838
+
839
+ impl < ' a , A > DoubleEndedIterator < & ' a mut A > for IterMut < ' a , A > {
840
+ #[ inline]
841
+ fn next_back ( & mut self ) -> Option < & ' a mut A > { self . inner . next_back ( ) }
842
+ }
843
+
844
+ impl < ' a , A > ExactSizeIterator < & ' a mut A > for IterMut < ' a , A > { }
845
+
846
+ /// An iterator over the item contained inside an Option.
847
+ #[ stable]
848
+ pub struct IntoIter < A > { inner : Item < A > }
849
+
850
+ impl < A > Iterator < A > for IntoIter < A > {
851
+ #[ inline]
852
+ fn next ( & mut self ) -> Option < A > { self . inner . next ( ) }
853
+ #[ inline]
854
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . inner . size_hint ( ) }
855
+ }
856
+
857
+ impl < A > DoubleEndedIterator < A > for IntoIter < A > {
858
+ #[ inline]
859
+ fn next_back ( & mut self ) -> Option < A > { self . inner . next_back ( ) }
860
+ }
861
+
862
+ impl < A > ExactSizeIterator < A > for IntoIter < A > { }
863
+
805
864
/////////////////////////////////////////////////////////////////////////////
806
865
// FromIterator
807
866
/////////////////////////////////////////////////////////////////////////////
@@ -826,6 +885,7 @@ impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
826
885
/// assert!(res == Some(vec!(2u, 3u)));
827
886
/// ```
828
887
#[ inline]
888
+ #[ stable]
829
889
fn from_iter < I : Iterator < Option < A > > > ( iter : I ) -> Option < V > {
830
890
// FIXME(#11084): This could be replaced with Iterator::scan when this
831
891
// performance bug is closed.
@@ -860,5 +920,6 @@ impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
860
920
}
861
921
}
862
922
923
+ #[ stable]
863
924
impl < T : Copy > Copy for Option < T > { }
864
925
0 commit comments