summaryrefslogtreecommitdiff
path: root/src/grid/row.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/grid/row.rs')
-rw-r--r--src/grid/row.rs186
1 files changed, 186 insertions, 0 deletions
diff --git a/src/grid/row.rs b/src/grid/row.rs
new file mode 100644
index 00000000..8711d04f
--- /dev/null
+++ b/src/grid/row.rs
@@ -0,0 +1,186 @@
+// Copyright 2016 Joe Wilm, The Alacritty Project Contributors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//! Defines the Row type which makes up lines in the grid
+
+use std::ops::{Deref, DerefMut, Index, IndexMut};
+use std::ops::{Range, RangeTo, RangeFrom, RangeFull};
+use std::slice;
+
+use index::Column;
+
+/// A row in the grid
+#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
+pub struct Row<T>(Vec<T>);
+
+impl<T: Clone> Row<T> {
+ pub fn new(columns: Column, template: &T) -> Row<T> {
+ Row(vec![template.to_owned(); *columns])
+ }
+
+ pub fn grow(&mut self, cols: Column, template: &T) {
+ while self.len() != *cols {
+ self.push(template.to_owned());
+ }
+ }
+}
+
+impl<T> Row<T> {
+ pub fn shrink(&mut self, cols: Column) {
+ while self.len() != *cols {
+ self.pop();
+ }
+ }
+
+ #[inline]
+ pub fn cells(&self) -> slice::Iter<T> {
+ self.0.iter()
+ }
+
+ #[inline]
+ pub fn cells_mut(&mut self) -> slice::IterMut<T> {
+ self.0.iter_mut()
+ }
+}
+
+
+impl<'a, T> IntoIterator for &'a Row<T> {
+ type Item = &'a T;
+ type IntoIter = slice::Iter<'a, T>;
+
+ #[inline]
+ fn into_iter(self) -> slice::Iter<'a, T> {
+ self.iter()
+ }
+}
+
+impl<'a, T> IntoIterator for &'a mut Row<T> {
+ type Item = &'a mut T;
+ type IntoIter = slice::IterMut<'a, T>;
+
+ #[inline]
+ fn into_iter(self) -> slice::IterMut<'a, T> {
+ self.iter_mut()
+ }
+}
+
+impl<T> Deref for Row<T> {
+ type Target = Vec<T>;
+
+ #[inline]
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+
+impl<T> DerefMut for Row<T> {
+ #[inline]
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.0
+ }
+}
+
+impl<T> Index<Column> for Row<T> {
+ type Output = T;
+
+ #[inline]
+ fn index(&self, index: Column) -> &T {
+ &self.0[index.0]
+ }
+}
+
+impl<T> IndexMut<Column> for Row<T> {
+ #[inline]
+ fn index_mut(&mut self, index: Column) -> &mut T {
+ &mut self.0[index.0]
+ }
+}
+
+macro_rules! row_index_range {
+ ($range:ty) => {
+ impl<T> Index<$range> for Row<T> {
+ type Output = [T];
+
+ #[inline]
+ fn index(&self, index: $range) -> &[T] {
+ &self.0[index]
+ }
+ }
+
+ impl<T> IndexMut<$range> for Row<T> {
+ #[inline]
+ fn index_mut(&mut self, index: $range) -> &mut [T] {
+ &mut self.0[index]
+ }
+ }
+ }
+}
+
+row_index_range!(Range<usize>);
+row_index_range!(RangeTo<usize>);
+row_index_range!(RangeFrom<usize>);
+row_index_range!(RangeFull);
+
+// -----------------------------------------------------------------------------
+// Column ranges for Row
+// -----------------------------------------------------------------------------
+
+impl<T> Index<Range<Column>> for Row<T> {
+ type Output = [T];
+
+ #[inline]
+ fn index(&self, index: Range<Column>) -> &[T] {
+ &self.0[(index.start.0)..(index.end.0)]
+ }
+}
+
+impl<T> IndexMut<Range<Column>> for Row<T> {
+ #[inline]
+ fn index_mut(&mut self, index: Range<Column>) -> &mut [T] {
+ &mut self.0[(index.start.0)..(index.end.0)]
+ }
+}
+
+impl<T> Index<RangeTo<Column>> for Row<T> {
+ type Output = [T];
+
+ #[inline]
+ fn index(&self, index: RangeTo<Column>) -> &[T] {
+ &self.0[..(index.end.0)]
+ }
+}
+
+impl<T> IndexMut<RangeTo<Column>> for Row<T> {
+ #[inline]
+ fn index_mut(&mut self, index: RangeTo<Column>) -> &mut [T] {
+ &mut self.0[..(index.end.0)]
+ }
+}
+
+impl<T> Index<RangeFrom<Column>> for Row<T> {
+ type Output = [T];
+
+ #[inline]
+ fn index(&self, index: RangeFrom<Column>) -> &[T] {
+ &self.0[(index.start.0)..]
+ }
+}
+
+impl<T> IndexMut<RangeFrom<Column>> for Row<T> {
+ #[inline]
+ fn index_mut(&mut self, index: RangeFrom<Column>) -> &mut [T] {
+ &mut self.0[(index.start.0)..]
+ }
+}