From 77b8144295fdc19bf7700e3db9551fd190ecebcb Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Sun, 26 Aug 2012 11:50:21 -0700 Subject: [PATCH] libcore: Implement result::get_ref. This can be more efficient than unwrapping for large structural types. --- src/libcore/result.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 9b8482c5b5c2..e916a28ea79d 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -26,6 +26,22 @@ pure fn get(res: result) -> T { } } +/** + * Get a reference to the value out of a successful result + * + * # Failure + * + * If the result is an error + */ +pure fn get_ref(res: &a/result) -> &a/T { + match *res { + ok(ref t) => t, + err(ref the_err) => unchecked { + fail fmt!("get_ref called on error result: %?", the_err) + } + } +} + /** * Get the value out of an error result *