prettytable-rs icon indicating copy to clipboard operation
prettytable-rs copied to clipboard

Add trait bound ToString to Cell::new()/Cell::new_align()

Open iosmanthus opened this issue 6 years ago • 6 comments

After adding trait bound ToString to Cell::new()/Cell::new_align(), maybe it will be more convenient to construct a row from another type because it will be unnecessary to transform the content to &str explicitly.

iosmanthus avatar Oct 21 '18 02:10 iosmanthus

Codecov Report

Merging #94 into master will increase coverage by 0.08%. The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #94      +/-   ##
==========================================
+ Coverage   84.05%   84.13%   +0.08%     
==========================================
  Files           5        5              
  Lines        1179     1179              
==========================================
+ Hits          991      992       +1     
+ Misses        188      187       -1
Impacted Files Coverage Δ
src/cell.rs 86.02% <100%> (+0.43%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 6f01431...d6db8b8. Read the comment docs.

codecov[bot] avatar Oct 21 '18 02:10 codecov[bot]

If I want to construct a Table from a matrix dynamically, I can implement it in the following code.

fn pretty_matrix(matrix: [[u8; 16]; 16]) -> Table {
    let mut table = Table::new();
    matrix.iter().for_each(|line| {
        table.add_row(Row::new(
            line.iter().map(|x| Cell::new(&x.to_string())).collect::<Vec<_>>(),
        ));
    });
    table
}

Now, the code becomes:

fn pretty_matrix(matrix: [[u8; 16]; 16]) -> Table {
    let mut table = Table::new();
    matrix.iter().for_each(|line| {
        table.add_row(Row::new(
            line.iter().map(|x| Cell::new(x)).collect::<Vec<_>>(),
        ));
    });
    table
}

iosmanthus avatar Oct 21 '18 04:10 iosmanthus

@phsym ping

iosmanthus avatar Sep 12 '19 08:09 iosmanthus

@phsym ping

iosmanthus avatar Sep 23 '21 09:09 iosmanthus

@phsym ping

Jedsek avatar Aug 21 '22 09:08 Jedsek

@iosmanthus cell!() could convert expression to string automatically, but Cell::new has no appropriate trait bounds.It is so strange.

Jedsek avatar Aug 21 '22 09:08 Jedsek