diff options
author | Pierre-Henri Symoneaux <pierre-henri.symoneaux@nokia.com> | 2018-02-19 22:06:44 +0100 |
---|---|---|
committer | Pierre-Henri Symoneaux <pierre-henri.symoneaux@nokia.com> | 2018-02-19 22:06:44 +0100 |
commit | ef0dea770fb3cde2444f69595cea9efb9101a910 (patch) | |
tree | f5cfcb53181370c471423dab57da2656638f2d17 /src/row.rs | |
parent | f38be5c886e2462f074ce849eb9aa6815900a7e3 (diff) |
Added Hash & Eq derive. Implemented Extend on Row and Table
Diffstat (limited to 'src/row.rs')
-rw-r--r-- | src/row.rs | 33 |
1 files changed, 32 insertions, 1 deletions
@@ -2,6 +2,7 @@ use std::io::{Write, Error}; use std::iter::FromIterator; use std::slice::{Iter, IterMut}; +// use std::vec::IntoIter; use std::ops::{Index, IndexMut}; use term::Terminal; @@ -11,7 +12,7 @@ use super::cell::Cell; use super::format::{TableFormat, ColumnPosition}; /// Represent a table row made of cells -#[derive(Clone, Debug, Hash)] +#[derive(Clone, Debug, Hash, PartialEq, Eq)] pub struct Row { cells: Vec<Cell>, } @@ -207,6 +208,14 @@ impl<'a> IntoIterator for &'a Row { } } +// impl IntoIterator for Row { +// type Item = Cell; +// type IntoIter = IntoIter<Cell>; +// fn into_iter(self) -> Self::IntoIter { +// self.cells.into_iter() +// } +// } + impl<'a> IntoIterator for &'a mut Row { type Item = &'a mut Cell; type IntoIter = IterMut<'a, Cell>; @@ -215,6 +224,18 @@ impl<'a> IntoIterator for &'a mut Row { } } +impl <S: ToString> Extend<S> for Row { + fn extend<T: IntoIterator<Item=S>>(&mut self, iter: T) { + self.cells.extend(iter.into_iter().map(|s| Cell::new(&s.to_string()))); + } +} + +// impl <S: Into<Cell>> Extend<S> for Row { +// fn extend<T: IntoIterator<Item=S>>(&mut self, iter: T) { +// self.cells.extend(iter.into_iter().map(|s| s.into())); +// } +// } + /// This macro simplifies `Row` creation /// /// The syntax support style spec @@ -305,4 +326,14 @@ mod tests { assert_eq!(row.get_cell(0).unwrap().get_content(), "foo"); assert_eq!(row.get_cell(1).unwrap().get_content(), "foobar"); } + + #[test] + fn extend_row() { + let mut row = Row::from(vec!["foo", "bar", "foobar"]); + row.extend(vec!["A", "B", "C"]); + assert_eq!(row.len(), 6); + assert_eq!(row.get_cell(3).unwrap().get_content(), "A"); + assert_eq!(row.get_cell(4).unwrap().get_content(), "B"); + assert_eq!(row.get_cell(5).unwrap().get_content(), "C"); + } } |