141141//! }
142142//! ```
143143
144+ #![ stable]
145+
144146use cmp:: { PartialEq , Eq , Ord } ;
145147use default:: Default ;
146148use slice:: Slice ;
@@ -155,6 +157,7 @@ use slice;
155157
156158/// The `Option` type.
157159#[ deriving( Clone , PartialEq , PartialOrd , Eq , Ord , Show ) ]
160+ #[ stable]
158161pub enum Option < T > {
159162 /// No value
160163 None ,
@@ -173,6 +176,7 @@ impl<T> Option<T> {
173176
174177 /// Returns `true` if the option is a `Some` value
175178 #[ inline]
179+ #[ stable]
176180 pub fn is_some ( & self ) -> bool {
177181 match * self {
178182 Some ( _) => true ,
@@ -182,6 +186,7 @@ impl<T> Option<T> {
182186
183187 /// Returns `true` if the option is a `None` value
184188 #[ inline]
189+ #[ stable]
185190 pub fn is_none ( & self ) -> bool {
186191 !self . is_some ( )
187192 }
@@ -207,18 +212,21 @@ impl<T> Option<T> {
207212 /// println!("still can print num_as_str: {}", num_as_str);
208213 /// ```
209214 #[ inline]
215+ #[ stable]
210216 pub fn as_ref < ' r > ( & ' r self ) -> Option < & ' r T > {
211217 match * self { Some ( ref x) => Some ( x) , None => None }
212218 }
213219
214220 /// Convert from `Option<T>` to `Option<&mut T>`
215221 #[ inline]
222+ #[ unstable = "waiting for mut conventions" ]
216223 pub fn as_mut < ' r > ( & ' r mut self ) -> Option < & ' r mut T > {
217224 match * self { Some ( ref mut x) => Some ( x) , None => None }
218225 }
219226
220227 /// Convert from `Option<T>` to `&mut [T]` (without copying)
221228 #[ inline]
229+ #[ unstable = "waiting for mut conventions" ]
222230 pub fn as_mut_slice < ' r > ( & ' r mut self ) -> & ' r mut [ T ] {
223231 match * self {
224232 Some ( ref mut x) => {
@@ -243,6 +251,7 @@ impl<T> Option<T> {
243251 /// Fails if the value is a `None` with a custom failure message provided by
244252 /// `msg`.
245253 #[ inline]
254+ #[ unstable = "waiting for conventions" ]
246255 pub fn expect ( self , msg : & str ) -> T {
247256 match self {
248257 Some ( val) => val,
@@ -262,6 +271,7 @@ impl<T> Option<T> {
262271 /// Instead, prefer to use pattern matching and handle the `None`
263272 /// case explicitly.
264273 #[ inline]
274+ #[ unstable = "waiting for conventions" ]
265275 pub fn unwrap ( self ) -> T {
266276 match self {
267277 Some ( val) => val,
@@ -271,6 +281,7 @@ impl<T> Option<T> {
271281
272282 /// Returns the contained value or a default.
273283 #[ inline]
284+ #[ unstable = "waiting for conventions" ]
274285 pub fn unwrap_or ( self , def : T ) -> T {
275286 match self {
276287 Some ( x) => x,
@@ -280,6 +291,7 @@ impl<T> Option<T> {
280291
281292 /// Returns the contained value or computes it from a closure.
282293 #[ inline]
294+ #[ unstable = "waiting for conventions" ]
283295 pub fn unwrap_or_else ( self , f: || -> T ) -> T {
284296 match self {
285297 Some ( x) => x,
@@ -303,27 +315,42 @@ impl<T> Option<T> {
303315 /// let num_as_int: Option<uint> = num_as_str.map(|n| n.len());
304316 /// ```
305317 #[ inline]
318+ #[ unstable = "waiting for unboxed closures" ]
306319 pub fn map < U > ( self , f: |T | -> U ) -> Option < U > {
307320 match self { Some ( x) => Some ( f ( x) ) , None => None }
308321 }
309322
310323 /// Applies a function to the contained value or returns a default.
311324 #[ inline]
325+ #[ unstable = "waiting for unboxed closures" ]
312326 pub fn map_or < U > ( self , def : U , f: |T | -> U ) -> U {
313327 match self { None => def, Some ( t) => f ( t) }
314328 }
315329
330+ /// Applies a function to the contained value or computes a default.
331+ #[ inline]
332+ #[ unstable = "waiting for unboxed closures" ]
333+ pub fn map_or_else < U > ( self , def: || -> U , f: |T | -> U ) -> U {
334+ match self { None => def ( ) , Some ( t) => f ( t) }
335+ }
336+
337+ /// Deprecated.
338+ ///
316339 /// Applies a function to the contained value or does nothing.
317340 /// Returns true if the contained value was mutated.
341+ #[ deprecated = "removed due to lack of use" ]
318342 pub fn mutate ( & mut self , f: |T | -> T ) -> bool {
319343 if self . is_some ( ) {
320344 * self = Some ( f ( self . take_unwrap ( ) ) ) ;
321345 true
322346 } else { false }
323347 }
324348
349+ /// Deprecated.
350+ ///
325351 /// Applies a function to the contained value or sets it to a default.
326352 /// Returns true if the contained value was mutated, or false if set to the default.
353+ #[ deprecated = "removed due to lack of use" ]
327354 pub fn mutate_or_set ( & mut self , def : T , f: |T | -> T ) -> bool {
328355 if self . is_some ( ) {
329356 * self = Some ( f ( self . take_unwrap ( ) ) ) ;
@@ -340,18 +367,21 @@ impl<T> Option<T> {
340367
341368 /// Returns an iterator over the possibly contained value.
342369 #[ inline]
370+ #[ unstable = "waiting for iterator conventions" ]
343371 pub fn iter < ' r > ( & ' r self ) -> Item < & ' r T > {
344372 Item { opt : self . as_ref ( ) }
345373 }
346374
347375 /// Returns a mutable iterator over the possibly contained value.
348376 #[ inline]
377+ #[ unstable = "waiting for iterator conventions" ]
349378 pub fn mut_iter < ' r > ( & ' r mut self ) -> Item < & ' r mut T > {
350379 Item { opt : self . as_mut ( ) }
351380 }
352381
353382 /// Returns a consuming iterator over the possibly contained value.
354383 #[ inline]
384+ #[ unstable = "waiting for iterator conventions" ]
355385 pub fn move_iter ( self ) -> Item < T > {
356386 Item { opt : self }
357387 }
@@ -362,6 +392,7 @@ impl<T> Option<T> {
362392
363393 /// Returns `None` if the option is `None`, otherwise returns `optb`.
364394 #[ inline]
395+ #[ stable]
365396 pub fn and < U > ( self , optb : Option < U > ) -> Option < U > {
366397 match self {
367398 Some ( _) => optb,
@@ -372,6 +403,7 @@ impl<T> Option<T> {
372403 /// Returns `None` if the option is `None`, otherwise calls `f` with the
373404 /// wrapped value and returns the result.
374405 #[ inline]
406+ #[ unstable = "waiting for unboxed closures" ]
375407 pub fn and_then < U > ( self , f: |T | -> Option < U > ) -> Option < U > {
376408 match self {
377409 Some ( x) => f ( x ) ,
@@ -381,6 +413,7 @@ impl<T> Option<T> {
381413
382414 /// Returns the option if it contains a value, otherwise returns `optb`.
383415 #[ inline]
416+ #[ stable]
384417 pub fn or( self , optb : Option < T > ) -> Option < T > {
385418 match self {
386419 Some ( _) => self ,
@@ -391,6 +424,7 @@ impl<T> Option<T> {
391424 /// Returns the option if it contains a value, otherwise calls `f` and
392425 /// returns the result.
393426 #[ inline]
427+ #[ unstable = "waiting for unboxed closures" ]
394428 pub fn or_else ( self , f: || -> Option < T > ) -> Option < T > {
395429 match self {
396430 Some ( _) => self ,
@@ -404,21 +438,28 @@ impl<T> Option<T> {
404438
405439 /// Takes the value out of the option, leaving a `None` in its place.
406440 #[ inline]
441+ #[ stable]
407442 pub fn take ( & mut self ) -> Option < T > {
408443 mem:: replace ( self , None )
409444 }
410445
446+ /// Deprecated.
447+ ///
411448 /// Filters an optional value using a given function.
412449 #[ inline( always) ]
450+ #[ deprecated = "removed due to lack of use" ]
413451 pub fn filtered ( self , f: |t: & T | -> bool ) -> Option < T > {
414452 match self {
415453 Some ( x) => if f( & x) { Some ( x) } else { None } ,
416454 None => None
417455 }
418456 }
419457
458+ /// Deprecated.
459+ ///
420460 /// Applies a function zero or more times until the result is `None`.
421461 #[ inline]
462+ #[ deprecated = "removed due to lack of use" ]
422463 pub fn while_some ( self , f: |v: T | -> Option < T > ) {
423464 let mut opt = self ;
424465 loop {
@@ -433,20 +474,25 @@ impl<T> Option<T> {
433474 // Common special cases
434475 /////////////////////////////////////////////////////////////////////////
435476
477+ /// Deprecated: use `take().unwrap()` instead.
478+ ///
436479 /// The option dance. Moves a value out of an option type and returns it,
437480 /// replacing the original with `None`.
438481 ///
439482 /// # Failure
440483 ///
441484 /// Fails if the value equals `None`.
442485 #[ inline]
486+ #[ deprecated = "use take().unwrap() instead" ]
443487 pub fn take_unwrap ( & mut self ) -> T {
444488 match self . take ( ) {
445489 Some ( x) => x,
446490 None => fail ! ( "called `Option::take_unwrap()` on a `None` value" )
447491 }
448492 }
449493
494+ /// Deprecated: use `as_ref().unwrap()` instead.
495+ ///
450496 /// Gets an immutable reference to the value inside an option.
451497 ///
452498 /// # Failure
@@ -460,13 +506,16 @@ impl<T> Option<T> {
460506 /// Instead, prefer to use pattern matching and handle the `None`
461507 /// case explicitly.
462508 #[ inline]
509+ #[ deprecated = "use .as_ref().unwrap() instead" ]
463510 pub fn get_ref < ' a > ( & ' a self ) -> & ' a T {
464511 match * self {
465512 Some ( ref x) => x,
466513 None => fail ! ( "called `Option::get_ref()` on a `None` value" ) ,
467514 }
468515 }
469516
517+ /// Deprecated: use `as_mut().unwrap()` instead.
518+ ///
470519 /// Gets a mutable reference to the value inside an option.
471520 ///
472521 /// # Failure
@@ -480,6 +529,7 @@ impl<T> Option<T> {
480529 /// Instead, prefer to use pattern matching and handle the `None`
481530 /// case explicitly.
482531 #[ inline]
532+ #[ deprecated = "use .as_mut().unwrap() instead" ]
483533 pub fn get_mut_ref < ' a > ( & ' a mut self ) -> & ' a mut T {
484534 match * self {
485535 Some ( ref mut x) => x,
@@ -512,6 +562,7 @@ impl<T: Default> Option<T> {
512562 /// assert_eq!(0i, bad_year);
513563 /// ```
514564 #[ inline]
565+ #[ unstable = "waiting for conventions" ]
515566 pub fn unwrap_or_default ( self ) -> T {
516567 match self {
517568 Some ( x) => x,
@@ -527,6 +578,7 @@ impl<T: Default> Option<T> {
527578impl < T > Slice < T > for Option < T > {
528579 /// Convert from `Option<T>` to `&[T]` (without copying)
529580 #[ inline]
581+ #[ stable]
530582 fn as_slice < ' a > ( & ' a self ) -> & ' a [ T ] {
531583 match * self {
532584 Some ( ref x) => slice:: ref_slice ( x) ,
@@ -552,6 +604,7 @@ impl<T> Default for Option<T> {
552604/// The `Item` iterator is returned by the `iter`, `mut_iter` and `move_iter`
553605/// methods on `Option`.
554606#[ deriving( Clone ) ]
607+ #[ unstable = "waiting for iterator conventions" ]
555608pub struct Item < A > {
556609 opt : Option < A >
557610}
@@ -584,54 +637,62 @@ impl<A> ExactSize<A> for Item<A> {}
584637// Free functions
585638/////////////////////////////////////////////////////////////////////////////
586639
587- /// Takes each element in the `Iterator`: if it is `None`, no further
588- /// elements are taken, and the `None` is returned. Should no `None` occur, a
589- /// vector containing the values of each `Option` is returned.
590- ///
591- /// Here is an example which increments every integer in a vector,
592- /// checking for overflow:
593- ///
594- /// ```rust
595- /// use std::option;
596- /// use std::uint;
597- ///
598- /// let v = vec!(1u, 2u);
599- /// let res: Option<Vec<uint>> = option::collect(v.iter().map(|x: &uint|
600- /// if *x == uint::MAX { None }
601- /// else { Some(x + 1) }
602- /// ));
603- /// assert!(res == Some(vec!(2u, 3u)));
604- /// ```
640+ /// Deprecated: use `Iterator::collect` instead.
605641#[ inline]
606- pub fn collect < T , Iter : Iterator < Option < T > > , V : FromIterator < T > > ( iter : Iter ) -> Option < V > {
607- // FIXME(#11084): This could be replaced with Iterator::scan when this
608- // performance bug is closed.
609-
610- struct Adapter < Iter > {
611- iter : Iter ,
612- found_none : bool ,
613- }
614-
615- impl < T , Iter : Iterator < Option < T > > > Iterator < T > for Adapter < Iter > {
616- #[ inline]
617- fn next ( & mut self ) -> Option < T > {
618- match self . iter . next ( ) {
619- Some ( Some ( value) ) => Some ( value) ,
620- Some ( None ) => {
621- self . found_none = true ;
622- None
642+ #[ deprecated = "use Iterator::collect instead" ]
643+ pub fn collect < T , Iter : Iterator < Option < T > > , V : FromIterator < T > > ( mut iter : Iter ) -> Option < V > {
644+ iter. collect ( )
645+ }
646+
647+ impl < A , V : FromIterator < A > > FromIterator < Option < A > > for Option < V > {
648+ /// Takes each element in the `Iterator`: if it is `None`, no further
649+ /// elements are taken, and the `None` is returned. Should no `None` occur, a
650+ /// container with the values of each `Option` is returned.
651+ ///
652+ /// Here is an example which increments every integer in a vector,
653+ /// checking for overflow:
654+ ///
655+ /// ```rust
656+ /// use std::uint;
657+ ///
658+ /// let v = vec!(1u, 2u);
659+ /// let res: Option<Vec<uint>> = v.iter().map(|x: &uint|
660+ /// if *x == uint::MAX { None }
661+ /// else { Some(x + 1) }
662+ /// ).collect();
663+ /// assert!(res == Some(vec!(2u, 3u)));
664+ /// ```
665+ #[ inline]
666+ fn from_iter < I : Iterator < Option < A > > > ( iter : I ) -> Option < V > {
667+ // FIXME(#11084): This could be replaced with Iterator::scan when this
668+ // performance bug is closed.
669+
670+ struct Adapter < Iter > {
671+ iter : Iter ,
672+ found_none : bool ,
673+ }
674+
675+ impl < T , Iter : Iterator < Option < T > > > Iterator < T > for Adapter < Iter > {
676+ #[ inline]
677+ fn next ( & mut self ) -> Option < T > {
678+ match self . iter . next ( ) {
679+ Some ( Some ( value) ) => Some ( value) ,
680+ Some ( None ) => {
681+ self . found_none = true ;
682+ None
683+ }
684+ None => None ,
623685 }
624- None => None ,
625686 }
626687 }
627- }
628688
629- let mut adapter = Adapter { iter : iter, found_none : false } ;
630- let v: V = FromIterator :: from_iter ( adapter. by_ref ( ) ) ;
689+ let mut adapter = Adapter { iter : iter, found_none : false } ;
690+ let v: V = FromIterator :: from_iter ( adapter. by_ref ( ) ) ;
631691
632- if adapter. found_none {
633- None
634- } else {
635- Some ( v)
692+ if adapter. found_none {
693+ None
694+ } else {
695+ Some ( v)
696+ }
636697 }
637698}
0 commit comments