about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorTim Chevalier <chevalier@alum.wellesley.edu>2013-01-13 14:29:05 -0800
committerTim Chevalier <chevalier@alum.wellesley.edu>2013-01-13 14:29:05 -0800
commit7eae397e58641392c20b4baefb39257264f7dcde (patch)
tree69e7287234622868d9bc555cba8ff005f90c7b33 /src/libcore
parent50cd3c18f5cb07f5c12ad6f1cbe872f5971fec32 (diff)
parentac85bf356f3674ed0e0e79668b384141d38e0efe (diff)
downloadrust-7eae397e58641392c20b4baefb39257264f7dcde.tar.gz
rust-7eae397e58641392c20b4baefb39257264f7dcde.zip
Merge pull request #4465 from thestinger/option
improvements to option module
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/option.rs43
1 files changed, 42 insertions, 1 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs
index 414027bb77e..48f2ae71938 100644
--- a/src/libcore/option.rs
+++ b/src/libcore/option.rs
@@ -118,7 +118,7 @@ pub pure fn map_consume<T, U>(opt: Option<T>,
      * As `map`, but consumes the option and gives `f` ownership to avoid
      * copying.
      */
-    if opt.is_some() { Some(f(option::unwrap(move opt))) } else { None }
+    match opt { None => None, Some(v) => Some(f(v)) }
 }
 
 #[inline(always)]
@@ -278,12 +278,42 @@ impl<T> Option<T> {
     #[inline(always)]
     pure fn map<U>(&self, f: fn(x: &T) -> U) -> Option<U> { map(self, f) }
 
+    /// As `map`, but consumes the option and gives `f` ownership to avoid
+    /// copying.
+    #[inline(always)]
+    pure fn map_consume<U>(self, f: fn(v: T) -> U) -> Option<U> {
+        map_consume(self, f)
+    }
+
     /// Applies a function to the contained value or returns a default
     #[inline(always)]
     pure fn map_default<U>(&self, def: U, f: fn(x: &T) -> U) -> U {
         map_default(self, move def, f)
     }
 
+    /// As `map_default`, but consumes the option and gives `f`
+    /// ownership to avoid copying.
+    #[inline(always)]
+    pure fn map_consume_default<U>(self, def: U, f: fn(v: T) -> U) -> U {
+        match self { None => def, Some(v) => f(v) }
+    }
+
+    /// Apply a function to the contained value or do nothing
+    fn mutate(&mut self, f: fn(T) -> T) {
+        if self.is_some() {
+            *self = Some(f(self.swap_unwrap()));
+        }
+    }
+
+    /// Apply a function to the contained value or set it to a default
+    fn mutate_default(&mut self, def: T, f: fn(T) -> T) {
+        if self.is_some() {
+            *self = Some(f(self.swap_unwrap()));
+        } else {
+            *self = Some(def);
+        }
+    }
+
     /// Performs an operation on the contained value by reference
     #[inline(always)]
     pure fn iter(&self, f: fn(x: &T)) { iter(self, f) }
@@ -316,6 +346,17 @@ impl<T> Option<T> {
     pure fn unwrap(self) -> T { unwrap(self) }
 
     /**
+     * The option dance. Moves a value out of an option type and returns it,
+     * replacing the original with `None`.
+     *
+     * # Failure
+     *
+     * Fails if the value equals `None`.
+     */
+    #[inline(always)]
+    fn swap_unwrap(&mut self) -> T { swap_unwrap(self) }
+
+    /**
      * Gets the value out of an option, printing a specified message on
      * failure
      *