Skip to content

Commit 5cc3b4c

Browse files
committed
---
yaml --- r: 24539 b: refs/heads/try2 c: e000d1d h: refs/heads/master i: 24537: 6abe5b6 24535: 7543aeb v: v3
1 parent b58c8b0 commit 5cc3b4c

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: af38333f75416e8599d6534ded7d73ae121f3207
8+
refs/heads/try2: e000d1db0ab047b8d2949de4ab221718905ce3b1
99
refs/heads/incoming: 05543fd04dfb3f63b453a331e239ceb1a9a219f9
1010
refs/heads/dist-snap: 2f32a1581f522e524009138b33b1c7049ced668d
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596

branches/try2/src/libcore/option.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ pure fn get<T: copy>(opt: option<T>) -> T {
2525
alt opt { some(x) { ret x; } none { fail "option none"; } }
2626
}
2727

28+
pure fn expect<T: copy>(opt: option<T>, reason: str) -> T {
29+
#[doc = "
30+
Gets the value out of an option, printing a specified message on failure
31+
32+
# Failure
33+
34+
Fails if the value equals `none`
35+
"];
36+
alt opt { some(x) { x } none { fail reason; } }
37+
}
38+
2839
pure fn map<T, U: copy>(opt: option<T>, f: fn(T) -> U) -> option<U> {
2940
#[doc = "Maps a `some` value from one type to another"];
3041

@@ -94,18 +105,18 @@ impl extensions<T> for option<T> {
94105
Update an optional value by optionally running its content through a
95106
function that returns an option.
96107
"]
97-
fn chain<U>(f: fn(T) -> option<U>) -> option<U> { chain(self, f) }
108+
pure fn chain<U>(f: fn(T) -> option<U>) -> option<U> { chain(self, f) }
98109
#[doc = "Applies a function to the contained value or returns a default"]
99-
fn map_default<U: copy>(def: U, f: fn(T) -> U) -> U
110+
pure fn map_default<U: copy>(def: U, f: fn(T) -> U) -> U
100111
{ map_default(self, def, f) }
101112
#[doc = "Performs an operation on the contained value or does nothing"]
102-
fn iter(f: fn(T)) { iter(self, f) }
113+
pure fn iter(f: fn(T)) { iter(self, f) }
103114
#[doc = "Returns true if the option equals `none`"]
104-
fn is_none() -> bool { is_none(self) }
115+
pure fn is_none() -> bool { is_none(self) }
105116
#[doc = "Returns true if the option contains some value"]
106-
fn is_some() -> bool { is_some(self) }
117+
pure fn is_some() -> bool { is_some(self) }
107118
#[doc = "Maps a `some` value from one type to another"]
108-
fn map<U:copy>(f: fn(T) -> U) -> option<U> { map(self, f) }
119+
pure fn map<U:copy>(f: fn(T) -> U) -> option<U> { map(self, f) }
109120
}
110121

111122
impl extensions<T: copy> for option<T> {
@@ -116,8 +127,16 @@ impl extensions<T: copy> for option<T> {
116127
117128
Fails if the value equals `none`
118129
"]
119-
fn get() -> T { get(self) }
120-
fn get_default(def: T) -> T { get_default(self, def) }
130+
pure fn get() -> T { get(self) }
131+
pure fn get_default(def: T) -> T { get_default(self, def) }
132+
#[doc = "
133+
Gets the value out of an option, printing a specified message on failure
134+
135+
# Failure
136+
137+
Fails if the value equals `none`
138+
"]
139+
pure fn expect<T: copy>(reason: str) -> T { expect(self, reason) }
121140
}
122141

123142
#[test]

0 commit comments

Comments
 (0)