Skip to content

Commit b7d1f3e

Browse files
committed
Improve Debug implementation, add additional formatting traits
1 parent 613f242 commit b7d1f3e

File tree

5 files changed

+123
-6
lines changed

5 files changed

+123
-6
lines changed

crates/core_simd/src/fmt.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
macro_rules! debug_wrapper {
2+
{ $($trait:ident => $name:ident,)* } => {
3+
$(
4+
pub(crate) fn $name<T: core::fmt::$trait>(slice: &[T], f: &mut core::fmt::Formatter) -> core::fmt::Result {
5+
#[repr(transparent)]
6+
struct Wrapper<'a, T: core::fmt::$trait>(&'a T);
7+
8+
impl<T: core::fmt::$trait> core::fmt::Debug for Wrapper<'_, T> {
9+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
10+
self.0.fmt(f)
11+
}
12+
}
13+
14+
f.debug_list()
15+
.entries(slice.iter().map(|x| Wrapper(x)))
16+
.finish()
17+
}
18+
)*
19+
}
20+
}
21+
22+
debug_wrapper! {
23+
Debug => format,
24+
Binary => format_binary,
25+
LowerExp => format_lower_exp,
26+
UpperExp => format_upper_exp,
27+
Octal => format_octal,
28+
LowerHex => format_lower_hex,
29+
UpperHex => format_upper_hex,
30+
Pointer => format_pointer,
31+
}
32+
33+
macro_rules! impl_fmt_trait {
34+
{ $($type:ty => $(($trait:ident, $format:ident)),*;)* } => {
35+
$( // repeat type
36+
$( // repeat trait
37+
impl core::fmt::$trait for $type {
38+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
39+
$format(self.as_ref(), f)
40+
}
41+
}
42+
)*
43+
)*
44+
};
45+
{ integers: $($type:ty,)* } => {
46+
impl_fmt_trait! {
47+
$($type =>
48+
(Debug, format),
49+
(Binary, format_binary),
50+
(LowerExp, format_lower_exp),
51+
(UpperExp, format_upper_exp),
52+
(Octal, format_octal),
53+
(LowerHex, format_lower_hex),
54+
(UpperHex, format_upper_hex);
55+
)*
56+
}
57+
};
58+
{ floats: $($type:ty,)* } => {
59+
impl_fmt_trait! {
60+
$($type =>
61+
(Debug, format),
62+
(LowerExp, format_lower_exp),
63+
(UpperExp, format_upper_exp);
64+
)*
65+
}
66+
};
67+
{ masks: $($type:ty,)* } => {
68+
impl_fmt_trait! {
69+
$($type =>
70+
(Debug, format);
71+
)*
72+
}
73+
}
74+
}
75+
76+
impl_fmt_trait! {
77+
integers:
78+
crate::u8x2, crate::u8x4, crate::u8x8, crate::u8x16, crate::u8x32, crate::u8x64,
79+
crate::i8x2, crate::i8x4, crate::i8x8, crate::i8x16, crate::i8x32, crate::i8x64,
80+
crate::u16x2, crate::u16x4, crate::u16x8, crate::u16x16, crate::u16x32,
81+
crate::i16x2, crate::i16x4, crate::i16x8, crate::i16x16, crate::i16x32,
82+
crate::u32x2, crate::u32x4, crate::u32x8, crate::u32x16,
83+
crate::i32x2, crate::i32x4, crate::i32x8, crate::i32x16,
84+
crate::u64x2, crate::u64x4, crate::u64x8,
85+
crate::i64x2, crate::i64x4, crate::i64x8,
86+
crate::u128x2, crate::u128x4,
87+
crate::i128x2, crate::i128x4,
88+
crate::usizex2, crate::usizex4, crate::usizex8,
89+
crate::isizex2, crate::isizex4, crate::isizex8,
90+
}
91+
92+
impl_fmt_trait! {
93+
floats:
94+
crate::f32x2, crate::f32x4, crate::f32x8, crate::f32x16,
95+
crate::f64x2, crate::f64x4, crate::f64x8,
96+
}
97+
98+
impl_fmt_trait! {
99+
masks:
100+
crate::mask8x2, crate::mask8x4, crate::mask8x8, crate::mask8x16, crate::mask8x32, crate::mask8x64,
101+
crate::mask16x2, crate::mask16x4, crate::mask16x8, crate::mask16x16, crate::mask16x32,
102+
crate::mask32x2, crate::mask32x4, crate::mask32x8, crate::mask32x16,
103+
crate::mask64x2, crate::mask64x4, crate::mask64x8,
104+
crate::mask128x2, crate::mask128x4,
105+
crate::masksizex2, crate::masksizex4, crate::masksizex8,
106+
}

crates/core_simd/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#[macro_use]
77
mod macros;
88

9+
mod fmt;
10+
911
mod masks;
1012
pub use masks::*;
1113

crates/core_simd/src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ macro_rules! define_vector {
247247
{ def $(#[$attr:meta])* | $name:ident | $($itype:ty)* } => {
248248
$(#[$attr])*
249249
#[allow(non_camel_case_types)]
250-
#[derive(Copy, Clone, Debug, Default, PartialEq, PartialOrd)]
250+
#[derive(Copy, Clone, Default, PartialEq, PartialOrd)]
251251
#[repr(simd)]
252252
pub struct $name($($itype),*);
253253
};
@@ -284,7 +284,7 @@ macro_rules! define_mask_vector {
284284
{ def $(#[$attr:meta])* | $name:ident | $($itype:ty)* } => {
285285
$(#[$attr])*
286286
#[allow(non_camel_case_types)]
287-
#[derive(Copy, Clone, Debug, Default, PartialEq, PartialOrd, Eq, Ord)]
287+
#[derive(Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord)]
288288
#[repr(simd)]
289289
pub struct $name($($itype),*);
290290
};

crates/core_simd/src/masks.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ macro_rules! define_mask {
22
{ $(#[$attr:meta])* struct $name:ident($type:ty); } => {
33
$(#[$attr])*
44
#[allow(non_camel_case_types)]
5-
#[derive(Copy, Clone, Debug, Default, PartialEq, PartialOrd, Eq, Ord, Hash)]
5+
#[derive(Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash)]
66
#[repr(transparent)]
77
pub struct $name(pub(crate) $type);
88

@@ -33,6 +33,12 @@ macro_rules! define_mask {
3333
mask.test()
3434
}
3535
}
36+
37+
impl core::fmt::Debug for $name {
38+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
39+
self.test().fmt(f)
40+
}
41+
}
3642
}
3743
}
3844

crates/core_simd/src/pointers.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,12 @@ macro_rules! define_pointer_vector {
100100
{ debug $name:ident | $type:ty | $($index:tt)* } => {
101101
impl<T> core::fmt::Debug for $name<T> {
102102
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
103-
f.debug_tuple(stringify!($name))
104-
$(.field(&(AsRef::<[isize]>::as_ref(&self.0)[$index] as $type)))*
105-
.finish()
103+
crate::fmt::format(self.as_ref(), f)
104+
}
105+
}
106+
impl<T> core::fmt::Pointer for $name<T> {
107+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
108+
crate::fmt::format_pointer(self.as_ref(), f)
106109
}
107110
}
108111
}

0 commit comments

Comments
 (0)