#![stable]
use self::Option::*;
use cmp::{Eq, Ord};
use default::Default;
use iter::{Iterator, IteratorExt, DoubleEndedIterator, FromIterator};
use iter::{ExactSizeIterator};
use mem;
use result::Result;
use result::Result::{Ok, Err};
use slice;
use slice::AsSlice;
use clone::Clone;
use ops::{Deref, FnOnce};
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Show, Hash)]
#[stable]
pub enum Option<T> {
#[stable]
None,
#[stable]
Some(T)
}
impl<T> Option<T> {
#[inline]
#[stable]
pub fn is_some(&self) -> bool {
match *self {
Some(_) => true,
None => false
}
}
#[inline]
#[stable]
pub fn is_none(&self) -> bool {
!self.is_some()
}
#[inline]
#[stable]
pub fn as_ref<'r>(&'r self) -> Option<&'r T> {
match *self {
Some(ref x) => Some(x),
None => None
}
}
#[inline]
#[stable]
pub fn as_mut<'r>(&'r mut self) -> Option<&'r mut T> {
match *self {
Some(ref mut x) => Some(x),
None => None
}
}
#[inline]
#[unstable = "waiting for mut conventions"]
pub fn as_mut_slice<'r>(&'r mut self) -> &'r mut [T] {
match *self {
Some(ref mut x) => {
let result: &mut [T] = slice::mut_ref_slice(x);
result
}
None => {
let result: &mut [T] = &mut [];
result
}
}
}
#[inline]
#[stable]
pub fn expect(self, msg: &str) -> T {
match self {
Some(val) => val,
None => panic!("{}", msg),
}
}
#[inline]
#[stable]
pub fn unwrap(self) -> T {
match self {
Some(val) => val,
None => panic!("called `Option::unwrap()` on a `None` value"),
}
}
#[inline]
#[stable]
pub fn unwrap_or(self, def: T) -> T {
match self {
Some(x) => x,
None => def
}
}
#[inline]
#[stable]
pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T {
match self {
Some(x) => x,
None => f()
}
}
#[inline]
#[stable]
pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> {
match self {
Some(x) => Some(f(x)),
None => None
}
}
#[inline]
#[stable]
pub fn map_or<U, F: FnOnce(T) -> U>(self, def: U, f: F) -> U {
match self {
Some(t) => f(t),
None => def
}
}
#[inline]
#[stable]
pub fn map_or_else<U, D: FnOnce() -> U, F: FnOnce(T) -> U>(self, def: D, f: F) -> U {
match self {
Some(t) => f(t),
None => def()
}
}
#[inline]
#[experimental]
pub fn ok_or<E>(self, err: E) -> Result<T, E> {
match self {
Some(v) => Ok(v),
None => Err(err),
}
}
#[inline]
#[experimental]
pub fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<T, E> {
match self {
Some(v) => Ok(v),
None => Err(err()),
}
}
#[inline]
#[stable]
pub fn iter(&self) -> Iter<T> {
Iter { inner: Item { opt: self.as_ref() } }
}
#[inline]
#[unstable = "waiting for iterator conventions"]
pub fn iter_mut(&mut self) -> IterMut<T> {
IterMut { inner: Item { opt: self.as_mut() } }
}
#[inline]
#[stable]
pub fn into_iter(self) -> IntoIter<T> {
IntoIter { inner: Item { opt: self } }
}
#[inline]
#[stable]
pub fn and<U>(self, optb: Option<U>) -> Option<U> {
match self {
Some(_) => optb,
None => None,
}
}
#[inline]
#[stable]
pub fn and_then<U, F: FnOnce(T) -> Option<U>>(self, f: F) -> Option<U> {
match self {
Some(x) => f(x),
None => None,
}
}
#[inline]
#[stable]
pub fn or(self, optb: Option<T>) -> Option<T> {
match self {
Some(_) => self,
None => optb
}
}
#[inline]
#[stable]
pub fn or_else<F: FnOnce() -> Option<T>>(self, f: F) -> Option<T> {
match self {
Some(_) => self,
None => f()
}
}
#[inline]
#[stable]
pub fn take(&mut self) -> Option<T> {
mem::replace(self, None)
}
}
impl<'a, T: Clone, D: Deref<Target=T>> Option<D> {
#[unstable = "recently added as part of collections reform"]
pub fn cloned(self) -> Option<T> {
self.map(|t| t.deref().clone())
}
}
impl<T: Default> Option<T> {
#[inline]
#[stable]
pub fn unwrap_or_default(self) -> T {
match self {
Some(x) => x,
None => Default::default()
}
}
}
#[unstable = "waiting on the stability of the trait itself"]
impl<T> AsSlice<T> for Option<T> {
#[inline]
fn as_slice<'a>(&'a self) -> &'a [T] {
match *self {
Some(ref x) => slice::ref_slice(x),
None => {
let result: &[_] = &[];
result
}
}
}
}
#[stable]
impl<T> Default for Option<T> {
#[stable]
#[inline]
#[stable]
fn default() -> Option<T> { None }
}
#[derive(Clone)]
struct Item<A> {
opt: Option<A>
}
impl<A> Iterator for Item<A> {
type Item = A;
#[inline]
fn next(&mut self) -> Option<A> {
self.opt.take()
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
match self.opt {
Some(_) => (1, Some(1)),
None => (0, Some(0)),
}
}
}
impl<A> DoubleEndedIterator for Item<A> {
#[inline]
fn next_back(&mut self) -> Option<A> {
self.opt.take()
}
}
impl<A> ExactSizeIterator for Item<A> {}
#[stable]
pub struct Iter<'a, A: 'a> { inner: Item<&'a A> }
#[stable]
impl<'a, A> Iterator for Iter<'a, A> {
type Item = &'a A;
#[inline]
fn next(&mut self) -> Option<&'a A> { self.inner.next() }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
}
#[stable]
impl<'a, A> DoubleEndedIterator for Iter<'a, A> {
#[inline]
fn next_back(&mut self) -> Option<&'a A> { self.inner.next_back() }
}
#[stable]
impl<'a, A> ExactSizeIterator for Iter<'a, A> {}
#[stable]
impl<'a, A> Clone for Iter<'a, A> {
fn clone(&self) -> Iter<'a, A> {
Iter { inner: self.inner.clone() }
}
}
#[stable]
pub struct IterMut<'a, A: 'a> { inner: Item<&'a mut A> }
#[stable]
impl<'a, A> Iterator for IterMut<'a, A> {
type Item = &'a mut A;
#[inline]
fn next(&mut self) -> Option<&'a mut A> { self.inner.next() }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
}
#[stable]
impl<'a, A> DoubleEndedIterator for IterMut<'a, A> {
#[inline]
fn next_back(&mut self) -> Option<&'a mut A> { self.inner.next_back() }
}
#[stable]
impl<'a, A> ExactSizeIterator for IterMut<'a, A> {}
#[stable]
pub struct IntoIter<A> { inner: Item<A> }
#[stable]
impl<A> Iterator for IntoIter<A> {
type Item = A;
#[inline]
fn next(&mut self) -> Option<A> { self.inner.next() }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
}
#[stable]
impl<A> DoubleEndedIterator for IntoIter<A> {
#[inline]
fn next_back(&mut self) -> Option<A> { self.inner.next_back() }
}
#[stable]
impl<A> ExactSizeIterator for IntoIter<A> {}
#[stable]
impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
#[inline]
#[stable]
fn from_iter<I: Iterator<Item=Option<A>>>(iter: I) -> Option<V> {
struct Adapter<Iter> {
iter: Iter,
found_none: bool,
}
impl<T, Iter: Iterator<Item=Option<T>>> Iterator for Adapter<Iter> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<T> {
match self.iter.next() {
Some(Some(value)) => Some(value),
Some(None) => {
self.found_none = true;
None
}
None => None,
}
}
}
let mut adapter = Adapter { iter: iter, found_none: false };
let v: V = FromIterator::from_iter(adapter.by_ref());
if adapter.found_none {
None
} else {
Some(v)
}
}
}