| // 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(Default, Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] |
| pub struct Row<T>(Vec<T>); |
| |
| impl<T: Copy + Clone> Row<T> { |
| pub fn new(columns: Column, template: &T) -> Row<T> { |
| Row(vec![*template; *columns]) |
| } |
| |
| pub fn grow(&mut self, cols: Column, template: &T) { |
| while self.len() != *cols { |
| self.push(*template); |
| } |
| } |
| |
| /// Resets contents to the contents of `other` |
| #[inline] |
| pub fn reset(&mut self, other: &T) { |
| for item in &mut self.0 { |
| *item = *other; |
| } |
| } |
| } |
| |
| impl<T> Row<T> { |
| pub fn shrink(&mut self, cols: Column) { |
| while self.len() != *cols { |
| self.pop(); |
| } |
| } |
| } |
| |
| |
| 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] |
| } |
| } |
| |
| // ----------------------------------------------------------------------------- |
| // Index ranges of columns |
| // ----------------------------------------------------------------------------- |
| |
| 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)..] |
| } |
| } |
| |
| impl<T> Index<RangeFull> for Row<T> { |
| type Output = [T]; |
| |
| #[inline] |
| fn index(&self, _: RangeFull) -> &[T] { |
| &self.0[..] |
| } |
| } |
| |
| impl<T> IndexMut<RangeFull> for Row<T> { |
| #[inline] |
| fn index_mut(&mut self, _: RangeFull) -> &mut [T] { |
| &mut self.0[..] |
| } |
| } |