mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-13 07:24:00 +00:00
core: add result::unwrap() .. patch from @nmatsakis
This commit is contained in:
parent
3d8f7d644b
commit
082a95a077
@ -216,6 +216,21 @@ fn iter2<S,T,U:copy>(ss: [S], ts: [T],
|
|||||||
ret ok(());
|
ret ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc="
|
||||||
|
Unwraps a result, assuming it is an `ok(T)`
|
||||||
|
|
||||||
|
This operation is unsafe.
|
||||||
|
"]
|
||||||
|
fn unwrap<T, U>(-res: result<T, U>) -> T unsafe {
|
||||||
|
let addr = alt res {
|
||||||
|
ok(x) { ptr::addr_of(x) }
|
||||||
|
err(_) { fail "option none" }
|
||||||
|
};
|
||||||
|
let liberated_value = unsafe::reinterpret_cast(*addr);
|
||||||
|
unsafe::forget(res);
|
||||||
|
ret liberated_value;
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
fn op1() -> result::result<int, str> { result::ok(666) }
|
fn op1() -> result::result<int, str> { result::ok(666) }
|
||||||
|
Loading…
Reference in New Issue
Block a user