Skip to content

Commit 77b8144

Browse files
committed
libcore: Implement result::get_ref.
This can be more efficient than unwrapping for large structural types.
1 parent 62be878 commit 77b8144

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/libcore/result.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ pure fn get<T: copy, U>(res: result<T, U>) -> T {
2626
}
2727
}
2828

29+
/**
30+
* Get a reference to the value out of a successful result
31+
*
32+
* # Failure
33+
*
34+
* If the result is an error
35+
*/
36+
pure fn get_ref<T, U>(res: &a/result<T, U>) -> &a/T {
37+
match *res {
38+
ok(ref t) => t,
39+
err(ref the_err) => unchecked {
40+
fail fmt!("get_ref called on error result: %?", the_err)
41+
}
42+
}
43+
}
44+
2945
/**
3046
* Get the value out of an error result
3147
*

0 commit comments

Comments
 (0)