From 0e3825d38cc459f9fcb24f1e95d4f6e867265300 Mon Sep 17 00:00:00 2001 From: Ben Blum Date: Thu, 16 Aug 2012 21:06:05 -0400 Subject: [PATCH] Add option::get_ref --- src/libcore/option.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 2f86e1be495..e677b9bde00 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -29,6 +29,20 @@ pure fn get(opt: option) -> T { } } +pure fn get_ref(opt: &option) -> &T { + /*! + * Gets an immutable reference to the value inside an option. + * + * # Failure + * + * Fails if the value equals `none` + */ + match *opt { + some(ref x) => x, + none => fail ~"option::get_ref none" + } +} + pure fn expect(opt: option, reason: ~str) -> T { #[doc = " Gets the value out of an option, printing a specified message on failure @@ -201,6 +215,8 @@ impl &option { pure fn iter_ref(f: fn(x: &T)) { iter_ref(self, f) } /// Maps a `some` value from one type to another by reference pure fn map_ref(f: fn(x: &T) -> U) -> option { map_ref(self, f) } + /// Gets an immutable reference to the value inside a `some`. + pure fn get_ref() -> &self/T { get_ref(self) } } impl option {