Skip to content

Commit e4cadf5

Browse files
committed
Merge pull request #19961 from alexcrichton/second-pass-result
std: Second pass stabilization of Result<T, E> Reviewed-by: aturon
2 parents ce79d84 + 8605ce2 commit e4cadf5

File tree

1 file changed

+93
-56
lines changed

1 file changed

+93
-56
lines changed

src/libcore/result.rs

+93-56
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,17 @@
230230
231231
#![stable]
232232

233-
use self::Result::*;
233+
use self::Result::{Ok, Err};
234234

235+
use clone::Clone;
236+
use fmt::Show;
237+
use iter::ExactSizeIterator;
238+
use iter::{Iterator, IteratorExt, DoubleEndedIterator, FromIterator};
235239
use kinds::Copy;
236-
use std::fmt::Show;
237-
use slice;
238-
use slice::AsSlice;
239-
use iter::{Iterator, IteratorExt, DoubleEndedIterator, FromIterator, ExactSizeIterator};
240-
use option::Option;
241-
use option::Option::{None, Some};
242240
use ops::{FnMut, FnOnce};
241+
use option::Option::{mod, None, Some};
242+
use slice::AsSlice;
243+
use slice;
243244

244245
/// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
245246
///
@@ -249,16 +250,19 @@ use ops::{FnMut, FnOnce};
249250
#[stable]
250251
pub enum Result<T, E> {
251252
/// Contains the success value
253+
#[stable]
252254
Ok(T),
253255

254256
/// Contains the error value
257+
#[stable]
255258
Err(E)
256259
}
257260

258261
/////////////////////////////////////////////////////////////////////////////
259262
// Type implementation
260263
/////////////////////////////////////////////////////////////////////////////
261264

