Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix format width calculation in no_std environments #1196

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/base/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq};
use std::any::TypeId;
use std::cmp::Ordering;
use std::fmt;
use std::fmt::Write;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::mem;
Expand Down Expand Up @@ -1880,6 +1881,17 @@ where
}
}

struct CharCounter {
pub count: usize,
}

impl Write for CharCounter {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.count += s.chars().count();
Ok(())
}
}

macro_rules! impl_fmt {
($trait: path, $fmt_str_without_precision: expr, $fmt_str_with_precision: expr) => {
impl<T, R: Dim, C: Dim, S> $trait for Matrix<T, R, C, S>
Expand All @@ -1888,21 +1900,20 @@ macro_rules! impl_fmt {
S: RawStorage<T, R, C>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[cfg(feature = "std")]
fn val_width<T: Scalar + $trait>(val: &T, f: &mut fmt::Formatter<'_>) -> usize {
let mut char_counter = CharCounter { count: 0 };
match f.precision() {
Some(precision) => format!($fmt_str_with_precision, val, precision)
.chars()
.count(),
None => format!($fmt_str_without_precision, val).chars().count(),
Some(precision) => {
write!(char_counter, $fmt_str_with_precision, val, precision).unwrap();
char_counter.count
}
None => {
write!(char_counter, $fmt_str_without_precision, val).unwrap();
char_counter.count
}
}
}

#[cfg(not(feature = "std"))]
fn val_width<T: Scalar + $trait>(_: &T, _: &mut fmt::Formatter<'_>) -> usize {
4
}

let (nrows, ncols) = self.shape();

if nrows == 0 || ncols == 0 {
Expand Down