Struct CompactTable

Source
pub struct CompactTable<I, D> { /* private fields */ }
Expand description

A table which consumes an IntoRecords iterator. It assumes that the content has only single line.

In contrast to Table CompactTable does no allocations but it consumes an iterator. It’s useful when you don’t want to re/allocate a buffer for your data.

§Example

It works smoothly with arrays.

use tabled::{settings::Style, tables::CompactTable};

let data = [
    ["FreeBSD", "1993", "William and Lynne Jolitz", "?"],
    ["OpenBSD", "1995", "Theo de Raadt", ""],
    ["HardenedBSD", "2014", "Oliver Pinter and Shawn Webb", ""],
];

let table = CompactTable::from(data)
    .with(Style::psql())
    .to_string();

assert_eq!(
    table,
    concat!(
        " FreeBSD     | 1993 | William and Lynne Jolitz     | ? \n",
        " OpenBSD     | 1995 | Theo de Raadt                |   \n",
        " HardenedBSD | 2014 | Oliver Pinter and Shawn Webb |   ",
    )
);

But it’s default creation requires to be given an estimated cell width, and the amount of columns.

use tabled::{settings::Style, tables::CompactTable};

let data = [
    ["FreeBSD", "1993", "William and Lynne Jolitz", "?"],
    ["OpenBSD", "1995", "Theo de Raadt", ""],
    ["HardenedBSD", "2014", "Oliver Pinter and Shawn Webb", ""],
];

// See what will happen if the given width is too narrow

let table = CompactTable::new(&data)
    .columns(4)
    .width(5)
    .with(Style::ascii())
    .to_string();

assert_eq!(
    table,
    "+-----+-----+-----+-----+\n\
     | FreeBSD | 1993 | William and Lynne Jolitz | ?   |\n\
     |-----+-----+-----+-----|\n\
     | OpenBSD | 1995 | Theo de Raadt |     |\n\
     |-----+-----+-----+-----|\n\
     | HardenedBSD | 2014 | Oliver Pinter and Shawn Webb |     |\n\
     +-----+-----+-----+-----+"
);

Implementations§

Source§

impl<I> CompactTable<I, ConstDimension<0, 0>>

Source

pub const fn new(iter: I) -> Self
where I: IntoRecords,

Creates a new CompactTable structure with a width dimension for all columns.

Source§

impl<I, const ROWS: usize, const COLS: usize> CompactTable<I, ConstDimension<COLS, ROWS>>

Source

pub fn height<S: Into<ConstSize<COUNT_ROWS>>, const COUNT_ROWS: usize>( self, size: S, ) -> CompactTable<I, ConstDimension<COLS, COUNT_ROWS>>

Set a height for each row.

Source

pub fn width<S: Into<ConstSize<COUNT_COLUMNS>>, const COUNT_COLUMNS: usize>( self, size: S, ) -> CompactTable<I, ConstDimension<COUNT_COLUMNS, ROWS>>

Set a width for each column.

Source§

impl<I, D> CompactTable<I, D>

Source

pub fn with_dimension(iter: I, dimension: D) -> Self
where I: IntoRecords,

Creates a new CompactTable structure with a known dimension.

Notice that the function wont call Estimate.

Source

pub fn with<O>(self, option: O) -> Self
where for<'a> O: TableOption<IterRecords<&'a I>, CompactConfig, D>,

With is a generic function which applies options to the CompactTable.

Examples found in repository?
examples/compact_table.rs (line 13)
5fn main() {
6    let data = [
7        ["Debian", "1.1.1.1", "true"],
8        ["Arch", "127.1.1.1", "true"],
9        ["Manjaro", "Arch", "true"],
10        ["Manjaro", "A\nr\nc\nh", "true"],
11    ];
12
13    let table = CompactTable::from(data).with(Style::ascii());
14
15    #[cfg(feature = "std")]
16    println!("{}", table.to_string());
17}
More examples
Hide additional examples
examples/no_std.rs (line 10)
3fn main() {
4    let data = [
5        ["Debian", "1.1.1.1", "true"],
6        ["Arch", "127.1.1.1", "true"],
7        ["Manjaro", "Arch", "true"],
8    ];
9
10    let table = CompactTable::from(data).with(Style::modern());
11    let mut buf = [0; 1024 * 10];
12    let mut w = Writer::new(&mut buf);
13
14    table.fmt(&mut w).unwrap();
15
16    assert_table!(w.as_str(),
17        "┌─────────┬───────────┬──────┐"
18        "│ Debian  │ 1.1.1.1   │ true │"
19        "│─────────┼───────────┼──────│"
20        "│ Arch    │ 127.1.1.1 │ true │"
21        "│─────────┼───────────┼──────│"
22        "│ Manjaro │ Arch      │ true │"
23        "└─────────┴───────────┴──────┘"
24    );
25}
Source

