提交 6cf2f894 编写于 作者: B Ben Blum

Implement option::unwrap safely...!

上级 bed37a1d
......@@ -44,13 +44,14 @@ enum option<T> {
}
pure fn expect<T: copy>(opt: option<T>, reason: ~str) -> T {
#[doc = "
Gets the value out of an option, printing a specified message on failure
# Failure
Fails if the value equals `none`
"];
/*!
* Gets the value out of an option, printing a specified message on
* failure
*
* # Failure
*
* Fails if the value equals `none`
*/
match opt { some(x) => x, none => fail reason }
}
......@@ -165,15 +166,9 @@ enum option<T> {
* Useful primarily for getting strings, vectors and unique pointers out
* of option types without copying them.
*/
unsafe {
let addr = match opt {
some(x) => ptr::addr_of(x),
none => fail ~"option::unwrap none"
};
let liberated_value = unsafe::reinterpret_cast(*addr);
unsafe::forget(opt);
return liberated_value;
match move opt {
some(move x) => x,
none => fail ~"option::unwrap none"
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册