rust/vec2d/src/lib.rs
changeset 14160 c24a76f131d6
parent 14121 69db1d2e4cec
child 14170 a4c1a2d0ac24
--- a/rust/vec2d/src/lib.rs	Wed Nov 07 19:43:17 2018 +0100
+++ b/rust/vec2d/src/lib.rs	Wed Nov 07 22:01:47 2018 +0300
@@ -55,8 +55,14 @@
         Self { size, data: vec![value; size.area()] }
     }
 
-    pub fn raw_data(&self) -> &[T] {
-        &self.data
+    #[inline]
+    pub fn as_slice(&self) -> &[T] {
+        self.data.as_slice()
+    }
+
+    #[inline]
+    pub fn as_mut_slice(&mut self) -> &mut [T] {
+        self.data.as_mut_slice()
     }
 
     #[inline]
@@ -83,6 +89,24 @@
     pub fn rows(&self) -> impl Iterator<Item = &[T]> {
         self.data.chunks(self.width())
     }
+
+    #[inline]
+    pub fn rows_mut(&mut self) -> impl Iterator<Item = &mut [T]> {
+        let width = self.width();
+        self.data.chunks_mut(width)
+    }
+}
+
+impl<T: Copy> AsRef<[T]> for Vec2D<T> {
+    fn as_ref(&self) -> &[T] {
+        self.as_slice()
+    }
+}
+
+impl<T: Copy> AsMut<[T]> for Vec2D<T> {
+    fn as_mut(&mut self) -> &mut [T] {
+        self.as_mut_slice()
+    }
 }
 
 #[cfg(test)]