]> git.lizzy.rs Git - rust.git/commitdiff
Implement `map_mut` on the Option type
authorAlex Crichton <alex@alexcrichton.com>
Sun, 30 Jun 2013 05:19:32 +0000 (22:19 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Sun, 30 Jun 2013 05:21:37 +0000 (22:21 -0700)
Closes #7394

src/libstd/option.rs

index 643812312582e30d33bdb66713f52ac133e329e3..fb9962f8a44ec7a5f6d4bb4a5c23226abae3d408 100644 (file)
@@ -161,7 +161,7 @@ pub fn chain_ref<'a, U>(&'a self, f: &fn(x: &'a T) -> Option<U>)
 
     /// Filters an optional value using given function.
     #[inline(always)]
-    pub fn filtered<'a>(self, f: &fn(t: &'a T) -> bool) -> Option<T> {
+    pub fn filtered(self, f: &fn(t: &T) -> bool) -> Option<T> {
         match self {
             Some(x) => if(f(&x)) {Some(x)} else {None},
             None => None
@@ -170,10 +170,16 @@ pub fn filtered<'a>(self, f: &fn(t: &'a T) -> bool) -> Option<T> {
 
     /// Maps a `some` value from one type to another by reference
     #[inline]
-    pub fn map<'a, U>(&self, f: &fn(&'a T) -> U) -> Option<U> {
+    pub fn map<'a, U>(&'a self, f: &fn(&'a T) -> U) -> Option<U> {
         match *self { Some(ref x) => Some(f(x)), None => None }
     }
 
+    /// Maps a `some` value from one type to another by a mutable reference
+    #[inline]
+    pub fn map_mut<'a, U>(&'a mut self, f: &fn(&'a mut T) -> U) -> Option<U> {
+        match *self { Some(ref mut x) => Some(f(x)), None => None }
+    }
+
     /// As `map`, but consumes the option and gives `f` ownership to avoid
     /// copying.
     #[inline]