Skip to content

Commit b924f1b

Browse files
committed
Merge pull request #19849 from alexcrichton/second-pass-option
std: Fully stabilize Option<T> Reviewed-by: aturon
2 parents fec4f19 + 1fbca88 commit b924f1b

File tree

3 files changed

+93
-32
lines changed

3 files changed

+93
-32
lines changed

src/libcore/option.rs

+87-26
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ use ops::{Deref, FnOnce};
168168
#[stable]
169169
pub enum Option<T> {
170170
/// No value
171+
#[stable]
171172
None,
172173
/// Some value `T`
174+
#[stable]
173175
Some(T)
174176
}
175177

@@ -261,7 +263,7 @@ impl<T> Option<T> {
261263
/// assert_eq!(x, Some(42u));
262264
/// ```
263265
#[inline]
264-
#[unstable = "waiting for mut conventions"]
266+
#[stable]
265267
pub fn as_mut<'r>(&'r mut self) -> Option<&'r mut T> {
266268
match *self {
267269
Some(ref mut x) => Some(x),
@@ -321,7 +323,7 @@ impl<T> Option<T> {
321323
/// x.expect("the world is ending"); // panics with `world is ending`
322324
/// ```
323325
#[inline]
324-
#[unstable = "waiting for conventions"]
326+
#[stable]
325327
pub fn expect(self, msg: &str) -> T {
326328
match self {
327329
Some(val) => val,
@@ -353,7 +355,7 @@ impl<T> Option<T> {
353355
/// assert_eq!(x.unwrap(), "air"); // fails
354356
/// ```
355357
#[inline]
356-
#[unstable = "waiting for conventions"]
358+
#[stable]
357359
pub fn unwrap(self) -> T {
358360
match self {
359361
Some(val) => val,
@@ -370,7 +372,7 @@ impl<T> Option<T> {
370372
/// assert_eq!(None.unwrap_or("bike"), "bike");
371373
/// ```
372374
#[inline]
373-
#[unstable = "waiting for conventions"]
375+
#[stable]
374376
pub fn unwrap_or(self, def: T) -> T {
375377
match self {
376378
Some(x) => x,
@@ -388,7 +390,7 @@ impl<T> Option<T> {
388390
/// assert_eq!(None.unwrap_or_else(|| 2 * k), 20u);
389391
/// ```
390392
#[inline]
391-
#[unstable = "waiting for conventions"]
393+
#[stable]
392394
pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T {
393395
match self {
394396
Some(x) => x,
@@ -412,7 +414,7 @@ impl<T> Option<T> {
412414
/// let num_as_int: Option<uint> = num_as_str.map(|n| n.len());
413415
/// ```
414416
#[inline]
415-
#[unstable = "waiting for unboxed closures"]
417+
#[stable]
416418
pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> {
417419
match self {
418420
Some(x) => Some(f(x)),
@@ -432,7 +434,7 @@ impl<T> Option<T> {
432434
/// assert_eq!(x.map_or(42u, |v| v.len()), 42u);
433435
/// ```
434436
#[inline]
435-
#[unstable = "waiting for unboxed closures"]
437+
#[stable]
436438
pub fn map_or<U, F: FnOnce(T) -> U>(self, def: U, f: F) -> U {
437439
match self {
438440
Some(t) => f(t),
@@ -454,7 +456,7 @@ impl<T> Option<T> {
454456
/// assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42u);
455457
/// ```
456458
#[inline]
457-
#[unstable = "waiting for unboxed closures"]
459+
#[stable]
458460
pub fn map_or_else<U, D: FnOnce() -> U, F: FnOnce(T) -> U>(self, def: D, f: F) -> U {
459461
match self {
460462
Some(t) => f(t),
@@ -520,9 +522,9 @@ impl<T> Option<T> {
520522
/// assert_eq!(x.iter().next(), None);
521523
/// ```
522524
#[inline]
523-
#[unstable = "waiting for iterator conventions"]
524-
pub fn iter<'r>(&'r self) -> Item<&'r T> {
525-
Item{opt: self.as_ref()}
525+
#[stable]
526+
pub fn iter(&self) -> Iter<T> {
527+
Iter { inner: Item { opt: self.as_ref() } }
526528
}
527529

528530
/// Returns a mutable iterator over the possibly contained value.
@@ -542,8 +544,8 @@ impl<T> Option<T> {
542544
/// ```
543545
#[inline]
544546
#[unstable = "waiting for iterator conventions"]
545-
pub fn iter_mut<'r>(&'r mut self) -> Item<&'r mut T> {
546-
Item{opt: self.as_mut()}
547+
pub fn iter_mut(&mut self) -> IterMut<T> {
548+
IterMut { inner: Item { opt: self.as_mut() } }
547549
}
548550

549551
/// Returns a consuming iterator over the possibly contained value.
@@ -560,9 +562,9 @@ impl<T> Option<T> {
560562
/// assert!(v.is_empty());
561563
/// ```
562564
#[inline]
563-
#[unstable = "waiting for iterator conventions"]
564-
pub fn into_iter(self) -> Item<T> {
565-
Item{opt: self}
565+
#[stable]
566+
pub fn into_iter(self) -> IntoIter<T> {
567+
IntoIter { inner: Item { opt: self } }
566568
}
567569

568570
/////////////////////////////////////////////////////////////////////////
@@ -614,7 +616,7 @@ impl<T> Option<T> {
614616
/// assert_eq!(None.and_then(sq).and_then(sq), None);
615617
/// ```
616618
#[inline]
617-
#[unstable = "waiting for unboxed closures"]
619+
#[stable]
618620
pub fn and_then<U, F: FnOnce(T) -> Option<U>>(self, f: F) -> Option<U> {
619621
match self {
620622
Some(x) => f(x),
@@ -666,7 +668,7 @@ impl<T> Option<T> {
666668
/// assert_eq!(None.or_else(nobody), None);
667669
/// ```
668670
#[inline]
669-
#[unstable = "waiting for unboxed closures"]
671+
#[stable]
670672
pub fn or_else<F: FnOnce() -> Option<T>>(self, f: F) -> Option<T> {
671673
match self {
672674
Some(_) => self,
@@ -731,7 +733,7 @@ impl<T: Default> Option<T> {
731733
/// assert_eq!(0i, bad_year);
732734
/// ```
733735
#[inline]
734-
#[unstable = "waiting for conventions"]
736+
#[stable]
735737
pub fn unwrap_or_default(self) -> T {
736738
match self {
737739
Some(x) => x,
@@ -744,6 +746,7 @@ impl<T: Default> Option<T> {
744746
// Trait implementations
745747
/////////////////////////////////////////////////////////////////////////////
746748

749+
#[unstable = "waiting on the stability of the trait itself"]
747750
impl<T> AsSlice<T> for Option<T> {
748751
/// Convert from `Option<T>` to `&[T]` (without copying)
749752
#[inline]
@@ -761,20 +764,16 @@ impl<T> AsSlice<T> for Option<T> {
761764
#[stable]
762765
impl<T> Default for Option<T> {
763766
#[inline]
767+
#[stable]
764768
fn default() -> Option<T> { None }
765769
}
766770

767771
/////////////////////////////////////////////////////////////////////////////
768-
// The Option Iterator
772+
// The Option Iterators
769773
/////////////////////////////////////////////////////////////////////////////
770774

771-
/// An `Option` iterator that yields either one or zero elements
772-
///
773-
/// The `Item` iterator is returned by the `iter`, `iter_mut` and `into_iter`
774-
/// methods on `Option`.
775775
#[deriving(Clone)]
776-
#[unstable = "waiting for iterator conventions"]
777-
pub struct Item<A> {
776+
struct Item<A> {
778777
opt: Option<A>
779778
}
780779

@@ -802,6 +801,66 @@ impl<A> DoubleEndedIterator<A> for Item<A> {
802801

803802
impl<A> ExactSizeIterator<A> for Item<A> {}
804803

804+
/// An iterator over a reference of the contained item in an Option.
805+
#[stable]
806+
pub struct Iter<'a, A: 'a> { inner: Item<&'a A> }
807+
808+
impl<'a, A> Iterator<&'a A> for Iter<'a, A> {
809+
#[inline]
810+
fn next(&mut self) -> Option<&'a A> { self.inner.next() }
811+
#[inline]
812+
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
813+
}
814+
815+
impl<'a, A> DoubleEndedIterator<&'a A> for Iter<'a, A> {
816+
#[inline]
817+
fn next_back(&mut self) -> Option<&'a A> { self.inner.next_back() }
818+
}
819+
820+
impl<'a, A> ExactSizeIterator<&'a A> for Iter<'a, A> {}
821+
822+
impl<'a, A> Clone for Iter<'a, A> {
823+
fn clone(&self) -> Iter<'a, A> {
824+
Iter { inner: self.inner.clone() }
825+
}
826+
}
827+
828+
/// An iterator over a mutable reference of the contained item in an Option.
829+
#[stable]
830+
pub struct IterMut<'a, A: 'a> { inner: Item<&'a mut A> }
831+
832+
impl<'a, A> Iterator<&'a mut A> for IterMut<'a, A> {
833+
#[inline]
834+
fn next(&mut self) -> Option<&'a mut A> { self.inner.next() }
835+
#[inline]
836+
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
837+
}
838+
839+
impl<'a, A> DoubleEndedIterator<&'a mut A> for IterMut<'a, A> {
840+
#[inline]
841+
fn next_back(&mut self) -> Option<&'a mut A> { self.inner.next_back() }
842+
}
843+
844+
impl<'a, A> ExactSizeIterator<&'a mut A> for IterMut<'a, A> {}
845+
846+
/// An iterator over the item contained inside an Option.
847+
#[stable]
848+
pub struct IntoIter<A> { inner: Item<A> }
849+
850+
impl<A> Iterator<A> for IntoIter<A> {
851+
#[inline]
852+
fn next(&mut self) -> Option<A> { self.inner.next() }
853+
#[inline]
854+
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
855+
}
856+
857+
impl<A> DoubleEndedIterator<A> for IntoIter<A> {
858+
#[inline]
859+
fn next_back(&mut self) -> Option<A> { self.inner.next_back() }
860+
}
861+
862+
impl<A> ExactSizeIterator<A> for IntoIter<A> {}
863+
805864
/////////////////////////////////////////////////////////////////////////////
806865
// FromIterator
807866
/////////////////////////////////////////////////////////////////////////////
@@ -826,6 +885,7 @@ impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
826885
/// assert!(res == Some(vec!(2u, 3u)));
827886
/// ```
828887
#[inline]
888+
#[stable]
829889
fn from_iter<I: Iterator<Option<A>>>(iter: I) -> Option<V> {
830890
// FIXME(#11084): This could be replaced with Iterator::scan when this
831891
// performance bug is closed.
@@ -860,5 +920,6 @@ impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
860920
}
861921
}
862922

923+
#[stable]
863924
impl<T:Copy> Copy for Option<T> {}
864925

src/librustc/metadata/encoder.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,12 @@ fn encode_enum_variant_info(ecx: &EncodeContext,
364364
}
365365
}
366366

367-
fn encode_path<PI: Iterator<PathElem> + Clone>(rbml_w: &mut Encoder,
368-
mut path: PI) {
367+
fn encode_path<PI: Iterator<PathElem>>(rbml_w: &mut Encoder, path: PI) {
368+
let path = path.collect::<Vec<_>>();
369369
rbml_w.start_tag(tag_path);
370-
rbml_w.wr_tagged_u32(tag_path_len, path.clone().count() as u32);
371-
for pe in path {
372-
let tag = match pe {
370+
rbml_w.wr_tagged_u32(tag_path_len, path.len() as u32);
371+
for pe in path.iter() {
372+
let tag = match *pe {
373373
ast_map::PathMod(_) => tag_path_elem_mod,
374374
ast_map::PathName(_) => tag_path_elem_name
375375
};

src/librustc_driver/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl FromStr for UserIdentifiedItem {
325325
}
326326

327327
enum NodesMatchingUII<'a, 'ast: 'a> {
328-
NodesMatchingDirect(option::Item<ast::NodeId>),
328+
NodesMatchingDirect(option::IntoIter<ast::NodeId>),
329329
NodesMatchingSuffix(ast_map::NodesMatchingSuffix<'a, 'ast, String>),
330330
}
331331

0 commit comments

Comments
 (0)