From 4fdd5280f1e79ea6575a6a110951c564a7dd235e Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Sat, 4 Jun 2016 19:40:30 -0700 Subject: Add iterator methods to Grid and Row types The iterator methods simplify logic in the main grid render function. To disambiguate iterator methods from those returning counts (and to free up names), the `rows()` and `cols()` methods on `Grid` have been renamed to `num_rows()` and `num_cols()`, respectively. --- src/grid.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'src/grid.rs') diff --git a/src/grid.rs b/src/grid.rs index 765a5e1a..a3009f8b 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -1,8 +1,8 @@ //! Functions for computing properties of the terminal grid -use std::collections::VecDeque; - +use std::collections::{vec_deque, VecDeque}; use std::ops::{Index, IndexMut, Deref, DerefMut}; +use std::slice::{Iter, IterMut}; use term::Cursor; use ::Rgb; @@ -60,11 +60,19 @@ impl Grid { } } - pub fn rows(&self) -> usize { + pub fn rows(&self) -> vec_deque::Iter { + self.raw.iter() + } + + pub fn rows_mut(&mut self) -> vec_deque::IterMut { + self.raw.iter_mut() + } + + pub fn num_rows(&self) -> usize { self.rows } - pub fn cols(&self) -> usize { + pub fn num_cols(&self) -> usize { self.cols } @@ -130,8 +138,12 @@ impl Row { Row(vec![Cell::new(' '); columns]) } - pub fn cols(&self) -> usize { - self.0.len() + pub fn cells(&self) -> Iter { + self.0.iter() + } + + pub fn cells_mut(&mut self) -> IterMut { + self.0.iter_mut() } } -- cgit