Skip to content

Commit

Permalink
Respect precision format specifier in Display implementations. (#491)
Browse files Browse the repository at this point in the history
Fixes #271
  • Loading branch information
bitshifter authored Mar 11, 2024
1 parent ab85e2a commit 35a2f76
Show file tree
Hide file tree
Showing 51 changed files with 439 additions and 103 deletions.
30 changes: 21 additions & 9 deletions codegen/templates/affine.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -734,15 +734,27 @@ impl core::fmt::Debug for {{ self_t }} {
#[cfg(not(target_arch = "spirv"))]
impl core::fmt::Display for {{ self_t }} {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
{% if dim == 2 %}
write!(f, "[{}, {}, {}]", self.matrix2.x_axis, self.matrix2.y_axis, self.translation)
{% elif dim == 3 %}
write!(
f,
"[{}, {}, {}, {}]",
self.matrix3.x_axis, self.matrix3.y_axis, self.matrix3.z_axis, self.translation
)
{% endif %}
if let Some(p) = f.precision() {
{% if dim == 2 %}
write!(f,
"[{:.*}, {:.*}, {:.*}]",
p, self.matrix2.x_axis, p, self.matrix2.y_axis, p, self.translation)
{% elif dim == 3 %}
write!(f,
"[{:.*}, {:.*}, {:.*}, {:.*}]",
p, self.matrix3.x_axis, p, self.matrix3.y_axis, p, self.matrix3.z_axis, p, self.translation)
{% endif %}
} else {
{% if dim == 2 %}
write!(f, "[{}, {}, {}]", self.matrix2.x_axis, self.matrix2.y_axis, self.translation)
{% elif dim == 3 %}
write!(
f,
"[{}, {}, {}, {}]",
self.matrix3.x_axis, self.matrix3.y_axis, self.matrix3.z_axis, self.translation
)
{% endif %}
}
}
}

Expand Down
24 changes: 17 additions & 7 deletions codegen/templates/mat.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -2402,13 +2402,23 @@ impl fmt::Debug for {{ self_t }} {
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for {{ self_t }} {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
{% if dim == 2 %}
write!(f, "[{}, {}]", self.x_axis, self.y_axis)
{% elif dim == 3 %}
write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
{% elif dim == 4 %}
write!(f, "[{}, {}, {}, {}]", self.x_axis, self.y_axis, self.z_axis, self.w_axis)
{% endif %}
if let Some(p) = f.precision() {
{% if dim == 2 %}
write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
{% elif dim == 3 %}
write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x_axis, p, self.y_axis, p, self.z_axis)
{% elif dim == 4 %}
write!(f, "[{:.*}, {:.*}, {:.*}, {:.*}]", p, self.x_axis, p, self.y_axis, p, self.z_axis, p, self.w_axis)
{% endif %}
} else {
{% if dim == 2 %}
write!(f, "[{}, {}]", self.x_axis, self.y_axis)
{% elif dim == 3 %}
write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
{% elif dim == 4 %}
write!(f, "[{}, {}, {}, {}]", self.x_axis, self.y_axis, self.z_axis, self.w_axis)
{% endif %}
}
}
}