265+
#[stable]
262266
impl<T, E> Result<T, E> {
263267
/////////////////////////////////////////////////////////////////////////
264268
// Querying the contained values
@@ -301,7 +305,6 @@ impl<T, E> Result<T, E> {
301305
!self.is_ok()
302306
}
303307

304-
305308
/////////////////////////////////////////////////////////////////////////
306309
// Adapter for each variant
307310
/////////////////////////////////////////////////////////////////////////
@@ -370,7 +373,7 @@ impl<T, E> Result<T, E> {
370373
/// ```
371374
#[inline]
372375
#[stable]
373-
pub fn as_ref<'r>(&'r self) -> Result<&'r T, &'r E> {
376+
pub fn as_ref(&self) -> Result<&T, &E> {
374377
match *self {
375378
Ok(ref x) => Ok(x),
376379
Err(ref x) => Err(x),
@@ -396,8 +399,8 @@ impl<T, E> Result<T, E> {
396399
/// assert_eq!(x.unwrap_err(), 0);
397400
/// ```
398401
#[inline]
399-
#[unstable = "waiting for mut conventions"]
400-
pub fn as_mut<'r>(&'r mut self) -> Result<&'r mut T, &'r mut E> {
402+
#[stable]
403+
pub fn as_mut(&mut self) -> Result<&mut T, &mut E> {
401404
match *self {
402405
Ok(ref mut x) => Ok(x),
403406
Err(ref mut x) => Err(x),
@@ -421,7 +424,7 @@ impl<T, E> Result<T, E> {
421424
/// ```
422425
#[inline]
423426
#[unstable = "waiting for mut conventions"]
424-
pub fn as_mut_slice<'r>(&'r mut self) -> &'r mut [T] {
427+
pub fn as_mut_slice(&mut self) -> &mut [T] {
425428
match *self {
426429
Ok(ref mut x) => slice::mut_ref_slice(x),
427430
Err(_) => {
@@ -466,11 +469,11 @@ impl<T, E> Result<T, E> {
466469
/// assert!(sum == 10);
467470
/// ```
468471
#[inline]
469-
#[unstable = "waiting for unboxed closures"]
472+
#[stable]
470473
pub fn map<U, F: FnOnce(T) -> U>(self, op: F) -> Result<U,E> {
471474
match self {
472-
Ok(t) => Ok(op(t)),
473-
Err(e) => Err(e)
475+
Ok(t) => Ok(op(t)),
476+
Err(e) => Err(e)
474477
}
475478
}
476479

@@ -492,15 +495,14 @@ impl<T, E> Result<T, E> {
492495
/// assert_eq!(x.map_err(stringify), Err("error code: 13".to_string()));
493496
/// ```
494497
#[inline]
495-
#[unstable = "waiting for unboxed closures"]
498+
#[stable]
496499
pub fn map_err<F, O: FnOnce(E) -> F>(self, op: O) -> Result<T,F> {
497500
match self {
498-
Ok(t) => Ok(t),
499-
Err(e) => Err(op(e))
501+
Ok(t) => Ok(t),
502+
Err(e) => Err(op(e))
500503
}
501504
}
502505

503-
504506
/////////////////////////////////////////////////////////////////////////
505507
// Iterator constructors
506508
/////////////////////////////////////////////////////////////////////////
@@ -517,9 +519,9 @@ impl<T, E> Result<T, E> {
517519
/// assert_eq!(x.iter().next(), None);
518520
/// ```
519521
#[inline]
520-
#[unstable = "waiting for iterator conventions"]
521-
pub fn iter<'r>(&'r self) -> Item<&'r T> {
522-
Item{opt: self.as_ref().ok()}
522+
#[stable]
523+
pub fn iter(&self) -> Iter<T> {
524+
Iter { inner: self.as_ref().ok() }
523525
}
524526

525527
/// Returns a mutable iterator over the possibly contained value.
@@ -538,9 +540,9 @@ impl<T, E> Result<T, E> {
538540
/// assert_eq!(x.iter_mut().next(), None);
539541
/// ```
540542
#[inline]
541-
#[unstable = "waiting for iterator conventions"]
542-
pub fn iter_mut<'r>(&'r mut self) -> Item<&'r mut T> {
543-
Item{opt: self.as_mut().ok()}
543+
#[stable]
544+
pub fn iter_mut(&mut self) -> IterMut<T> {
545+
IterMut { inner: self.as_mut().ok() }
544546
}
545547

546548
/// Returns a consuming iterator over the possibly contained value.
@@ -557,9 +559,9 @@ impl<T, E> Result<T, E> {
557559
/// assert_eq!(v, vec![]);
558560
/// ```
559561
#[inline]
560-
#[unstable = "waiting for iterator conventions"]
561-
pub fn into_iter(self) -> Item<T> {
562-
Item{opt: self.ok()}
562+
#[stable]
563+
pub fn into_iter(self) -> IntoIter<T> {
564+
IntoIter { inner: self.ok() }
563565
}
564566

565567
////////////////////////////////////////////////////////////////////////
@@ -612,7 +614,7 @@ impl<T, E> Result<T, E> {
612614
/// assert_eq!(Err(3).and_then(sq).and_then(sq), Err(3));
613615
/// ```
614616
#[inline]
615-
#[unstable = "waiting for unboxed closures"]
617+
#[stable]
616618
pub fn and_then<U, F: FnOnce(T) -> Result<U, E>>(self, op: F) -> Result<U, E> {
617619
match self {
618620
Ok(t) => op(t),
@@ -666,7 +668,7 @@ impl<T, E> Result<T, E> {
666668
/// assert_eq!(Err(3).or_else(err).or_else(err), Err(3));
667669
/// ```
668670
#[inline]
669-
#[unstable = "waiting for unboxed closures"]
671+
#[stable]
670672
pub fn or_else<F, O: FnOnce(E) -> Result<T, F>>(self, op: O) -> Result<T, F> {
671673
match self {
672674
Ok(t) => Ok(t),
@@ -688,7 +690,7 @@ impl<T, E> Result<T, E> {
688690
/// assert_eq!(x.unwrap_or(optb), optb);
689691
/// ```
690692
#[inline]
691-
#[unstable = "waiting for conventions"]
693+
#[stable]
692694
pub fn unwrap_or(self, optb: T) -> T {
693695
match self {
694696
Ok(t) => t,
@@ -708,7 +710,7 @@ impl<T, E> Result<T, E> {
708710
/// assert_eq!(Err("foo").unwrap_or_else(count), 3u);
709711
/// ```
710712
#[inline]
711-
#[unstable = "waiting for conventions"]
713+
#[stable]
712714
pub fn unwrap_or_else<F: FnOnce(E) -> T>(self, op: F) -> T {
713715
match self {
714716
Ok(t) => t,
@@ -717,6 +719,7 @@ impl<T, E> Result<T, E> {
717719
}
718720
}
719721

722+
#[stable]
720723
impl<T, E: Show> Result<T, E> {
721724
/// Unwraps a result, yielding the content of an `Ok`.
722725
///
@@ -737,7 +740,7 @@ impl<T, E: Show> Result<T, E> {
737740
/// x.unwrap(); // panics with `emergency failure`
738741
/// ```
739742
#[inline]
740-
#[unstable = "waiting for conventions"]
743+
#[stable]
741744
pub fn unwrap(self) -> T {
742745
match self {
743746
Ok(t) => t,
@@ -747,6 +750,7 @@ impl<T, E: Show> Result<T, E> {
747750
}
748751
}
749752

753+
#[stable]
750754
impl<T: Show, E> Result<T, E> {
751755
/// Unwraps a result, yielding the content of an `Err`.
752756
///
@@ -767,7 +771,7 @@ impl<T: Show, E> Result<T, E> {
767771
/// assert_eq!(x.unwrap_err(), "emergency failure");
768772
/// ```
769773
#[inline]
770-
#[unstable = "waiting for conventions"]
774+
#[stable]
771775
pub fn unwrap_err(self) -> E {
772776
match self {
773777
Ok(t) =>
@@ -798,42 +802,75 @@ impl<T, E> AsSlice<T> for Result<T, E> {
798802
}
799803

800804
/////////////////////////////////////////////////////////////////////////////
801-
// The Result Iterator
805+
// The Result Iterators
802806
/////////////////////////////////////////////////////////////////////////////
803807

804-
/// A `Result` iterator that yields either one or zero elements
805-
///
806-
/// The `Item` iterator is returned by the `iter`, `iter_mut` and `into_iter`
807-
/// methods on `Result`.
808-
#[deriving(Clone)]
809-
#[unstable = "waiting for iterator conventions"]
810-
pub struct Item<T> {
811-
opt: Option<T>
812-
}
808+
/// An iterator over a reference to the `Ok` variant of a `Result`.
809+
#[stable]
810+
pub struct Iter<'a, T: 'a> { inner: Option<&'a T> }
813811

814-
impl<T> Iterator<T> for Item<T> {
812+
impl<'a, T> Iterator<&'a T> for Iter<'a, T> {
813+
#[inline]
814+
fn next(&mut self) -> Option<&'a T> { self.inner.take() }
815815
#[inline]
816-
fn next(&mut self) -> Option<T> {
817-
self.opt.take()
816+
fn size_hint(&self) -> (uint, Option<uint>) {
817+
let n = if self.inner.is_some() {1} else {0};
818+
(n, Some(n))
818819
}
820+
}
821+
822+
impl<'a, T> DoubleEndedIterator<&'a T> for Iter<'a, T> {
823+
#[inline]
824+
fn next_back(&mut self) -> Option<&'a T> { self.inner.take() }
825+
}
819826

827+
impl<'a, T> ExactSizeIterator<&'a T> for Iter<'a, T> {}
828+
829+
impl<'a, T> Clone for Iter<'a, T> {
830+
fn clone(&self) -> Iter<'a, T> { Iter { inner: self.inner } }
831+
}
832+
833+
/// An iterator over a mutable reference to the `Ok` variant of a `Result`.
834+
#[stable]
835+
pub struct IterMut<'a, T: 'a> { inner: Option<&'a mut T> }
836+
837+
impl<'a, T> Iterator<&'a mut T> for IterMut<'a, T> {
838+
#[inline]
839+
fn next(&mut self) -> Option<&'a mut T> { self.inner.take() }
820840
#[inline]
821841
fn size_hint(&self) -> (uint, Option<uint>) {
822-
match self.opt {
823-
Some(_) => (1, Some(1)),
824-
None => (0, Some(0)),
825-
}
842+
let n = if self.inner.is_some() {1} else {0};
843+
(n, Some(n))
826844
}
827845
}
828846

829-
impl<A> DoubleEndedIterator<A> for Item<A> {
847+
impl<'a, T> DoubleEndedIterator<&'a mut T> for IterMut<'a, T> {
830848
#[inline]
831-
fn next_back(&mut self) -> Option<A> {
832-
self.opt.take()
849+
fn next_back(&mut self) -> Option<&'a mut T> { self.inner.take() }
850+
}
851+
852+
impl<'a, T> ExactSizeIterator<&'a mut T> for IterMut<'a, T> {}
853+
854+
/// An iterator over the value in a `Ok` variant of a `Result`.
855+
#[stable]
856+
pub struct IntoIter<T> { inner: Option<T> }
857+
858+
impl<T> Iterator<T> for IntoIter<T> {
859+
#[inline]
860+
fn next(&mut self) -> Option<T> { self.inner.take() }
861+
#[inline]
862+
fn size_hint(&self) -> (uint, Option<uint>) {
863+
let n = if self.inner.is_some() {1} else {0};
864+
(n, Some(n))
833865
}
834866
}
835867

836-
impl<A> ExactSizeIterator<A> for Item<A> {}
868+
impl<T> DoubleEndedIterator<T> for IntoIter<T> {
869+
#[inline]
870+
fn next_back(&mut self) -> Option<T> { self.inner.take() }
871+
}
872+
873+
impl<T> ExactSizeIterator<T> for IntoIter<T> {}
837874

838875
/////////////////////////////////////////////////////////////////////////////
839876
// FromIterator
@@ -920,5 +957,5 @@ pub fn fold<T,
920957
Ok(init)
921958
}
922959

960+
#[stable]
923961
impl<T:Copy,U:Copy> Copy for Result<T,U> {}
924-

0 commit comments

Comments
 (0)