From 082a95a0777d03fd7469b8df691e9caa8217f8ac Mon Sep 17 00:00:00 2001 From: Jeff Olson Date: Wed, 2 May 2012 16:19:30 -0700 Subject: [PATCH] core: add result::unwrap() .. patch from @nmatsakis --- src/libcore/result.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index a3a889d94da..a8aa250ddad 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -216,6 +216,21 @@ fn iter2(ss: [S], ts: [T], ret ok(()); } +#[doc=" +Unwraps a result, assuming it is an `ok(T)` + +This operation is unsafe. +"] +fn unwrap(-res: result) -> 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)] mod tests { fn op1() -> result::result { result::ok(666) }