141
141
//! }
142
142
//! ```
143
143
144
+ #![ stable]
145
+
144
146
use cmp:: { PartialEq , Eq , Ord } ;
145
147
use default:: Default ;
146
148
use slice:: Slice ;
@@ -155,6 +157,7 @@ use slice;
155
157
156
158
/// The `Option` type.
157
159
#[ deriving( Clone , PartialEq , PartialOrd , Eq , Ord , Show ) ]
160
+ #[ stable]
158
161
pub enum Option < T > {
159
162
/// No value
160
163
None ,
@@ -173,6 +176,7 @@ impl<T> Option<T> {
173
176
174
177
/// Returns `true` if the option is a `Some` value
175
178
#[ inline]
179
+ #[ stable]
176
180
pub fn is_some ( & self ) -> bool {
177
181
match * self {
178
182
Some ( _) => true ,
@@ -182,6 +186,7 @@ impl<T> Option<T> {
182
186
183
187
/// Returns `true` if the option is a `None` value
184
188
#[ inline]
189
+ #[ stable]
185
190
pub fn is_none ( & self ) -> bool {
186
191
!self . is_some ( )
187
192
}
@@ -207,18 +212,21 @@ impl<T> Option<T> {
207
212
/// println!("still can print num_as_str: {}", num_as_str);
208
213
/// ```
209
214
#[ inline]
215
+ #[ stable]
210
216
pub fn as_ref < ' r > ( & ' r self ) -> Option < & ' r T > {
211
217
match * self { Some ( ref x) => Some ( x) , None => None }
212
218
}
213
219
214
220
/// Convert from `Option<T>` to `Option<&mut T>`
215
221
#[ inline]
222
+ #[ unstable = "waiting for mut conventions" ]
216
223
pub fn as_mut < ' r > ( & ' r mut self ) -> Option < & ' r mut T > {
217
224
match * self { Some ( ref mut x) => Some ( x) , None => None }
218
225
}
219
226
220
227
/// Convert from `Option<T>` to `&mut [T]` (without copying)
221
228
#[ inline]
229
+ #[ unstable = "waiting for mut conventions" ]
222
230
pub fn as_mut_slice < ' r > ( & ' r mut self ) -> & ' r mut [ T ] {
223
231
match * self {
224
232
Some ( ref mut x) => {
@@ -243,6 +251,7 @@ impl<T> Option<T> {
243
251
/// Fails if the value is a `None` with a custom failure message provided by
244
252
/// `msg`.
245
253
#[ inline]
254
+ #[ unstable = "waiting for conventions" ]
246
255
pub fn expect ( self , msg : & str ) -> T {
247
256
match self {
248
257
Some ( val) => val,
@@ -262,6 +271,7 @@ impl<T> Option<T> {
262
271
/// Instead, prefer to use pattern matching and handle the `None`
263
272
/// case explicitly.
264
273
#[ inline]
274
+ #[ unstable = "waiting for conventions" ]
265
275
pub fn unwrap ( self ) -> T {
266
276
match self {
267
277
Some ( val) => val,
@@ -271,6 +281,7 @@ impl<T> Option<T> {
271
281
272
282
/// Returns the contained value or a default.
273
283
#[ inline]
284
+ #[ unstable = "waiting for conventions" ]
274
285
pub fn unwrap_or ( self , def : T ) -> T {
275
286
match self {
276
287
Some ( x) => x,
@@ -280,6 +291,7 @@ impl<T> Option<T> {
280
291
281
292
/// Returns the contained value or computes it from a closure.
282
293
#[ inline]
294
+ #[ unstable = "waiting for conventions" ]
283
295
pub fn unwrap_or_else ( self , f: || -> T ) -> T {
284
296
match self {
285
297
Some ( x) => x,
@@ -303,27 +315,42 @@ impl<T> Option<T> {
303
315
/// let num_as_int: Option<uint> = num_as_str.map(|n| n.len());
304
316
/// ```
305
317
#[ inline]
318
+ #[ unstable = "waiting for unboxed closures" ]
306
319
pub fn map < U > ( self , f: |T | -> U ) -> Option < U > {
307
320
match self { Some ( x) => Some ( f ( x) ) , None => None }
308
321
}
309
322
310
323
/// Applies a function to the contained value or returns a default.
311
324
#[ inline]
325
+ #[ unstable = "waiting for unboxed closures" ]
312
326
pub fn map_or < U > ( self , def : U , f: |T | -> U ) -> U {
313
327
match self { None => def, Some ( t) => f ( t) }
314
328
}
315
329
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
+ ///
316
339
/// Applies a function to the contained value or does nothing.
317
340
/// Returns true if the contained value was mutated.
341
+ #[ deprecated = "removed due to lack of use" ]
318
342
pub fn mutate ( & mut self , f: |T | -> T ) -> bool {
319
343
if self . is_some ( ) {
320
344
* self = Some ( f ( self . take_unwrap ( ) ) ) ;
321
345
true
322
346
} else { false }
323
347
}
324
348
349
+ /// Deprecated.
350
+ ///
325
351
/// Applies a function to the contained value or sets it to a default.
326
352
/// Returns true if the contained value was mutated, or false if set to the default.
353
+ #[ deprecated = "removed due to lack of use" ]
327
354
pub fn mutate_or_set ( & mut self , def : T , f: |T | -> T ) -> bool {
328
355
if self . is_some ( ) {
329
356
* self = Some ( f ( self . take_unwrap ( ) ) ) ;
@@ -340,18 +367,21 @@ impl<T> Option<T> {
340
367
341
368
/// Returns an iterator over the possibly contained value.
342
369
#[ inline]
370
+ #[ unstable = "waiting for iterator conventions" ]
343
371
pub fn iter < ' r > ( & ' r self ) -> Item < & ' r T > {
344
372
Item { opt : self . as_ref ( ) }
345
373
}
346
374
347
375
/// Returns a mutable iterator over the possibly contained value.
348
376
#[ inline]
377
+ #[ unstable = "waiting for iterator conventions" ]
349
378
pub fn mut_iter < ' r > ( & ' r mut self ) -> Item < & ' r mut T > {
350
379
Item { opt : self . as_mut ( ) }
351
380
}
352
381
353
382
/// Returns a consuming iterator over the possibly contained value.
354
383
#[ inline]
384
+ #[ unstable = "waiting for iterator conventions" ]
355
385
pub fn move_iter ( self ) -> Item < T > {
356
386
Item { opt : self }
357
387
}
@@ -362,6 +392,7 @@ impl<T> Option<T> {
362
392
363
393
/// Returns `None` if the option is `None`, otherwise returns `optb`.
364
394
#[ inline]
395
+ #[ stable]
365
396
pub fn and < U > ( self , optb : Option < U > ) -> Option < U > {
366
397
match self {
367
398
Some ( _) => optb,
@@ -372,6 +403,7 @@ impl<T> Option<T> {
372
403
/// Returns `None` if the option is `None`, otherwise calls `f` with the
373
404
/// wrapped value and returns the result.
374
405
#[ inline]
406
+ #[ unstable = "waiting for unboxed closures" ]
375
407
pub fn and_then < U > ( self , f: |T | -> Option < U > ) -> Option < U > {
376
408
match self {
377
409
Some ( x) => f ( x ) ,
@@ -381,6 +413,7 @@ impl<T> Option<T> {
381
413
382
414
/// Returns the option if it contains a value, otherwise returns `optb`.
383
415
#[ inline]
416
+ #[ stable]
384
417
pub fn or( self , optb : Option < T > ) -> Option < T > {
385
418
match self {
386
419
Some ( _) => self ,
@@ -391,6 +424,7 @@ impl<T> Option<T> {
391
424
/// Returns the option if it contains a value, otherwise calls `f` and
392
425
/// returns the result.
393
426
#[ inline]
427
+ #[ unstable = "waiting for unboxed closures" ]
394
428
pub fn or_else ( self , f: || -> Option < T > ) -> Option < T > {
395
429
match self {
396
430
Some ( _) => self ,
@@ -404,21 +438,28 @@ impl<T> Option<T> {
404
438
405
439
/// Takes the value out of the option, leaving a `None` in its place.
406
440
#[ inline]
441
+ #[ stable]
407
442
pub fn take ( & mut self ) -> Option < T > {
408
443
mem:: replace ( self , None )
409
444
}
410
445
446
+ /// Deprecated.
447
+ ///
411
448
/// Filters an optional value using a given function.
412
449
#[ inline( always) ]
450
+ #[ deprecated = "removed due to lack of use" ]
413
451
pub fn filtered ( self , f: |t: & T | -> bool ) -> Option < T > {
414
452
match self {
415
453
Some ( x) => if f( & x) { Some ( x) } else { None } ,
416
454
None => None
417
455
}
418
456
}
419
457
458
+ /// Deprecated.
459
+ ///
420
460
/// Applies a function zero or more times until the result is `None`.
421
461
#[ inline]
462
+ #[ deprecated = "removed due to lack of use" ]
422
463
pub fn while_some ( self , f: |v: T | -> Option < T > ) {
423
464
let mut opt = self ;
424
465
loop {
@@ -433,20 +474,25 @@ impl<T> Option<T> {
433
474
// Common special cases
434
475
/////////////////////////////////////////////////////////////////////////
435
476
477
+ /// Deprecated: use `take().unwrap()` instead.
478
+ ///
436
479
/// The option dance. Moves a value out of an option type and returns it,
437
480
/// replacing the original with `None`.
438
481
///
439
482
/// # Failure
440
483
///
441
484
/// Fails if the value equals `None`.
442
485
#[ inline]
486
+ #[ deprecated = "use take().unwrap() instead" ]
443
487
pub fn take_unwrap ( & mut self ) -> T {
444
488
match self . take ( ) {
445
489
Some ( x) => x,
446
490
None => fail ! ( "called `Option::take_unwrap()` on a `None` value" )
447
491
}
448
492
}
449
493
494
+ /// Deprecated: use `as_ref().unwrap()` instead.
495
+ ///
450
496
/// Gets an immutable reference to the value inside an option.
451
497
///
452
498
/// # Failure
@@ -460,13 +506,16 @@ impl<T> Option<T> {
460
506
/// Instead, prefer to use pattern matching and handle the `None`
461
507
/// case explicitly.
462
508
#[ inline]
509
+ #[ deprecated = "use .as_ref().unwrap() instead" ]
463
510
pub fn get_ref < ' a > ( & ' a self ) -> & ' a T {
464
511
match * self {
465
512
Some ( ref x) => x,
466
513
None => fail ! ( "called `Option::get_ref()` on a `None` value" ) ,
467
514
}
468
515
}
469
516
517
+ /// Deprecated: use `as_mut().unwrap()` instead.
518
+ ///
470
519
/// Gets a mutable reference to the value inside an option.
471
520
///
472
521
/// # Failure
@@ -480,6 +529,7 @@ impl<T> Option<T> {
480
529
/// Instead, prefer to use pattern matching and handle the `None`
481
530
/// case explicitly.
482
531
#[ inline]
532
+ #[ deprecated = "use .as_mut().unwrap() instead" ]
483
533
pub fn get_mut_ref < ' a > ( & ' a mut self ) -> & ' a mut T {
484
534
match * self {
485
535
Some ( ref mut x) => x,
@@ -512,6 +562,7 @@ impl<T: Default> Option<T> {
512
562
/// assert_eq!(0i, bad_year);
513
563
/// ```
514
564
#[ inline]
565
+ #[ unstable = "waiting for conventions" ]
515
566
pub fn unwrap_or_default ( self ) -> T {
516
567
match self {
517
568
Some ( x) => x,
@@ -527,6 +578,7 @@ impl<T: Default> Option<T> {
527
578
impl < T > Slice < T > for Option < T > {
528
579
/// Convert from `Option<T>` to `&[T]` (without copying)
529
580
#[ inline]
581
+ #[ stable]
530
582
fn as_slice < ' a > ( & ' a self ) -> & ' a [ T ] {
531
583
match * self {
532
584
Some ( ref x) => slice:: ref_slice ( x) ,
@@ -552,6 +604,7 @@ impl<T> Default for Option<T> {
552
604
/// The `Item` iterator is returned by the `iter`, `mut_iter` and `move_iter`
553
605
/// methods on `Option`.
554
606
#[ deriving( Clone ) ]
607
+ #[ unstable = "waiting for iterator conventions" ]
555
608
pub struct Item < A > {
556
609
opt : Option < A >
557
610
}
@@ -584,54 +637,62 @@ impl<A> ExactSize<A> for Item<A> {}
584
637
// Free functions
585
638
/////////////////////////////////////////////////////////////////////////////
586
639
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.
605
641
#[ 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 ,
623
685
}
624
- None => None ,
625
686
}
626
687
}
627
- }
628
688
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 ( ) ) ;
631
691
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
+ }
636
697
}
637
698
}
0 commit comments