diff options
author | Pierre-Henri Symoneaux <pierre-henri.symoneaux@nokia.com> | 2016-04-21 10:49:51 +0200 |
---|---|---|
committer | Pierre-Henri Symoneaux <pierre-henri.symoneaux@nokia.com> | 2016-04-21 10:49:51 +0200 |
commit | e236702ee7df575618b7ea62d90e200b892bdb63 (patch) | |
tree | e0d44185a591eaa84b00029516596d79233f118c | |
parent | afb06f568e9b091024b581727ce21066802f0310 (diff) |
Improvements to #27 (for #25)
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | src/lib.rs | 34 | ||||
-rw-r--r-- | src/row.rs | 20 |
3 files changed, 53 insertions, 7 deletions
@@ -6,15 +6,15 @@ # prettytable-rs
+A formatted and aligned table printer written in rust.
+
[Documentation](http://phsym.github.io/prettytable-rs)
-*Copyright © 2015 Pierre-Henri Symoneaux*
+*Copyright © 2016 Pierre-Henri Symoneaux*
> THIS SOFTWARE IS DISTRIBUTED WITHOUT ANY WARRANTY <br>
> Check LICENSE.txt file for more information. <br>
-A formatted and aligned table printer written in rust.
-
# How to use
## Including
@@ -110,11 +110,16 @@ impl <'a> TableSlice<'a> { return col_width;
}
- /// Return an iterator over the immutable cells of the column specified by `column`
+ /// Returns an iterator over the immutable cells of the column specified by `column`
pub fn column_iter(&self, column: usize) -> ColumnIter {
return ColumnIter(self.rows.iter(), column);
}
+ /// Returns an iterator over immutable rows
+ pub fn row_iter(&self) -> Iter<Row> {
+ self.rows.iter()
+ }
+
/// Internal only
fn __print<T: Write+?Sized, F>(&self, out: &mut T, f: F) -> Result<(), Error>
where F: Fn(&Row, &mut T, &TableFormat, &[usize]) -> Result<(), Error> {
@@ -177,6 +182,14 @@ impl <'a> TableSlice<'a> { }
}
+impl <'a> IntoIterator for &'a TableSlice<'a> {
+ type Item=&'a Row;
+ type IntoIter=Iter<'a, Row>;
+ fn into_iter(self) -> Self::IntoIter {
+ return self.row_iter();
+ }
+}
+
impl Table {
/// Create an empty table
pub fn new() -> Table {
@@ -275,15 +288,16 @@ impl Table { return ColumnIterMut(self.rows.iter_mut(), column);
}
+ /// Returns an iterator over immutable rows pub fn row_iter<'a>(&'a self) -> Iter<'a, Row> { self.rows.iter() } + /// Returns an iterator over mutable rows pub fn row_iter_mut<'a>(&'a mut self) -> IterMut<'a, Row> { self.rows.iter_mut() } -
/// Print the table to `out`
pub fn print<T: Write+?Sized>(&self, out: &mut T) -> Result<(), Error> {
return self.as_ref().print(out);
@@ -366,6 +380,22 @@ impl <T, A, B> From<T> for Table where B: ToString, A: IntoIterator<Item=B>, T : }
}
+impl <'a> IntoIterator for &'a Table {
+ type Item=&'a Row;
+ type IntoIter=Iter<'a, Row>;
+ fn into_iter(self) -> Self::IntoIter {
+ return self.as_ref().row_iter();
+ }
+}
+
+impl <'a> IntoIterator for &'a mut Table {
+ type Item=&'a mut Row;
+ type IntoIter=IterMut<'a, Row>;
+ fn into_iter(self) -> Self::IntoIter {
+ return self.row_iter_mut();
+ }
+}
+
/// Iterator over immutable cells in a column
pub struct ColumnIter<'a>(std::slice::Iter<'a, Row>, usize);
@@ -96,12 +96,12 @@ impl Row { }
}
- // You need to impl Iterator for these to work with for / map implicitly
- // But there is a compiler warning when you do add the trait.
+ /// Returns an immutable iterator over cells
pub fn iter<'a>(&'a self) -> Iter<'a, Cell> {
self.cells.iter()
}
+ /// Returns an mutable iterator over cells
pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, Cell> {
self.cells.iter_mut()
}
@@ -175,6 +175,22 @@ impl <T, A> From<T> for Row where A: ToString, T : IntoIterator<Item=A> { }
}
+impl <'a> IntoIterator for &'a Row {
+ type Item=&'a Cell;
+ type IntoIter=Iter<'a, Cell>;
+ fn into_iter(self) -> Self::IntoIter {
+ return self.iter();
+ }
+}
+
+impl <'a> IntoIterator for &'a mut Row {
+ type Item=&'a mut Cell;
+ type IntoIter=IterMut<'a, Cell>;
+ fn into_iter(self) -> Self::IntoIter {
+ return self.iter_mut();
+ }
+}
+
/// This macro simplifies `Row` creation
///
/// The syntax support style spec
|