From 2bd104fd4ffaf2a72799b5d49fcea3931e6a9e15 Mon Sep 17 00:00:00 2001 From: Hunter Praska Date: Wed, 7 Jun 2017 22:51:45 -0500 Subject: Impl Try for Option --- src/libcore/option.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/libcore') 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> FromIterator> for Option { } } } + +/// 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 ops::Try for Option { + type Ok = T; + type Error = Missing; + + fn into_result(self) -> Result { + self.ok_or(Missing) + } + + fn from_ok(v: T) -> Self { + Some(v) + } + + fn from_error(_: Missing) -> Self { + None + } +} -- cgit 1.4.1-3-g733a5