From 9a98d5e0ee9139d5a2988d125352c5d70a39ad20 Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Tue, 29 May 2018 21:33:13 -0700 Subject: [PATCH] Refactor Storage to be a Vec> internally This will allow certain optimizations around swap which are currently only possible in a generalized way with specialization. --- src/grid/mod.rs | 10 ++++---- src/grid/storage.rs | 57 +++++++++++++++++++++++---------------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/grid/mod.rs b/src/grid/mod.rs index c9829c2e..b03ae54f 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -67,7 +67,7 @@ impl ::std::cmp::PartialEq for Grid { pub struct Grid { /// Lines in the grid. Each row holds a list of cells corresponding to the /// columns in that row. - raw: Storage>, + raw: Storage, /// Number of columns cols: index::Column, @@ -540,7 +540,7 @@ impl<'point, T> IndexMut<&'point Point> for Grid { pub struct Region<'a, T: 'a> { start: Line, end: Line, - raw: &'a Storage>, + raw: &'a Storage, } /// A mutable subset of lines in the grid @@ -549,7 +549,7 @@ pub struct Region<'a, T: 'a> { pub struct RegionMut<'a, T: 'a> { start: Line, end: Line, - raw: &'a mut Storage>, + raw: &'a mut Storage, } impl<'a, T> RegionMut<'a, T> { @@ -653,13 +653,13 @@ impl IndexRegion for Grid { pub struct RegionIter<'a, T: 'a> { end: Line, cur: Line, - raw: &'a Storage>, + raw: &'a Storage, } pub struct RegionIterMut<'a, T: 'a> { end: Line, cur: Line, - raw: &'a mut Storage>, + raw: &'a mut Storage, } impl<'a, T> IntoIterator for Region<'a, T> { diff --git a/src/grid/storage.rs b/src/grid/storage.rs index eb87c622..57afde82 100644 --- a/src/grid/storage.rs +++ b/src/grid/storage.rs @@ -14,14 +14,15 @@ use std::ops::{Index, IndexMut}; use std::slice; -use index::Line; +use index::{Column, Line}; +use super::Row; /// Maximum number of invisible lines before buffer is resized const TRUNCATE_STEP: usize = 100; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Storage { - inner: Vec, + inner: Vec>, zero: usize, visible_lines: Line, @@ -78,7 +79,7 @@ impl ::std::cmp::PartialEq for Storage { impl Storage { #[inline] - pub fn with_capacity(cap: usize, lines: Line, template: T) -> Storage + pub fn with_capacity(cap: usize, lines: Line, template: Row) -> Storage where T: Clone, { @@ -97,7 +98,7 @@ impl Storage { } /// Increase the number of lines in the buffer - pub fn grow_visible_lines(&mut self, next: Line, template_row: T) + pub fn grow_visible_lines(&mut self, next: Line, template_row: Row) where T: Clone, { @@ -225,7 +226,7 @@ impl Storage { /// is needed because of the grow lines functionality implemented on /// this type, and maybe that's where the leak is necessitating this /// accessor. - pub fn iter_mut_raw<'a>(&'a mut self) -> slice::IterMut<'a, T> { + pub fn iter_mut_raw<'a>(&'a mut self) -> slice::IterMut<'a, Row> { self.inner.iter_mut() } @@ -243,9 +244,9 @@ impl Storage { } impl Index for Storage { - type Output = T; + type Output = Row; #[inline] - fn index(&self, index: usize) -> &T { + fn index(&self, index: usize) -> &Self::Output { let index = self.compute_index(index); // borrowck &self.inner[index] } @@ -253,16 +254,16 @@ impl Index for Storage { impl IndexMut for Storage { #[inline] - fn index_mut(&mut self, index: usize) -> &mut T { + fn index_mut(&mut self, index: usize) -> &mut Self::Output { let index = self.compute_index(index); // borrowck &mut self.inner[index] } } impl Index for Storage { - type Output = T; + type Output = Row; #[inline] - fn index(&self, index: Line) -> &T { + fn index(&self, index: Line) -> &Self::Output { let index = self.visible_lines - index; &self[*index] } @@ -270,7 +271,7 @@ impl Index for Storage { impl IndexMut for Storage { #[inline] - fn index_mut(&mut self, index: Line) -> &mut T { + fn index_mut(&mut self, index: Line) -> &mut Self::Output { let index = self.visible_lines - index; &mut self[*index] } @@ -282,7 +283,7 @@ pub struct IterMut<'a, T: 'a> { } impl<'a, T: 'a> Iterator for IterMut<'a, T> { - type Item = &'a mut T; + type Item = &'a mut Row; fn next(&mut self) -> Option { if self.index == self.storage.len() { @@ -313,18 +314,18 @@ impl<'a, T: 'a> Iterator for IterMut<'a, T> { fn grow_after_zero() { // Setup storage area let mut storage = Storage { - inner: vec!["0", "1", "-"], + inner: vec![Row::new(Column(1), &'0'), Row::new(Column(1), &'1'), Row::new(Column(1), &'-')], zero: 0, visible_lines: Line(2), len: 3, }; // Grow buffer - storage.grow_visible_lines(Line(4), "-"); + storage.grow_visible_lines(Line(4), Row::new(Column(1), &'-')); // Make sure the result is correct let expected = Storage { - inner: vec!["-", "0", "1", "-"], + inner: vec![Row::new(Column(1), &'-'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1'), Row::new(Column(1), &'-')], zero: 1, visible_lines: Line(0), len: 4, @@ -349,18 +350,18 @@ fn grow_after_zero() { fn grow_before_zero() { // Setup storage area let mut storage = Storage { - inner: vec!["-", "0", "1"], + inner: vec![Row::new(Column(1), &'-'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1')], zero: 1, visible_lines: Line(2), len: 3, }; // Grow buffer - storage.grow_visible_lines(Line(4), "-"); + storage.grow_visible_lines(Line(4), Row::new(Column(1), &'-')); // Make sure the result is correct let expected = Storage { - inner: vec!["-", "-", "0", "1"], + inner: vec![Row::new(Column(1), &'-'), Row::new(Column(1), &'-'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1')], zero: 2, visible_lines: Line(0), len: 4, @@ -384,7 +385,7 @@ fn grow_before_zero() { fn shrink_before_zero() { // Setup storage area let mut storage = Storage { - inner: vec!["2", "0", "1"], + inner: vec![Row::new(Column(1), &'2'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1')], zero: 1, visible_lines: Line(2), len: 3, @@ -395,7 +396,7 @@ fn shrink_before_zero() { // Make sure the result is correct let expected = Storage { - inner: vec!["2", "0", "1"], + inner: vec![Row::new(Column(1), &'2'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1')], zero: 1, visible_lines: Line(0), len: 2, @@ -419,7 +420,7 @@ fn shrink_before_zero() { fn shrink_after_zero() { // Setup storage area let mut storage = Storage { - inner: vec!["0", "1", "2"], + inner: vec![Row::new(Column(1), &'0'), Row::new(Column(1), &'1'), Row::new(Column(1), &'2')], zero: 0, visible_lines: Line(2), len: 3, @@ -430,7 +431,7 @@ fn shrink_after_zero() { // Make sure the result is correct let expected = Storage { - inner: vec!["0", "1", "2"], + inner: vec![Row::new(Column(1), &'0'), Row::new(Column(1), &'1'), Row::new(Column(1), &'2')], zero: 0, visible_lines: Line(0), len: 2, @@ -460,7 +461,7 @@ fn shrink_after_zero() { fn shrink_before_and_after_zero() { // Setup storage area let mut storage = Storage { - inner: vec!["4", "5", "0", "1", "2", "3"], + inner: vec![Row::new(Column(1), &'4'), Row::new(Column(1), &'5'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1'), Row::new(Column(1), &'2'), Row::new(Column(1), &'3')], zero: 2, visible_lines: Line(5), len: 6, @@ -471,7 +472,7 @@ fn shrink_before_and_after_zero() { // Make sure the result is correct let expected = Storage { - inner: vec!["4", "5", "0", "1", "2", "3"], + inner: vec![Row::new(Column(1), &'4'), Row::new(Column(1), &'5'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1'), Row::new(Column(1), &'2'), Row::new(Column(1), &'3')], zero: 2, visible_lines: Line(0), len: 2, @@ -497,7 +498,7 @@ fn shrink_before_and_after_zero() { fn truncate_invisible_lines() { // Setup storage area let mut storage = Storage { - inner: vec!["4", "5", "0", "1", "2", "3"], + inner: vec![Row::new(Column(1), &'4'), Row::new(Column(1), &'5'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1'), Row::new(Column(1), &'2'), Row::new(Column(1), &'3')], zero: 2, visible_lines: Line(1), len: 2, @@ -508,7 +509,7 @@ fn truncate_invisible_lines() { // Make sure the result is correct let expected = Storage { - inner: vec!["0", "1"], + inner: vec![Row::new(Column(1), &'0'), Row::new(Column(1), &'1')], zero: 0, visible_lines: Line(1), len: 2, @@ -532,7 +533,7 @@ fn truncate_invisible_lines() { fn truncate_invisible_lines_beginning() { // Setup storage area let mut storage = Storage { - inner: vec!["1", "2", "0"], + inner: vec![Row::new(Column(1), &'1'), Row::new(Column(1), &'2'), Row::new(Column(1), &'0')], zero: 2, visible_lines: Line(1), len: 2, @@ -543,7 +544,7 @@ fn truncate_invisible_lines_beginning() { // Make sure the result is correct let expected = Storage { - inner: vec!["1", "0"], + inner: vec![Row::new(Column(1), &'1'), Row::new(Column(1), &'0')], zero: 1, visible_lines: Line(1), len: 2,