about summary refs log tree commit diff
path: root/src/libcore/option.rs
diff options
context:
space:
mode:
authorBen Blum <bblum@andrew.cmu.edu>2012-08-16 21:06:05 -0400
committerBen Blum <bblum@andrew.cmu.edu>2012-08-16 21:07:55 -0400
commit0e3825d38cc459f9fcb24f1e95d4f6e867265300 (patch)
tree503876b8bcd1f96a8829ab63a4842d3b592ca72d /src/libcore/option.rs
parenta076c287de55bd69328fbf542bf769ca20a12ab4 (diff)
Add option::get_ref
Diffstat (limited to 'src/libcore/option.rs')
-rw-r--r--src/libcore/option.rs16
1 files changed, 16 insertions, 0 deletions
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<T: copy>(opt: option<T>) -> T {
     }
 }
 
+pure fn get_ref<T>(opt: &option<T>) -> &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<T: copy>(opt: option<T>, reason: ~str) -> T {
     #[doc = "
     Gets the value out of an option, printing a specified message on failure
@@ -201,6 +215,8 @@ impl<T> &option<T> {
     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<U>(f: fn(x: &T) -> U) -> option<U> { map_ref(self, f) }
+    /// Gets an immutable reference to the value inside a `some`.
+    pure fn get_ref() -> &self/T { get_ref(self) }
 }
 
 impl<T: copy> option<T> {