about summary refs log tree commit diff
path: root/src/libcore/option.rs
diff options
context:
space:
mode:
authorHunter Praska <hunter@wiggin-labs.com>2017-06-07 22:51:45 -0500
committerNiko Matsakis <niko@alum.mit.edu>2017-09-27 17:56:15 -0400
commit2bd104fd4ffaf2a72799b5d49fcea3931e6a9e15 (patch)
treee55a3eee6afaeec90b596a606099cd294d6875c6 /src/libcore/option.rs
parent0e6f4cf51cd3b799fb057956f8e733d16605d09b (diff)
downloadrust-2bd104fd4ffaf2a72799b5d49fcea3931e6a9e15.tar.gz
rust-2bd104fd4ffaf2a72799b5d49fcea3931e6a9e15.zip
Impl Try for Option
Diffstat (limited to 'src/libcore/option.rs')
-rw-r--r--src/libcore/option.rs25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs
index 138e04c7737..fa6e3095479 100644
--- a/src/libcore/option.rs
+++ b/src/libcore/option.rs
@@ -146,7 +146,7 @@
 #![stable(feature = "rust1", since = "1.0.0")]
 
 use iter::{FromIterator, FusedIterator, TrustedLen};
-use mem;
+use {mem, ops};
 
 // Note that this is not a lang item per se, but it has a hidden dependency on
 // `Iterator`, which is one. The compiler assumes that the `next` method of
@@ -1123,3 +1123,26 @@ impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
         }
     }
 }
+
+/// The `Option` type. See [the module level documentation](index.html) for more.
+#[unstable(feature = "try_trait", issue = "42327")]
+#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
+pub struct Missing;
+
+#[unstable(feature = "try_trait", issue = "42327")]
+impl<T> ops::Try for Option<T> {
+    type Ok = T;
+    type Error = Missing;
+
+    fn into_result(self) -> Result<T, Missing> {
+        self.ok_or(Missing)
+    }
+
+    fn from_ok(v: T) -> Self {
+        Some(v)
+    }
+
+    fn from_error(_: Missing) -> Self {
+        None
+    }
+}