about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorKevin Ballard <kevin@sb.org>2013-08-01 15:18:10 -0700
committerDaniel Micay <danielmicay@gmail.com>2013-08-03 03:11:11 -0400
commit54e685d4fd70eeb607668fed2026ac6cafec6107 (patch)
tree7b3ec24758491399314eb2d1afbbe764d5299fbc /src/libstd
parent1f9c392389de3de5a65e825413ab9503549db56a (diff)
downloadrust-54e685d4fd70eeb607668fed2026ac6cafec6107.tar.gz
rust-54e685d4fd70eeb607668fed2026ac6cafec6107.zip
option: mutate() and mutate_default() should return bool
Fixes #8047.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/option.rs29
1 files changed, 24 insertions, 5 deletions
diff --git a/src/libstd/option.rs b/src/libstd/option.rs
index 7eca47743d6..547c453b02d 100644
--- a/src/libstd/option.rs
+++ b/src/libstd/option.rs
@@ -235,19 +235,24 @@ impl<T> Option<T> {
         self.take().map_consume_default(def, blk)
     }
 
-    /// Apply a function to the contained value or do nothing
-    pub fn mutate(&mut self, f: &fn(T) -> T) {
+    /// Apply a function to the contained value or do nothing.
+    /// Returns true if the contained value was mutated.
+    pub fn mutate(&mut self, f: &fn(T) -> T) -> bool {
         if self.is_some() {
             *self = Some(f(self.take_unwrap()));
-        }
+            true
+        } else { false }
     }
 
-    /// Apply a function to the contained value or set it to a default
-    pub fn mutate_default(&mut self, def: T, f: &fn(T) -> T) {
+    /// Apply a function to the contained value or set it to a default.
+    /// Returns true if the contained value was mutated, or false if set to the default.
+    pub fn mutate_default(&mut self, def: T, f: &fn(T) -> T) -> bool {
         if self.is_some() {
             *self = Some(f(self.take_unwrap()));
+            true
         } else {
             *self = Some(def);
+            false
         }
     }
 
@@ -575,4 +580,18 @@ mod tests {
         assert_eq!(it.size_hint(), (0, Some(0)));
         assert!(it.next().is_none());
     }
+
+    #[test]
+    fn test_mutate() {
+        let mut x = Some(3i);
+        assert!(x.mutate(|i| i+1));
+        assert_eq!(x, Some(4i));
+        assert!(x.mutate_default(0, |i| i+1));
+        assert_eq!(x, Some(5i));
+        x = None;
+        assert!(!x.mutate(|i| i+1));
+        assert_eq!(x, None);
+        assert!(!x.mutate_default(0i, |i| i+1));
+        assert_eq!(x, Some(0i));
+    }
 }