274
274
//! the context. The caller of `fail!` should assume that execution
275
275
//! will not resume after failure, that failure is catastrophic.
276
276
277
+ #![ stable]
278
+
277
279
use clone:: Clone ;
278
280
use cmp:: PartialEq ;
279
281
use std:: fmt:: Show ;
280
- use iter:: { Iterator , FromIterator } ;
282
+ use slice;
283
+ use slice:: Slice ;
284
+ use iter:: { Iterator , DoubleEndedIterator , FromIterator , ExactSize } ;
281
285
use option:: { None , Option , Some } ;
282
286
283
287
/// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
284
288
///
285
289
/// See the [`std::result`](index.html) module documentation for details.
286
290
#[ deriving( Clone , PartialEq , PartialOrd , Eq , Ord , Show ) ]
287
291
#[ must_use]
292
+ #[ stable]
288
293
pub enum Result < T , E > {
289
294
/// Contains the success value
290
295
Ok ( T ) ,
@@ -315,6 +320,7 @@ impl<T, E> Result<T, E> {
315
320
/// # }
316
321
/// ~~~
317
322
#[ inline]
323
+ #[ stable]
318
324
pub fn is_ok ( & self ) -> bool {
319
325
match * self {
320
326
Ok ( _) => true ,
@@ -335,6 +341,7 @@ impl<T, E> Result<T, E> {
335
341
/// assert!(bogus.is_err());
336
342
/// ~~~
337
343
#[ inline]
344
+ #[ stable]
338
345
pub fn is_err ( & self ) -> bool {
339
346
!self . is_ok ( )
340
347
}
@@ -362,6 +369,7 @@ impl<T, E> Result<T, E> {
362
369
/// let bdays: File = bdays.ok().expect("unable to open birthday file");
363
370
/// ~~~
364
371
#[ inline]
372
+ #[ stable]
365
373
pub fn ok ( self ) -> Option < T > {
366
374
match self {
367
375
Ok ( x) => Some ( x) ,
@@ -374,6 +382,7 @@ impl<T, E> Result<T, E> {
374
382
/// Converts `self` into an `Option<T>`, consuming `self`,
375
383
/// and discarding the value, if any.
376
384
#[ inline]
385
+ #[ stable]
377
386
pub fn err ( self ) -> Option < E > {
378
387
match self {
379
388
Ok ( _) => None ,
@@ -390,6 +399,7 @@ impl<T, E> Result<T, E> {
390
399
/// Produces a new `Result`, containing a reference
391
400
/// into the original, leaving the original in place.
392
401
#[ inline]
402
+ #[ stable]
393
403
pub fn as_ref < ' r > ( & ' r self ) -> Result < & ' r T , & ' r E > {
394
404
match * self {
395
405
Ok ( ref x) => Ok ( x) ,
@@ -399,13 +409,28 @@ impl<T, E> Result<T, E> {
399
409
400
410
/// Convert from `Result<T, E>` to `Result<&mut T, &mut E>`
401
411
#[ inline]
412
+ #[ unstable = "waiting for mut conventions" ]
402
413
pub fn as_mut < ' r > ( & ' r mut self ) -> Result < & ' r mut T , & ' r mut E > {
403
414
match * self {
404
415
Ok ( ref mut x) => Ok ( x) ,
405
416
Err ( ref mut x) => Err ( x) ,
406
417
}
407
418
}
408
419
420
+ /// Convert from `Result<T, E>` to `&mut [T]` (without copying)
421
+ #[ inline]
422
+ #[ unstable = "waiting for mut conventions" ]
423
+ pub fn as_mut_slice < ' r > ( & ' r mut self ) -> & ' r mut [ T ] {
424
+ match * self {
425
+ Ok ( ref mut x) => slice:: mut_ref_slice ( x) ,
426
+ Err ( _) => {
427
+ // work around lack of implicit coercion from fixed-size array to slice
428
+ let emp: & mut [ _ ] = & mut [ ] ;
429
+ emp
430
+ }
431
+ }
432
+ }
433
+
409
434
/////////////////////////////////////////////////////////////////////////
410
435
// Transforming contained values
411
436
/////////////////////////////////////////////////////////////////////////
@@ -441,6 +466,7 @@ impl<T, E> Result<T, E> {
441
466
/// assert!(sum == 10);
442
467
/// ~~~
443
468
#[ inline]
469
+ #[ unstable = "waiting for unboxed closures" ]
444
470
pub fn map < U > ( self , op: |T | -> U ) -> Result < U , E > {
445
471
match self {
446
472
Ok ( t) => Ok ( op ( t) ) ,
@@ -454,19 +480,47 @@ impl<T, E> Result<T, E> {
454
480
/// This function can be used to pass through a successful result while handling
455
481
/// an error.
456
482
#[ inline]
483
+ #[ unstable = "waiting for unboxed closures" ]
457
484
pub fn map_err < F > ( self , op: |E | -> F ) -> Result < T , F > {
458
485
match self {
459
486
Ok ( t) => Ok ( t) ,
460
487
Err ( e) => Err ( op ( e) )
461
488
}
462
489
}
463
490
491
+
492
+ /////////////////////////////////////////////////////////////////////////
493
+ // Iterator constructors
494
+ /////////////////////////////////////////////////////////////////////////
495
+
496
+ /// Returns an iterator over the possibly contained value.
497
+ #[ inline]
498
+ #[ unstable = "waiting for iterator conventions" ]
499
+ pub fn iter < ' r > ( & ' r self ) -> Item < & ' r T > {
500
+ Item { opt : self . as_ref ( ) . ok ( ) }
501
+ }
502
+
503
+ /// Returns a mutable iterator over the possibly contained value.
504
+ #[ inline]
505
+ #[ unstable = "waiting for iterator conventions" ]
506
+ pub fn mut_iter < ' r > ( & ' r mut self ) -> Item < & ' r mut T > {
507
+ Item { opt : self . as_mut ( ) . ok ( ) }
508
+ }
509
+
510
+ /// Returns a consuming iterator over the possibly contained value.
511
+ #[ inline]
512
+ #[ unstable = "waiting for iterator conventions" ]
513
+ pub fn move_iter ( self ) -> Item < T > {
514
+ Item { opt : self . ok ( ) }
515
+ }
516
+
464
517
////////////////////////////////////////////////////////////////////////
465
518
// Boolean operations on the values, eager and lazy
466
519
/////////////////////////////////////////////////////////////////////////
467
520
468
521
/// Returns `res` if the result is `Ok`, otherwise returns the `Err` value of `self`.
469
522
#[ inline]
523
+ #[ stable]
470
524
pub fn and < U > ( self , res : Result < U , E > ) -> Result < U , E > {
471
525
match self {
472
526
Ok ( _) => res,
@@ -478,6 +532,7 @@ impl<T, E> Result<T, E> {
478
532
///
479
533
/// This function can be used for control flow based on result values
480
534
#[ inline]
535
+ #[ unstable = "waiting for unboxed closures" ]
481
536
pub fn and_then < U > ( self , op: |T | -> Result < U , E > ) -> Result < U , E > {
482
537
match self {
483
538
Ok ( t) => op ( t ) ,
@@ -487,6 +542,7 @@ impl<T, E> Result<T, E> {
487
542
488
543
/// Returns `res` if the result is `Err`, otherwise returns the `Ok` value of `self`.
489
544
#[ inline]
545
+ #[ stable]
490
546
pub fn or( self , res : Result < T , E > ) -> Result < T , E > {
491
547
match self {
492
548
Ok ( _) => self ,
@@ -498,6 +554,7 @@ impl<T, E> Result<T, E> {
498
554
///
499
555
/// This function can be used for control flow based on result values
500
556
#[ inline]
557
+ #[ unstable = "waiting for unboxed closures" ]
501
558
pub fn or_else < F > ( self , op: |E | -> Result < T , F > ) -> Result < T , F > {
502
559
match self {
503
560
Ok ( t) => Ok ( t ) ,
@@ -508,6 +565,7 @@ impl<T, E> Result<T, E> {
508
565
/// Unwraps a result, yielding the content of an `Ok`.
509
566
/// Else it returns `optb`.
510
567
#[ inline]
568
+ #[ unstable = "waiting for conventions" ]
511
569
pub fn unwrap_or ( self , optb : T ) -> T {
512
570
match self {
513
571
Ok ( t) => t,
@@ -518,6 +576,7 @@ impl<T, E> Result<T, E> {
518
576
/// Unwraps a result, yielding the content of an `Ok`.
519
577
/// If the value is an `Err` then it calls `op` with its value.
520
578
#[ inline]
579
+ #[ unstable = "waiting for conventions" ]
521
580
pub fn unwrap_or_else ( self , op: |E | -> T ) -> T {
522
581
match self {
523
582
Ok ( t) => t,
@@ -541,6 +600,7 @@ impl<T, E: Show> Result<T, E> {
541
600
/// Fails if the value is an `Err`, with a custom failure message provided
542
601
/// by the `Err`'s value.
543
602
#[ inline]
603
+ #[ unstable = "waiting for conventions" ]
544
604
pub fn unwrap ( self ) -> T {
545
605
match self {
546
606
Ok ( t) => t,
@@ -558,6 +618,7 @@ impl<T: Show, E> Result<T, E> {
558
618
/// Fails if the value is an `Ok`, with a custom failure message provided
559
619
/// by the `Ok`'s value.
560
620
#[ inline]
621
+ #[ unstable = "waiting for conventions" ]
561
622
pub fn unwrap_err ( self ) -> E {
562
623
match self {
563
624
Ok ( t) =>
@@ -568,57 +629,124 @@ impl<T: Show, E> Result<T, E> {
568
629
}
569
630
570
631
/////////////////////////////////////////////////////////////////////////////
571
- // Free functions
632
+ // Trait implementations
572
633
/////////////////////////////////////////////////////////////////////////////
573
634
574
- /// Takes each element in the `Iterator`: if it is an `Err`, no further
575
- /// elements are taken, and the `Err` is returned. Should no `Err` occur, a
576
- /// vector containing the values of each `Result` is returned.
577
- ///
578
- /// Here is an example which increments every integer in a vector,
579
- /// checking for overflow:
580
- ///
581
- /// ```rust
582
- /// use std::result;
583
- /// use std::uint;
635
+ impl < T , E > Slice < T > for Result < T , E > {
636
+ /// Convert from `Result<T, E>` to `&[T]` (without copying)
637
+ #[ inline]
638
+ #[ stable]
639
+ fn as_slice < ' a > ( & ' a self ) -> & ' a [ T ] {
640
+ match * self {
641
+ Ok ( ref x) => slice:: ref_slice ( x) ,
642
+ Err ( _) => {
643
+ // work around lack of implicit coercion from fixed-size array to slice
644
+ let emp: & [ _ ] = & [ ] ;
645
+ emp
646
+ }
647
+ }
648
+ }
649
+ }
650
+
651
+ /////////////////////////////////////////////////////////////////////////////
652
+ // The Result Iterator
653
+ /////////////////////////////////////////////////////////////////////////////
654
+
655
+ /// A `Result` iterator that yields either one or zero elements
584
656
///
585
- /// let v = vec!(1u, 2u);
586
- /// let res: Result<Vec<uint>, &'static str> = result::collect(v.iter().map(|x: &uint|
587
- /// if *x == uint::MAX { Err("Overflow!") }
588
- /// else { Ok(x + 1) }
589
- /// ));
590
- /// assert!(res == Ok(vec!(2u, 3u)));
591
- /// ```
592
- #[ inline]
593
- pub fn collect < T , E , Iter : Iterator < Result < T , E > > , V : FromIterator < T > > ( iter : Iter ) -> Result < V , E > {
594
- // FIXME(#11084): This could be replaced with Iterator::scan when this
595
- // performance bug is closed.
657
+ /// The `Item` iterator is returned by the `iter`, `mut_iter` and `move_iter`
658
+ /// methods on `Result`.
659
+ #[ deriving( Clone ) ]
660
+ #[ unstable = "waiting for iterator conventions" ]
661
+ pub struct Item < T > {
662
+ opt : Option < T >
663
+ }
664
+
665
+ impl < T > Iterator < T > for Item < T > {
666
+ #[ inline]
667
+ fn next ( & mut self ) -> Option < T > {
668
+ self . opt . take ( )
669
+ }
670
+
671
+ #[ inline]
672
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
673
+ match self . opt {
674
+ Some ( _) => ( 1 , Some ( 1 ) ) ,
675
+ None => ( 0 , Some ( 0 ) ) ,
676
+ }
677
+ }
678
+ }
596
679
597
- struct Adapter < Iter , E > {
598
- iter : Iter ,
599
- err : Option < E > ,
680
+ impl < A > DoubleEndedIterator < A > for Item < A > {
681
+ #[ inline]
682
+ fn next_back ( & mut self ) -> Option < A > {
683
+ self . opt . take ( )
600
684
}
685
+ }
686
+
687
+ impl < A > ExactSize < A > for Item < A > { }
601
688
602
- impl < T , E , Iter : Iterator < Result < T , E > > > Iterator < T > for Adapter < Iter , E > {
603
- #[ inline]
604
- fn next ( & mut self ) -> Option < T > {
605
- match self . iter . next ( ) {
606
- Some ( Ok ( value) ) => Some ( value) ,
607
- Some ( Err ( err) ) => {
608
- self . err = Some ( err) ;
609
- None
689
+ /////////////////////////////////////////////////////////////////////////////
690
+ // Free functions
691
+ /////////////////////////////////////////////////////////////////////////////
692
+
693
+ /// Deprecated: use `Iterator::collect`.
694
+ #[ inline]
695
+ #[ deprecated = "use Iterator::collect instead" ]
696
+ pub fn collect < T , E , Iter : Iterator < Result < T , E > > , V : FromIterator < T > > ( mut iter : Iter )
697
+ -> Result < V , E > {
698
+ iter. collect ( )
699
+ }
700
+
701
+ impl < A , E , V : FromIterator < A > > FromIterator < Result < A , E > > for Result < V , E > {
702
+ /// Takes each element in the `Iterator`: if it is an `Err`, no further
703
+ /// elements are taken, and the `Err` is returned. Should no `Err` occur, a
704
+ /// container with the values of each `Result` is returned.
705
+ ///
706
+ /// Here is an example which increments every integer in a vector,
707
+ /// checking for overflow:
708
+ ///
709
+ /// ```rust
710
+ /// use std::uint;
711
+ ///
712
+ /// let v = vec!(1u, 2u);
713
+ /// let res: Result<Vec<uint>, &'static str> = v.iter().map(|x: &uint|
714
+ /// if *x == uint::MAX { Err("Overflow!") }
715
+ /// else { Ok(x + 1) }
716
+ /// ).collect();
717
+ /// assert!(res == Ok(vec!(2u, 3u)));
718
+ /// ```
719
+ #[ inline]
720
+ fn from_iter < I : Iterator < Result < A , E > > > ( iter : I ) -> Result < V , E > {
721
+ // FIXME(#11084): This could be replaced with Iterator::scan when this
722
+ // performance bug is closed.
723
+
724
+ struct Adapter < Iter , E > {
725
+ iter : Iter ,
726
+ err : Option < E > ,
727
+ }
728
+
729
+ impl < T , E , Iter : Iterator < Result < T , E > > > Iterator < T > for Adapter < Iter , E > {
730
+ #[ inline]
731
+ fn next ( & mut self ) -> Option < T > {
732
+ match self . iter . next ( ) {
733
+ Some ( Ok ( value) ) => Some ( value) ,
734
+ Some ( Err ( err) ) => {
735
+ self . err = Some ( err) ;
736
+ None
737
+ }
738
+ None => None ,
610
739
}
611
- None => None ,
612
740
}
613
741
}
614
- }
615
742
616
- let mut adapter = Adapter { iter : iter, err : None } ;
617
- let v: V = FromIterator :: from_iter ( adapter. by_ref ( ) ) ;
743
+ let mut adapter = Adapter { iter : iter, err : None } ;
744
+ let v: V = FromIterator :: from_iter ( adapter. by_ref ( ) ) ;
618
745
619
- match adapter. err {
620
- Some ( err) => Err ( err) ,
621
- None => Ok ( v) ,
746
+ match adapter. err {
747
+ Some ( err) => Err ( err) ,
748
+ None => Ok ( v) ,
749
+ }
622
750
}
623
751
}
624
752
@@ -627,6 +755,7 @@ pub fn collect<T, E, Iter: Iterator<Result<T, E>>, V: FromIterator<T>>(iter: Ite
627
755
/// If an `Err` is encountered, it is immediately returned.
628
756
/// Otherwise, the folded value is returned.
629
757
#[ inline]
758
+ #[ experimental]
630
759
pub fn fold < T ,
631
760
V ,
632
761
E ,
@@ -644,12 +773,15 @@ pub fn fold<T,
644
773
Ok ( init)
645
774
}
646
775
776
+ /// Deprecated.
777
+ ///
647
778
/// Perform a trivial fold operation over the result values
648
779
/// from an iterator.
649
780
///
650
781
/// If an `Err` is encountered, it is immediately returned.
651
782
/// Otherwise, a simple `Ok(())` is returned.
652
783
#[ inline]
784
+ #[ deprecated = "use fold instead" ]
653
785
pub fn fold_ < T , E , Iter : Iterator < Result < T , E > > > ( iterator : Iter ) -> Result < ( ) , E > {
654
786
fold ( iterator, ( ) , |_, _| ( ) )
655
787
}
0 commit comments