// 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(Vec); impl Row { pub fn new(columns: Column, template: &T) -> Row { Row(vec![*template; *columns]) } pub fn grow(&mut self, cols: Column, template: &T) { assert!(self.len() < * cols); while self.len() != *cols { self.0.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 Row { pub fn shrink(&mut self, cols: Column) { while self.len() != *cols { self.pop(); } } } impl<'a, T> IntoIterator for &'a Row { 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 { type Item = &'a mut T; type IntoIter = slice::IterMut<'a, T>; #[inline] fn into_iter(self) -> slice::IterMut<'a, T> { self.iter_mut() } } impl Deref for Row { type Target = Vec; #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for Row { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl Index for Row { type Output = T; #[inline] fn index(&self, index: Column) -> &T { &self.0[index.0] } } impl IndexMut for Row { #[inline] fn index_mut(&mut self, index: Column) -> &mut T { &mut self.0[index.0] } } // ----------------------------------------------------------------------------- // Index ranges of columns // ----------------------------------------------------------------------------- impl Index> for Row { type Output = [T]; #[inline] fn index(&self, index: Range) -> &[T] { &self.0[(index.start.0)..(index.end.0)] } } impl IndexMut> for Row { #[inline] fn index_mut(&mut self, index: Range) -> &mut [T] { &mut self.0[(index.start.0)..(index.end.0)] } } impl Index> for Row { type Output = [T]; #[inline] fn index(&self, index: RangeTo) -> &[T] { &self.0[..(index.end.0)] } } impl IndexMut> for Row { #[inline] fn index_mut(&mut self, index: RangeTo) -> &mut [T] { &mut self.0[..(index.end.0)] } } impl Index> for Row { type Output = [T]; #[inline] fn index(&self, index: RangeFrom) -> &[T] { &self.0[(index.start.0)..] } } impl IndexMut> for Row { #[inline] fn index_mut(&mut self, index: RangeFrom) -> &mut [T] { &mut self.0[(index.start.0)..] } } impl Index for Row { type Output = [T]; #[inline] fn index(&self, _: RangeFull) -> &[T] { &self.0[..] } } impl IndexMut for Row { #[inline] fn index_mut(&mut self, _: RangeFull) -> &mut [T] { &mut self.0[..] } }