8 changes: 6 additions & 2 deletions codegen/templates/quat.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -1089,8 +1089,12 @@ impl fmt::Debug for {{ self_t }} {

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for {{ self_t }} {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(p) = f.precision() {
write!(f, "[{:.*}, {:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z, p, self.w)
} else {
write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
}
}
}

Expand Down
26 changes: 20 additions & 6 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -2958,12 +2958,26 @@ impl IndexMut<usize> for {{ self_t }} {
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for {{ self_t }} {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
{% if dim == 2 %}
write!(f, "[{}, {}]", self.x, self.y)
{% elif dim == 3 %}
write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
{% elif dim == 4 %}
write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
{% if is_float %}
if let Some(p) = f.precision() {
{% if dim == 2 %}
write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
{% elif dim == 3 %}
write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
{% elif dim == 4 %}
write!(f, "[{:.*}, {:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z, p, self.w)
{% endif %}
} else {
{% endif %}
{% if dim == 2 %}
write!(f, "[{}, {}]", self.x, self.y)
{% elif dim == 3 %}
write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
{% elif dim == 4 %}
write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
{% endif %}
{% if is_float %}
}
{% endif %}
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/f32/affine2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,19 @@ impl core::fmt::Debug for Affine2 {
#[cfg(not(target_arch = "spirv"))]
impl core::fmt::Display for Affine2 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"[{}, {}, {}]",
self.matrix2.x_axis, self.matrix2.y_axis, self.translation
)
if let Some(p) = f.precision() {
write!(
f,
"[{:.*}, {:.*}, {:.*}]",
p, self.matrix2.x_axis, p, self.matrix2.y_axis, p, self.translation
)
} else {
write!(
f,
"[{}, {}, {}]",
self.matrix2.x_axis, self.matrix2.y_axis, self.translation
)
}
}
}

Expand Down
25 changes: 20 additions & 5 deletions src/f32/affine3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,26 @@ impl core::fmt::Debug for Affine3A {
#[cfg(not(target_arch = "spirv"))]
impl core::fmt::Display for Affine3A {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"[{}, {}, {}, {}]",
self.matrix3.x_axis, self.matrix3.y_axis, self.matrix3.z_axis, self.translation
)
if let Some(p) = f.precision() {
write!(
f,
"[{:.*}, {:.*}, {:.*}, {:.*}]",
p,
self.matrix3.x_axis,
p,
self.matrix3.y_axis,
p,
self.matrix3.z_axis,
p,
self.translation
)
} else {
write!(
f,
"[{}, {}, {}, {}]",
self.matrix3.x_axis, self.matrix3.y_axis, self.matrix3.z_axis, self.translation
)
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/f32/coresimd/mat2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ impl fmt::Debug for Mat2 {
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Mat2 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}]", self.x_axis, self.y_axis)
if let Some(p) = f.precision() {
write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
} else {
write!(f, "[{}, {}]", self.x_axis, self.y_axis)
}
}
}
10 changes: 9 additions & 1 deletion src/f32/coresimd/mat3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,14 @@ impl fmt::Debug for Mat3A {
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Mat3A {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
if let Some(p) = f.precision() {
write!(
f,
"[{:.*}, {:.*}, {:.*}]",
p, self.x_axis, p, self.y_axis, p, self.z_axis
)
} else {
write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
}
}
}
18 changes: 13 additions & 5 deletions src/f32/coresimd/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1451,10 +1451,18 @@ impl fmt::Debug for Mat4 {
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Mat4 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"[{}, {}, {}, {}]",
self.x_axis, self.y_axis, self.z_axis, self.w_axis
)
if let Some(p) = f.precision() {
write!(
f,
"[{:.*}, {:.*}, {:.*}, {:.*}]",
p, self.x_axis, p, self.y_axis, p, self.z_axis, p, self.w_axis
)
} else {
write!(
f,
"[{}, {}, {}, {}]",
self.x_axis, self.y_axis, self.z_axis, self.w_axis
)
}
}
}
12 changes: 10 additions & 2 deletions src/f32/coresimd/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,16 @@ impl fmt::Debug for Quat {

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Quat {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(p) = f.precision() {
write!(
f,
"[{:.*}, {:.*}, {:.*}, {:.*}]",
p, self.x, p, self.y, p, self.z, p, self.w
)
} else {
write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/f32/coresimd/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,11 @@ impl IndexMut<usize> for Vec3A {
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Vec3A {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
if let Some(p) = f.precision() {
write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
} else {
write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/f32/coresimd/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,15 @@ impl IndexMut<usize> for Vec4 {
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Vec4 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
if let Some(p) = f.precision() {
write!(
f,
"[{:.*}, {:.*}, {:.*}, {:.*}]",
p, self.x, p, self.y, p, self.z, p, self.w
)
} else {
write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/f32/mat3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,14 @@ impl fmt::Debug for Mat3 {
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Mat3 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
if let Some(p) = f.precision() {
write!(
f,
"[{:.*}, {:.*}, {:.*}]",
p, self.x_axis, p, self.y_axis, p, self.z_axis
)
} else {
write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
}
}
}
6 changes: 5 additions & 1 deletion src/f32/scalar/mat2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ impl fmt::Debug for Mat2 {
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Mat2 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}]", self.x_axis, self.y_axis)
if let Some(p) = f.precision() {
write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
} else {
write!(f, "[{}, {}]", self.x_axis, self.y_axis)
}
}
}
10 changes: 9 additions & 1 deletion src/f32/scalar/mat3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,14 @@ impl fmt::Debug for Mat3A {
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Mat3A {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
if let Some(p) = f.precision() {
write!(
f,
"[{:.*}, {:.*}, {:.*}]",
p, self.x_axis, p, self.y_axis, p, self.z_axis
)
} else {
write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
}
}
}
18 changes: 13 additions & 5 deletions src/f32/scalar/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1363,10 +1363,18 @@ impl fmt::Debug for Mat4 {
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Mat4 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"[{}, {}, {}, {}]",
self.x_axis, self.y_axis, self.z_axis, self.w_axis
)
if let Some(p) = f.precision() {
write!(
f,
"[{:.*}, {:.*}, {:.*}, {:.*}]",
p, self.x_axis, p, self.y_axis, p, self.z_axis, p, self.w_axis
)
} else {
write!(
f,
"[{}, {}, {}, {}]",
self.x_axis, self.y_axis, self.z_axis, self.w_axis
)
}
}
}
12 changes: 10 additions & 2 deletions src/f32/scalar/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,8 +721,16 @@ impl fmt::Debug for Quat {

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Quat {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(p) = f.precision() {
write!(
f,
"[{:.*}, {:.*}, {:.*}, {:.*}]",
p, self.x, p, self.y, p, self.z, p, self.w
)
} else {
write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/f32/scalar/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,11 @@ impl IndexMut<usize> for Vec3A {
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Vec3A {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
if let Some(p) = f.precision() {
write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
} else {
write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/f32/scalar/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,15 @@ impl IndexMut<usize> for Vec4 {
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Vec4 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
if let Some(p) = f.precision() {
write!(
f,
"[{:.*}, {:.*}, {:.*}, {:.*}]",
p, self.x, p, self.y, p, self.z, p, self.w
)
} else {
write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
}
}
}

Expand Down
Loading

0 comments on commit 35a2f76

Please sign in to comment.