]> git.lizzy.rs Git - rust.git/commitdiff
option: mutate() and mutate_default() should return bool
authorKevin Ballard <kevin@sb.org>
Thu, 1 Aug 2013 22:18:10 +0000 (15:18 -0700)
committerDaniel Micay <danielmicay@gmail.com>
Sat, 3 Aug 2013 07:11:11 +0000 (03:11 -0400)
Fixes #8047.

src/librustc/middle/trans/_match.rs
src/libstd/option.rs

index 5f9351e4e12e79c66a89ee2ae01d22bb1bba57cf..9922106a66380a0003d62a0dd720b9244006529e 100644 (file)
@@ -1159,7 +1159,7 @@ fn store_non_ref_bindings(bcx: @mut Block,
                     add_clean_temp_mem(bcx, lldest, binding_info.ty);
                     temp_cleanups.push(lldest);
                     temp_cleanups
-                }
+                };
             }
             TrByRef => {}
         }
index 7eca47743d6a6136882a187134912b12f99f3f3d..547c453b02d1a143f5697cc84ecb189a601ac9c6 100644 (file)
@@ -235,19 +235,24 @@ pub fn take_map_default<U> (&mut self, def: U, blk: &fn(T) -> U) -> U {
         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 @@ fn test_mut_iter() {
         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));
+    }
 }