Skip to content

Commit 6cf2f89

Browse files
committed
Implement option::unwrap safely...!
1 parent bed37a1 commit 6cf2f89

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

src/libcore/option.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ pure fn get_ref<T>(opt: &r/option<T>) -> &r/T {
4444
}
4545

4646
pure fn expect<T: copy>(opt: option<T>, reason: ~str) -> T {
47-
#[doc = "
48-
Gets the value out of an option, printing a specified message on failure
49-
50-
# Failure
51-
52-
Fails if the value equals `none`
53-
"];
47+
/*!
48+
* Gets the value out of an option, printing a specified message on
49+
* failure
50+
*
51+
* # Failure
52+
*
53+
* Fails if the value equals `none`
54+
*/
5455
match opt { some(x) => x, none => fail reason }
5556
}
5657

@@ -165,15 +166,9 @@ pure fn unwrap<T>(+opt: option<T>) -> T {
165166
* Useful primarily for getting strings, vectors and unique pointers out
166167
* of option types without copying them.
167168
*/
168-
169-
unsafe {
170-
let addr = match opt {
171-
some(x) => ptr::addr_of(x),
172-
none => fail ~"option::unwrap none"
173-
};
174-
let liberated_value = unsafe::reinterpret_cast(*addr);
175-
unsafe::forget(opt);
176-
return liberated_value;
169+
match move opt {
170+
some(move x) => x,
171+
none => fail ~"option::unwrap none"
177172
}
178173
}
179174

0 commit comments

Comments
 (0)