tabled/table_iterator_ext.rs
1use papergrid::records::{cell_info::CellInfo, vec_records::VecRecords};
2
3use crate::{Table, Tabled};
4
5/// A trait for [`IntoIterator`] whose Item type is bound to [`Tabled`].
6/// Any type implements [`IntoIterator`] can call this function directly
7///
8/// ```rust
9/// use tabled::{TableIteratorExt, Style};
10///
11/// let strings: &[&str] = &["Hello", "World"];
12///
13/// let mut table = strings.table();
14/// table.with(Style::psql());
15///
16/// println!("{}", table);
17/// ```
18pub trait TableIteratorExt {
19 /// A underline [`Records`]
20 ///
21 /// [`Records`]: crate::papergrid::records::Records
22 type Records;
23
24 /// Returns a [`Table`] instance from a given type
25 fn table(self) -> Table<Self::Records>;
26}
27
28impl<I, T> TableIteratorExt for I
29where
30 I: IntoIterator<Item = T>,
31 T: Tabled,
32{
33 type Records = VecRecords<CellInfo<'static>>;
34
35 fn table(self) -> Table<Self::Records> {
36 Table::new(self)
37 }
38}