Fix format width calculation in no_std
environments
This commit is contained in:
parent
52e6db99c6
commit
e0c7d8f684
@ -4,6 +4,7 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq};
|
|||||||
use std::any::TypeId;
|
use std::any::TypeId;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::fmt::Write;
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
@ -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 {
|
macro_rules! impl_fmt {
|
||||||
($trait: path, $fmt_str_without_precision: expr, $fmt_str_with_precision: expr) => {
|
($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>
|
impl<T, R: Dim, C: Dim, S> $trait for Matrix<T, R, C, S>
|
||||||
@ -1888,21 +1900,20 @@ macro_rules! impl_fmt {
|
|||||||
S: RawStorage<T, R, C>,
|
S: RawStorage<T, R, C>,
|
||||||
{
|
{
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
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 {
|
fn val_width<T: Scalar + $trait>(val: &T, f: &mut fmt::Formatter<'_>) -> usize {
|
||||||
|
let mut char_counter = CharCounter { count: 0 };
|
||||||
match f.precision() {
|
match f.precision() {
|
||||||
Some(precision) => format!($fmt_str_with_precision, val, precision)
|
Some(precision) => {
|
||||||
.chars()
|
write!(char_counter, $fmt_str_with_precision, val, precision).unwrap();
|
||||||
.count(),
|
char_counter.count
|
||||||
None => format!($fmt_str_without_precision, val).chars().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();
|
let (nrows, ncols) = self.shape();
|
||||||
|
|
||||||
if nrows == 0 || ncols == 0 {
|
if nrows == 0 || ncols == 0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user