remove iter from trait
This commit is contained in:
		
							parent
							
								
									17d41ef6c6
								
							
						
					
					
						commit
						c600761f29
					
				
					 3 changed files with 37 additions and 60 deletions
				
			
		| 
						 | 
				
			
			@ -1,3 +1,5 @@
 | 
			
		|||
use std::slice::{Iter, IterMut};
 | 
			
		||||
 | 
			
		||||
use crate::grid::RefGrid;
 | 
			
		||||
use crate::{DataRef, Grid};
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -43,6 +45,24 @@ impl ByteGrid {
 | 
			
		|||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn iter(&self) -> Iter<u8> {
 | 
			
		||||
        self.data.iter()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn iter_rows(&self) -> IterRows {
 | 
			
		||||
        IterRows {
 | 
			
		||||
            byte_grid: self,
 | 
			
		||||
            row: 0,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Returns an iterator that allows modifying each value.
 | 
			
		||||
    ///
 | 
			
		||||
    /// The iterator yields all cells from top left to bottom right.
 | 
			
		||||
    pub fn iter_mut(&mut self) -> IterMut<u8> {
 | 
			
		||||
        self.data.iter_mut()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn check_indexes(&self, x: usize, y: usize) {
 | 
			
		||||
        assert!(
 | 
			
		||||
            x < self.width,
 | 
			
		||||
| 
						 | 
				
			
			@ -92,21 +112,6 @@ impl Grid<u8> for ByteGrid {
 | 
			
		|||
        self.data[x + y * self.width]
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn iter(&self) -> impl Iterator<Item = u8> {
 | 
			
		||||
        Iter {
 | 
			
		||||
            byte_grid: self,
 | 
			
		||||
            index: 0,
 | 
			
		||||
            end: self.data.len(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn iter_rows(&self) -> impl Iterator<Item = impl Iterator<Item = u8>> {
 | 
			
		||||
        IterRows {
 | 
			
		||||
            byte_grid: self,
 | 
			
		||||
            row: 0,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn fill(&mut self, value: u8) {
 | 
			
		||||
        self.data.fill(value);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -167,45 +172,24 @@ impl RefGrid<u8> for ByteGrid {
 | 
			
		|||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct Iter<'t> {
 | 
			
		||||
    byte_grid: &'t ByteGrid,
 | 
			
		||||
    index: usize,
 | 
			
		||||
    end: usize,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'t> Iterator for Iter<'t> {
 | 
			
		||||
    type Item = u8;
 | 
			
		||||
 | 
			
		||||
    fn next(&mut self) -> Option<Self::Item> {
 | 
			
		||||
        if self.index >= self.end {
 | 
			
		||||
            return None;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        let result = self.byte_grid.data[self.index];
 | 
			
		||||
        self.index += 1;
 | 
			
		||||
        Some(result)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct IterRows<'t> {
 | 
			
		||||
    byte_grid: &'t ByteGrid,
 | 
			
		||||
    row: usize,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'t> Iterator for IterRows<'t> {
 | 
			
		||||
    type Item = Iter<'t>;
 | 
			
		||||
    type Item = Iter<'t, u8>;
 | 
			
		||||
 | 
			
		||||
    fn next(&mut self) -> Option<Self::Item> {
 | 
			
		||||
        if self.row >= self.byte_grid.height {
 | 
			
		||||
            return None;
 | 
			
		||||
        }
 | 
			
		||||
        let result = Some(Iter {
 | 
			
		||||
            byte_grid: self.byte_grid,
 | 
			
		||||
            index: self.row * self.byte_grid.width,
 | 
			
		||||
            end: (self.row + 1) * self.byte_grid.width,
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        let start = self.row * self.byte_grid.width;
 | 
			
		||||
        let end = start + self.byte_grid.width;
 | 
			
		||||
        let result = self.byte_grid.data[start..end].iter();
 | 
			
		||||
        self.row += 1;
 | 
			
		||||
        result
 | 
			
		||||
        Some(result)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -54,13 +54,6 @@ pub trait Grid<T> {
 | 
			
		|||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get an iterator over every cell in the grid.
 | 
			
		||||
    ///
 | 
			
		||||
    /// Iteration is done in memory order, rows first.
 | 
			
		||||
    fn iter(&self) -> impl Iterator<Item = T>;
 | 
			
		||||
 | 
			
		||||
    fn iter_rows(&self) -> impl Iterator<Item = impl Iterator<Item = T>>;
 | 
			
		||||
 | 
			
		||||
    /// Sets all cells in the grid to the specified value
 | 
			
		||||
    fn fill(&mut self, value: T);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -60,6 +60,17 @@ impl PixelGrid {
 | 
			
		|||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn iter(&self) -> crate::bit_vec::Iter {
 | 
			
		||||
        self.bit_vec.iter()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn iter_rows(&self) -> IterRows {
 | 
			
		||||
        IterRows {
 | 
			
		||||
            pixel_grid: self,
 | 
			
		||||
            row: 0,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn check_indexes(&self, x: usize, y: usize) {
 | 
			
		||||
        assert!(
 | 
			
		||||
            x < self.width,
 | 
			
		||||
| 
						 | 
				
			
			@ -96,17 +107,6 @@ impl Grid<bool> for PixelGrid {
 | 
			
		|||
        self.bit_vec.get(x + y * self.width)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn iter(&self) -> impl Iterator<Item = bool> {
 | 
			
		||||
        self.bit_vec.iter()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn iter_rows(&self) -> impl Iterator<Item = impl Iterator<Item = bool>> {
 | 
			
		||||
        IterRows {
 | 
			
		||||
            pixel_grid: self,
 | 
			
		||||
            row: 0,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Sets the state of all pixels in the `PixelGrid`.
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Arguments
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue