Skip to content

Commit 1bdcfd6

Browse files
committed
auto merge of #19961 : alexcrichton/rust/second-pass-result, r=aturon
This commit, like the second pass of `Option`, largely just stablizes the existing functionality after renaming a few iterators. The specific actions taken were: * The `Ok` and `Err` variants were marked `#[stable]` as the stability inheritance was since removed. * The `as_mut` method is now stable. * The `map` method is now stable * The `map_err` method is now stable * The `iter`, `iter_mut`, and `into_iter` methods now returned structures named after the method of iteration. The methods are also now all stable. * The `and_then` method is now stable. * The `or_else` method is now stable. * The `unwrap` family of functions are now all stable: `unwrap_or`, `unwrap_or_else`, `unwrap`, and `unwrap_err`. There is a possible open extension to `Result::{and, and_then}` to make the return type further generic over `FromError` (as proposed in #19078), but this is a backwards compatible change due to the usage of default type parameters, which makes the two functions safe to stabilize now regardless of the outcome of that issue.
2 parents c141f22 + a71686f commit 1bdcfd6

File tree

1 file changed

+90
-54
lines changed

1 file changed

+90
-54
lines changed

src/libcore/result.rs

+90-54
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,15 @@
230230
231231
#![stable]
232232

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

235-
use std::fmt::Show;
236-
use slice;
237-
use slice::AsSlice;
235+
use clone::Clone;
236+
use fmt::Show;
238237
use iter::{Iterator, IteratorExt, DoubleEndedIterator, FromIterator, ExactSizeIterator};
239-
use option::Option;
240-
use option::Option::{None, Some};
241238
use ops::{FnMut, FnOnce};
239+
use option::Option::{mod, None, Some};
240+
use slice::AsSlice;
241+
use slice;
242242

243243
/// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
244244
///
@@ -248,16 +248,19 @@ use ops::{FnMut, FnOnce};
248248
#[stable]
249249
pub enum Result<T, E> {
250250
/// Contains the success value
251+
#[stable]
251252
Ok(T),
252253

253254
/// Contains the error value
255+
#[stable]
254256
Err(E)
255257
}
256258

257259
/////////////////////////////////////////////////////////////////////////////
258260
// Type implementation
259261
/////////////////////////////////////////////////////////////////////////////
260262

263+
#[stable]
261264
impl<T, E> Result<T, E> {
262265
/////////////////////////////////////////////////////////////////////////
263266
// Querying the contained values
@@ -300,7 +303,6 @@ impl<T, E> Result<T, E> {
300303
!self.is_ok()
301304
}
302305

303-
304306
/////////////////////////////////////////////////////////////////////////
305307
// Adapter for each variant
306308
/////////////////////////////////////////////////////////////////////////
@@ -369,7 +371,7 @@ impl<T, E> Result<T, E> {
369371
/// ```
370372
#[inline]
371373
#[stable]
372-
pub fn as_ref<'r>(&'r self) -> Result<&'r T, &'r E> {
374+
pub fn as_ref(&self) -> Result<&T, &E> {
373375
match *self {
374376
Ok(ref x) => Ok(x),
375377
Err(ref x) => Err(x),
@@ -395,8 +397,8 @@ impl<T, E> Result<T, E> {
395397
/// assert_eq!(x.unwrap_err(), 0);
396398
/// ```
397399
#[inline]
398-
#[unstable = "waiting for mut conventions"]
399-
pub fn as_mut<'r>(&'r mut self) -> Result<&'r mut T, &'r mut E> {
400+
#[stable]
401+
pub fn as_mut(&mut self) -> Result<&mut T, &mut E> {
400402
match *self {
401403
Ok(ref mut x) => Ok(x),
402404
Err(ref mut x) => Err(x),
@@ -420,7 +422,7 @@ impl<T, E> Result<T, E> {
420422
/// ```
421423
#[inline]
422424
#[unstable = "waiting for mut conventions"]
423-
pub fn as_mut_slice<'r>(&'r mut self) -> &'r mut [T] {
425+
pub fn as_mut_slice(&mut self) -> &mut [T] {
424426
match *self {
425427
Ok(ref mut x) => slice::mut_ref_slice(x),
426428
Err(_) => {
@@ -465,11 +467,11 @@ impl<T, E> Result<T, E> {
465467
/// assert!(sum == 10);
466468
/// ```
467469
#[inline]
468-
#[unstable = "waiting for unboxed closures"]
470+
#[stable]
469471
pub fn map<U, F: FnOnce(T) -> U>(self, op: F) -> Result<U,E> {
470472
match self {
471-
Ok(t) => Ok(op(t)),
472-
Err(e) => Err(e)
473+
Ok(t) => Ok(op(t)),
474+
Err(e) => Err(e)
473475
}
474476
}
475477

@@ -491,15 +493,14 @@ impl<T, E> Result<T, E> {
491493
/// assert_eq!(x.map_err(stringify), Err("error code: 13".to_string()));
492494
/// ```
493495
#[inline]
494-
#[unstable = "waiting for unboxed closures"]
496+
#[stable]
495497
pub fn map_err<F, O: FnOnce(E) -> F>(self, op: O) -> Result<T,F> {
496498
match self {
497-
Ok(t) => Ok(t),
498-
Err(e) => Err(op(e))
499+
Ok(t) => Ok(t),
500+
Err(e) => Err(op(e))
499501
}
500502
}
501503

502-
503504
/////////////////////////////////////////////////////////////////////////
504505
// Iterator constructors
505506
/////////////////////////////////////////////////////////////////////////
@@ -516,9 +517,9 @@ impl<T, E> Result<T, E> {
516517
/// assert_eq!(x.iter().next(), None);
517518
/// ```
518519
#[inline]
519-
#[unstable = "waiting for iterator conventions"]
520-
pub fn iter<'r>(&'r self) -> Item<&'r T> {
521-
Item{opt: self.as_ref().ok()}
520+
#[stable]
521+
pub fn iter(&self) -> Iter<T> {
522+
Iter { inner: self.as_ref().ok() }
522523
}
523524

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

545546
/// Returns a consuming iterator over the possibly contained value.
@@ -556,9 +557,9 @@ impl<T, E> Result<T, E> {
556557
/// assert_eq!(v, vec![]);
557558
/// ```
558559
#[inline]
559-
#[unstable = "waiting for iterator conventions"]
560-
pub fn into_iter(self) -> Item<T> {
561-
Item{opt: self.ok()}
560+
#[stable]
561+
pub fn into_iter(self) -> IntoIter<T> {
562+
IntoIter { inner: self.ok() }
562563
}
563564

564565
////////////////////////////////////////////////////////////////////////
@@ -611,7 +612,7 @@ impl<T, E> Result<T, E> {
611612
/// assert_eq!(Err(3).and_then(sq).and_then(sq), Err(3));
612613
/// ```
613614
#[inline]
614-
#[unstable = "waiting for unboxed closures"]
615+
#[stable]
615616
pub fn and_then<U, F: FnOnce(T) -> Result<U, E>>(self, op: F) -> Result<U, E> {
616617
match self {
617618
Ok(t) => op(t),
@@ -665,7 +666,7 @@ impl<T, E> Result<T, E> {
665666
/// assert_eq!(Err(3).or_else(err).or_else(err), Err(3));
666667
/// ```
667668
#[inline]
668-
#[unstable = "waiting for unboxed closures"]
669+
#[stable]
669670
pub fn or_else<F, O: FnOnce(E) -> Result<T, F>>(self, op: O) -> Result<T, F> {
670671
match self {
671672
Ok(t) => Ok(t),
@@ -687,7 +688,7 @@ impl<T, E> Result<T, E> {
687688
/// assert_eq!(x.unwrap_or(optb), optb);
688689
/// ```
689690
#[inline]
690-
#[unstable = "waiting for conventions"]
691+
#[stable]
691692
pub fn unwrap_or(self, optb: T) -> T {
692693
match self {
693694
Ok(t) => t,
@@ -707,7 +708,7 @@ impl<T, E> Result<T, E> {
707708
/// assert_eq!(Err("foo").unwrap_or_else(count), 3u);
708709
/// ```
709710
#[inline]
710-
#[unstable = "waiting for conventions"]
711+
#[stable]
711712
pub fn unwrap_or_else<F: FnOnce(E) -> T>(self, op: F) -> T {
712713
match self {
713714
Ok(t) => t,
@@ -716,6 +717,7 @@ impl<T, E> Result<T, E> {
716717
}
717718
}
718719

720+
#[stable]
719721
impl<T, E: Show> Result<T, E> {
720722
/// Unwraps a result, yielding the content of an `Ok`.
721723
///
@@ -736,7 +738,7 @@ impl<T, E: Show> Result<T, E> {
736738
/// x.unwrap(); // panics with `emergency failure`
737739
/// ```
738740
#[inline]
739-
#[unstable = "waiting for conventions"]
741+
#[stable]
740742
pub fn unwrap(self) -> T {
741743
match self {
742744
Ok(t) => t,
@@ -746,6 +748,7 @@ impl<T, E: Show> Result<T, E> {
746748
}
747749
}
748750

751+
#[stable]
749752
impl<T: Show, E> Result<T, E> {
750753
/// Unwraps a result, yielding the content of an `Err`.
751754
///
@@ -766,7 +769,7 @@ impl<T: Show, E> Result<T, E> {
766769
/// assert_eq!(x.unwrap_err(), "emergency failure");
767770
/// ```
768771
#[inline]
769-
#[unstable = "waiting for conventions"]
772+
#[stable]
770773
pub fn unwrap_err(self) -> E {
771774
match self {
772775
Ok(t) =>
@@ -797,42 +800,75 @@ impl<T, E> AsSlice<T> for Result<T, E> {
797800
}
798801

799802
/////////////////////////////////////////////////////////////////////////////
800-
// The Result Iterator
803+
// The Result Iterators
801804
/////////////////////////////////////////////////////////////////////////////
802805

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

813-
impl<T> Iterator<T> for Item<T> {
810+
impl<'a, T> Iterator<&'a T> for Iter<'a, T> {
814811
#[inline]
815-
fn next(&mut self) -> Option<T> {
816-
self.opt.take()
812+
fn next(&mut self) -> Option<&'a T> { self.inner.take() }
813+
#[inline]
814+
fn size_hint(&self) -> (uint, Option<uint>) {
815+
let n = if self.inner.is_some() {1} else {0};
816+
(n, Some(n))
817817
}
818+
}
819+
820+
impl<'a, T> DoubleEndedIterator<&'a T> for Iter<'a, T> {
821+
#[inline]
822+
fn next_back(&mut self) -> Option<&'a T> { self.inner.take() }
823+
}
824+
825+
impl<'a, T> ExactSizeIterator<&'a T> for Iter<'a, T> {}
826+
827+
impl<'a, T> Clone for Iter<'a, T> {
828+
fn clone(&self) -> Iter<'a, T> { Iter { inner: self.inner } }
829+
}
830+
831+
/// An iterator over a mutable reference to the `Ok` variant of a `Result`.
832+
#[stable]
833+
pub struct IterMut<'a, T: 'a> { inner: Option<&'a mut T> }
818834

835+
impl<'a, T> Iterator<&'a mut T> for IterMut<'a, T> {
836+
#[inline]
837+
fn next(&mut self) -> Option<&'a mut T> { self.inner.take() }
819838
#[inline]
820839
fn size_hint(&self) -> (uint, Option<uint>) {
821-
match self.opt {
822-
Some(_) => (1, Some(1)),
823-
None => (0, Some(0)),
824-
}
840+
let n = if self.inner.is_some() {1} else {0};
841+
(n, Some(n))
825842
}
826843
}
827844

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

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

837873
/////////////////////////////////////////////////////////////////////////////
838874
// FromIterator

0 commit comments

Comments
 (0)