about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDaniel Micay <danielmicay@gmail.com>2013-01-13 11:09:09 -0500
committerDaniel Micay <danielmicay@gmail.com>2013-01-13 11:09:09 -0500
commitac85bf356f3674ed0e0e79668b384141d38e0efe (patch)
tree0d14d2b33a071e32cb34b3222b362216a1937f94
parent3c6da7761bf74c54c1d56ba85f4ef55004abcb0a (diff)
add Option methods for in-place mutation
-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 3fb2acd3480..40c6315bfe9 100644
--- a/src/libcore/option.rs
+++ b/src/libcore/option.rs
@@ -284,6 +284,22 @@ impl<T> Option<T> {
         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) }