diff options
author | Joe Wilm <joe@jwilm.com> | 2017-10-12 20:12:29 -0700 |
---|---|---|
committer | Joe Wilm <joe@jwilm.com> | 2018-06-02 09:24:38 -0700 |
commit | 6fc0e1ec49561fd9783332b30632471336004aed (patch) | |
tree | fdb0e7bd9e6910ce9ef18eb7127e5006f2aab953 /src/grid | |
parent | 277425956f361677deb1de92b25aeca9cbcd1cd1 (diff) | |
download | alacritty-6fc0e1ec49561fd9783332b30632471336004aed.tar.gz alacritty-6fc0e1ec49561fd9783332b30632471336004aed.zip |
Eliminate ClearRegion trait
Diffstat (limited to 'src/grid')
-rw-r--r-- | src/grid/mod.rs | 59 |
1 files changed, 30 insertions, 29 deletions
diff --git a/src/grid/mod.rs b/src/grid/mod.rs index 123e13fa..bd994033 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -23,7 +23,7 @@ use std::cmp::Ordering; use std::collections::{VecDeque, vec_deque}; use std::iter::IntoIterator; -use std::ops::{Deref, Range, RangeTo, RangeFrom, Index, IndexMut}; +use std::ops::{Deref, Range, RangeTo, RangeFrom, RangeFull, Index, IndexMut}; use index::{self, Point, Line, Column, IndexRange, RangeInclusive}; @@ -207,12 +207,6 @@ impl<T> Grid<T> { self.raw.swap(*src, *dst); } - #[inline] - pub fn clear<F: Fn(&mut T)>(&mut self, func: F) { - let region = index::Line(0)..self.num_lines(); - self.clear_region(region, func); - } - fn shrink_lines(&mut self, lines: index::Line) { while index::Line(self.raw.len()) != lines { self.raw.pop_back(); @@ -315,28 +309,6 @@ impl<'a, T> IntoIterator for &'a Grid<T> { } } -pub trait ClearRegion<R, T> { - fn clear_region<F: Fn(&mut T)>(&mut self, region: R, func: F); -} - -macro_rules! clear_region_impl { - ($range:ty) => { - impl<T> ClearRegion<$range, T> for Grid<T> { - fn clear_region<F: Fn(&mut T)>(&mut self, region: $range, func: F) { - for row in self.region_mut(region) { - for cell in row { - func(cell); - } - } - } - } - } -} - -clear_region_impl!(Range<index::Line>); -clear_region_impl!(RangeTo<index::Line>); -clear_region_impl!(RangeFrom<index::Line>); - // ================================================================================================= // Regions ========================================================================================= // ================================================================================================= @@ -359,6 +331,17 @@ pub struct RegionMut<'a, T: 'a> { raw: &'a mut VecDeque<Row<T>>, } +impl<'a, T> RegionMut<'a, T> { + /// Call the provided function for every item in this region + pub fn each<F: Fn(&mut T)>(self, func: F) { + for row in self { + for item in row { + func(item) + } + } + } +} + pub trait IndexRegion<I, T> { /// Get an immutable region of Self fn region<'a>(&'a self, _: I) -> Region<'a, T>; @@ -428,6 +411,24 @@ impl<T> IndexRegion<RangeFrom<Line>, T> for Grid<T> { } } +impl<T> IndexRegion<RangeFull, T> for Grid<T> { + fn region(&self, _: RangeFull) -> Region<T> { + Region { + start: Line(0), + end: self.num_lines(), + raw: &self.raw + } + } + + fn region_mut(&mut self, _: RangeFull) -> RegionMut<T> { + RegionMut { + start: Line(0), + end: self.num_lines(), + raw: &mut self.raw + } + } +} + pub struct RegionIter<'a, T: 'a> { end: Line, cur: Line, |