rust/land2d/src/lib.rs
changeset 13945 1c30793b1cea
parent 13943 1fa905aa4cdb
child 13948 a325ed57ebfe
--- a/rust/land2d/src/lib.rs	Thu Oct 18 06:50:21 2018 +0300
+++ b/rust/land2d/src/lib.rs	Thu Oct 18 07:27:14 2018 +0300
@@ -50,22 +50,17 @@
     }
 
     #[inline]
-    pub fn get_mut(&mut self, y: i32, x: i32) -> Option<&mut T> {
+    pub fn map<U: Default, F: FnOnce(&mut T) -> U>(&mut self, y: i32, x: i32, f: F) -> U {
         if self.is_valid_coordinate(x, y) {
             unsafe {
                 // hey, I just checked that coordinates are valid!
-                Some(self.pixels.get_unchecked_mut(y as usize, x as usize))
+                f(self.pixels.get_unchecked_mut(y as usize, x as usize))
             }
         } else {
-            None
+            U::default()
         }
     }
 
-    #[inline]
-    pub fn map<U: Default, F: FnOnce(&mut T) -> U>(&mut self, y: i32, x: i32, f: F) -> U {
-        self.get_mut(y, x).map(f).unwrap_or_default()
-    }
-
     fn apply_along_line<U: Default + ops::AddAssign, F: FnMut(i32, i32) -> U>(
         x1: i32,
         y1: i32,
@@ -156,7 +151,7 @@
     pub fn fill_from_iter<I>(&mut self, i: I, value: T) -> usize
         where I: std::iter::Iterator<Item = Point>
     {
-        i.map(|p| self.get_mut(p.y, p.x).map(|v| *v = value)).count()
+        i.map(|p| self.map(p.y, p.x, |v| {*v = value; 1})).count()
     }
 
     pub fn draw_line(&mut self, from: Point, to: Point, value: T) -> usize {