Skip to content

Commit 082a95a

Browse files
olsonjefferybrson
authored andcommitted
core: add result::unwrap() .. patch from @nmatsakis
1 parent 3d8f7d6 commit 082a95a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/libcore/result.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,21 @@ fn iter2<S,T,U:copy>(ss: [S], ts: [T],
216216
ret ok(());
217217
}
218218

219+
#[doc="
220+
Unwraps a result, assuming it is an `ok(T)`
221+
222+
This operation is unsafe.
223+
"]
224+
fn unwrap<T, U>(-res: result<T, U>) -> T unsafe {
225+
let addr = alt res {
226+
ok(x) { ptr::addr_of(x) }
227+
err(_) { fail "option none" }
228+
};
229+
let liberated_value = unsafe::reinterpret_cast(*addr);
230+
unsafe::forget(res);
231+
ret liberated_value;
232+
}
233+
219234
#[cfg(test)]
220235
mod tests {
221236
fn op1() -> result::result<int, str> { result::ok(666) }

0 commit comments

Comments
 (0)