Skip to content

Commit 2bd104f

Browse files
huntiepnikomatsakis
authored andcommitted
Impl Try for Option
1 parent 0e6f4cf commit 2bd104f

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/libcore/option.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
#![stable(feature = "rust1", since = "1.0.0")]
147147

148148
use iter::{FromIterator, FusedIterator, TrustedLen};
149-
use mem;
149+
use {mem, ops};
150150

151151
// Note that this is not a lang item per se, but it has a hidden dependency on
152152
// `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> {
11231123
}
11241124
}
11251125
}
1126+
1127+
/// The `Option` type. See [the module level documentation](index.html) for more.
1128+
#[unstable(feature = "try_trait", issue = "42327")]
1129+
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
1130+
pub struct Missing;
1131+
1132+
#[unstable(feature = "try_trait", issue = "42327")]
1133+
impl<T> ops::Try for Option<T> {
1134+
type Ok = T;
1135+
type Error = Missing;
1136+
1137+
fn into_result(self) -> Result<T, Missing> {
1138+
self.ok_or(Missing)
1139+
}
1140+
1141+
fn from_ok(v: T) -> Self {
1142+
Some(v)
1143+
}
1144+
1145+
fn from_error(_: Missing) -> Self {
1146+
None
1147+
}
1148+
}

0 commit comments

Comments
 (0)