Skip to content

Commit ceb2611

Browse files
Remove formats [T; N] does not impl (#337)
Remove these extra formatting traits, as they are inconsistent with how arrays and slices format, and it can cause unnecessary code bloat in binaries. We can revisit this if people ever agree on doing these formatters for the other slice-y types. Prefer to dispatch to the `impl `fmt::Debug for [T]`, to reduce the chances of monomorphizing twice. Inlining it seems like a good idea for similar reasons?
1 parent 90f2af7 commit ceb2611

File tree

1 file changed

+16
-34
lines changed

1 file changed

+16
-34
lines changed

Diff for: crates/core_simd/src/fmt.rs

+16-34
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,21 @@
11
use crate::simd::{LaneCount, Simd, SimdElement, SupportedLaneCount};
22
use core::fmt;
33

4-
macro_rules! impl_fmt_trait {
5-
{ $($trait:ident,)* } => {
6-
$(
7-
impl<T, const LANES: usize> fmt::$trait for Simd<T, LANES>
8-
where
9-
LaneCount<LANES>: SupportedLaneCount,
10-
T: SimdElement + fmt::$trait,
11-
{
12-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13-
#[repr(transparent)]
14-
struct Wrapper<'a, T: fmt::$trait>(&'a T);
15-
16-
impl<T: fmt::$trait> fmt::Debug for Wrapper<'_, T> {
17-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18-
self.0.fmt(f)
19-
}
20-
}
21-
22-
f.debug_list()
23-
.entries(self.as_array().iter().map(|x| Wrapper(x)))
24-
.finish()
25-
}
26-
}
27-
)*
4+
impl<T, const LANES: usize> fmt::Debug for Simd<T, LANES>
5+
where
6+
LaneCount<LANES>: SupportedLaneCount,
7+
T: SimdElement + fmt::Debug,
8+
{
9+
/// A `Simd<T, N>` has a debug format like the one for `[T]`:
10+
/// ```
11+
/// # #![feature(portable_simd)]
12+
/// # #[cfg(feature = "as_crate")] use core_simd::simd::Simd;
13+
/// # #[cfg(not(feature = "as_crate"))] use core::simd::Simd;
14+
/// let floats = Simd::<f32, 4>::splat(-1.0);
15+
/// assert_eq!(format!("{:?}", [-1.0; 4]), format!("{:?}", floats));
16+
/// ```
17+
#[inline]
18+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19+
<[T] as fmt::Debug>::fmt(self.as_array(), f)
2820
}
2921
}
30-
31-
impl_fmt_trait! {
32-
Debug,
33-
Binary,
34-
LowerExp,
35-
UpperExp,
36-
Octal,
37-
LowerHex,
38-
UpperHex,
39-
}

0 commit comments

Comments
 (0)