nalgebra icon indicating copy to clipboard operation
nalgebra copied to clipboard

Fix format width calculation in `no_std` environments

Open matteocarnelos opened this issue 3 years ago • 0 comments

When disabling the std feature, the calculation of the formatted width of the values is skipped and replaced by a fixed number (i.e. 4). This causes the output to be wrongly formatted when printing. I fixed this issue by implementing a custom writer, that implements the fmt::Write trait, and counts the number of character in the write_str() method, instead of printing them.

How to reproduce the issue

Cargo.toml

[dependencies]
nalgebra = { version = "0.32", default-features = false, features = ["macros"] }

main.rs

use nalgebra::matrix;

fn main() {
    println!("{}", matrix![1, 2, 3]);
}

Output:

  ┌                ┐
  │ 1 2 3 │
  └                ┘

How to test the fix

Cargo.toml

[dependencies]
nalgebra = { version = "0.32", default-features = false, features = ["macros"] }
[patch.crates-io]
nalgebra = { git = "https://github.com/matteocarnelos/nalgebra" }

main.rs

use nalgebra::matrix;

fn main() {
    println!("{}", matrix![1, 2, 3]);
}

Output:

  ┌       ┐
  │ 1 2 3 │
  └       ┘

matteocarnelos avatar Jan 20 '23 13:01 matteocarnelos