pub const fn rows(self, count_rows: usize) -> Self

Limit a number of rows.

Source

pub const fn columns(self, count: usize) -> Self

Limit a number of columns.

Source

pub fn get_config(&self) -> &CompactConfig

Returns a table config.

Source

pub fn get_config_mut(&mut self) -> &mut CompactConfig

Returns a table config.

Source

pub fn fmt<W>(self, writer: W) -> Result
where I: IntoRecords, I::Cell: AsRef<str>, D: Dimension, W: Write,

Format table into fmt::Writeer.

Examples found in repository?
examples/no_std.rs (line 14)
3fn main() {
4    let data = [
5        ["Debian", "1.1.1.1", "true"],
6        ["Arch", "127.1.1.1", "true"],
7        ["Manjaro", "Arch", "true"],
8    ];
9
10    let table = CompactTable::from(data).with(Style::modern());
11    let mut buf = [0; 1024 * 10];
12    let mut w = Writer::new(&mut buf);
13
14    table.fmt(&mut w).unwrap();
15
16    assert_table!(w.as_str(),
17        "┌─────────┬───────────┬──────┐"
18        "│ Debian  │ 1.1.1.1   │ true │"
19        "│─────────┼───────────┼──────│"
20        "│ Arch    │ 127.1.1.1 │ true │"
21        "│─────────┼───────────┼──────│"
22        "│ Manjaro │ Arch      │ true │"
23        "└─────────┴───────────┴──────┘"
24    );
25}
Source

pub fn build<W>(self, writer: W) -> Result<()>
where I: IntoRecords, I::Cell: AsRef<str>, D: Dimension, W: Write,

Available on crate feature std only.

Format table into a writer.

Source

pub fn to_string(self) -> String
where I: IntoRecords, I::Cell: AsRef<str>, D: Dimension,

Available on crate feature std only.

Build a string.

We can’t implement std::string::ToString cause it does takes &self reference.

Examples found in repository?
examples/compact_table.rs (line 16)
5fn main() {
6    let data = [
7        ["Debian", "1.1.1.1", "true"],
8        ["Arch", "127.1.1.1", "true"],
9        ["Manjaro", "Arch", "true"],
10        ["Manjaro", "A\nr\nc\nh", "true"],
11    ];
12
13    let table = CompactTable::from(data).with(Style::ascii());
14
15    #[cfg(feature = "std")]
16    println!("{}", table.to_string());
17}

Trait Implementations§

Source§

impl<I: Clone, D: Clone> Clone for CompactTable<I, D>

Source§

fn clone(&self) -> CompactTable<I, D>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<I: Debug, D: Debug> Debug for CompactTable<I, D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, const ROWS: usize, const COLS: usize> From<[[T; COLS]; ROWS]> for CompactTable<[[T; COLS]; ROWS], ConstDimension<COLS, ROWS>>
where T: AsRef<str>,

Source§

fn from(mat: [[T; COLS]; ROWS]) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<I, D> Freeze for CompactTable<I, D>
where I: Freeze, D: Freeze,

§

impl<I, D> RefUnwindSafe for CompactTable<I, D>

§

impl<I, D> Send for CompactTable<I, D>
where I: Send, D: Send,

§

impl<I, D> Sync for CompactTable<I, D>
where I: Sync, D: Sync,

§

impl<I, D> Unpin for CompactTable<I, D>
where I: Unpin, D: Unpin,

§

impl<I, D> UnwindSafe for CompactTable<I, D>
where I: UnwindSafe, D: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.