From 5d38f27393cbb5e90ec69b1ea5d8fa85fc0b34cd Mon Sep 17 00:00:00 2001
From: Ben Bridle <bridle.benjamin@gmail.com>
Date: Mon, 7 Oct 2024 17:46:11 +1300
Subject: Implement slice indexing for Buffer

Buffer can now be indexed by a range to take a slice of the underlying
pixel data.
---
 src/lib.rs | 38 +++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)

(limited to 'src/lib.rs')

diff --git a/src/lib.rs b/src/lib.rs
index 47dedac..abcd80f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,8 +1,12 @@
 pub use colour::Colour;
+pub use geometry::HasDimensions;
+
+use std::ops::{Deref, DerefMut, Index, IndexMut};
+use std::slice::SliceIndex;
+
 pub type Point = geometry::Point<i32>;
 pub type Dimensions = geometry::Dimensions<u32>;
 pub type Rect = geometry::Rect<i32, u32>;
-pub use geometry::HasDimensions;
 
 
 pub struct Buffer {
@@ -42,12 +46,10 @@ impl Buffer {
         let inter_width = intersection.width() as usize;
         let other_width = other.width() as usize;
         let self_width = self.width() as usize;
-        let other_slice = other.as_slice();
-        let self_slice = self.as_mut_slice();
 
         for _ in 0..intersection.height() {
-            let self_row = &mut self_slice[self_i .. self_i+inter_width];
-            let other_row = &other_slice[other_i .. other_i+inter_width];
+            let self_row = &mut self[self_i .. self_i+inter_width];
+            let other_row = &other[other_i .. other_i+inter_width];
             self_row.copy_from_slice(other_row);
             other_i += other_width;
             self_i += self_width;
@@ -63,13 +65,6 @@ impl Buffer {
         self.pixels.chunks_exact(self.dimensions.width as usize)
     }
 
-    pub fn as_slice(&self) -> &[Colour] {
-        &self.pixels
-    }
-
-    pub fn as_mut_slice(&mut self) -> &mut [Colour] {
-        &mut self.pixels
-    }
 
     pub fn as_u32_slice(&self) -> &[u32] {
         unsafe { std::mem::transmute::<&[Colour], &[u32]>(&self.pixels) }
@@ -80,16 +75,21 @@ impl HasDimensions<u32> for Buffer {
     fn dimensions(&self) -> Dimensions { self.dimensions }
 }
 
-impl std::ops::Index<usize> for Buffer {
-    type Output = Colour;
-    fn index(&self, i: usize) -> &Colour { &self.pixels[i] }
+impl<I: SliceIndex<[Colour]>> Index<I> for Buffer {
+    type Output = I::Output;
+
+    fn index(&self, index: I) -> &Self::Output {
+        &self.pixels[index]
+    }
 }
 
-impl std::ops::IndexMut<usize> for Buffer {
-    fn index_mut(&mut self, i: usize) -> &mut Colour { &mut self.pixels[i] }
+impl<I: SliceIndex<[Colour]>> IndexMut<I> for Buffer {
+    fn index_mut(&mut self, index: I) -> &mut Self::Output {
+        &mut self.pixels[index]
+    }
 }
 
-impl std::ops::Deref for Buffer {
+impl Deref for Buffer {
   type Target = [Colour];
 
   fn deref(&self) -> &[Colour] {
@@ -97,7 +97,7 @@ impl std::ops::Deref for Buffer {
   }
 }
 
-impl std::ops::DerefMut for Buffer {
+impl DerefMut for Buffer {
   fn deref_mut(&mut self) -> &mut [Colour] {
     self.pixels.as_mut_slice()
   }
-- 
cgit v1.2.3-70-g09d2