-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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?
- Loading branch information
1 parent
90f2af7
commit ceb2611
Showing
1 changed file
with
16 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,21 @@ | ||
use crate::simd::{LaneCount, Simd, SimdElement, SupportedLaneCount}; | ||
use core::fmt; | ||
|
||
macro_rules! impl_fmt_trait { | ||
{ $($trait:ident,)* } => { | ||
$( | ||
impl<T, const LANES: usize> fmt::$trait for Simd<T, LANES> | ||
where | ||
LaneCount<LANES>: SupportedLaneCount, | ||
T: SimdElement + fmt::$trait, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
#[repr(transparent)] | ||
struct Wrapper<'a, T: fmt::$trait>(&'a T); | ||
|
||
impl<T: fmt::$trait> fmt::Debug for Wrapper<'_, T> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.0.fmt(f) | ||
} | ||
} | ||
|
||
f.debug_list() | ||
.entries(self.as_array().iter().map(|x| Wrapper(x))) | ||
.finish() | ||
} | ||
} | ||
)* | ||
impl<T, const LANES: usize> fmt::Debug for Simd<T, LANES> | ||
where | ||
LaneCount<LANES>: SupportedLaneCount, | ||
T: SimdElement + fmt::Debug, | ||
{ | ||
/// A `Simd<T, N>` has a debug format like the one for `[T]`: | ||
/// ``` | ||
/// # #![feature(portable_simd)] | ||
/// # #[cfg(feature = "as_crate")] use core_simd::simd::Simd; | ||
/// # #[cfg(not(feature = "as_crate"))] use core::simd::Simd; | ||
/// let floats = Simd::<f32, 4>::splat(-1.0); | ||
/// assert_eq!(format!("{:?}", [-1.0; 4]), format!("{:?}", floats)); | ||
/// ``` | ||
#[inline] | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
<[T] as fmt::Debug>::fmt(self.as_array(), f) | ||
} | ||
} | ||
|
||
impl_fmt_trait! { | ||
Debug, | ||
Binary, | ||
LowerExp, | ||
UpperExp, | ||
Octal, | ||
LowerHex, | ||
UpperHex, | ||
} |