const_panic/fmt_impls/
basic_fmt_impls.rs

1use crate::{
2    panic_val::{IntVal, PanicVal, PanicVariant, StrFmt},
3    utils::Packed,
4    FmtArg, PanicFmt, StdWrapper,
5};
6
7macro_rules! primitive_static_panicfmt {
8    (
9        fn[$($impl:tt)*](&$self:ident: $ty:ty, $f:ident) {
10            $($content:tt)*
11        }
12    ) => {
13        impl<$($impl)*> crate::PanicFmt for $ty {
14            type This = Self;
15            type Kind = crate::fmt::IsStdType;
16            const PV_COUNT: usize = 1;
17        }
18
19        impl<$($impl)*> crate::StdWrapper<&$ty> {
20            #[doc = concat!(
21                "Converts this `", stringify!($ty), "` to a single-element `PanicVal` array."
22            )]
23            pub const fn to_panicvals($self, $f: crate::FmtArg) -> [PanicVal<'static>; 1] {
24                [{
25                    $($content)*
26                }]
27            }
28
29            #[doc = concat!(
30                "Converts this `", stringify!($ty), "` to a `PanicVal`."
31            )]
32            pub const fn to_panicval($self, $f: crate::FmtArg) -> PanicVal<'static> {
33                $($content)*
34            }
35        }
36    }
37}
38pub(crate) use primitive_static_panicfmt;
39
40macro_rules! impl_panicfmt_panicval_array {
41    (
42        PV_COUNT = $pv_len:expr;
43        fn[$($impl:tt)*](&$self:ident: $ty:ty) -> $ret:ty {
44            $($content:tt)*
45        }
46    ) => (
47        impl<$($impl)*> PanicFmt for $ty {
48            type This = Self;
49            type Kind = crate::fmt::IsStdType;
50            const PV_COUNT: usize = $pv_len;
51        }
52
53        impl<'s, $($impl)*> StdWrapper<&'s $ty> {
54            ///
55            pub const fn to_panicvals($self: Self, _: FmtArg) -> $ret {
56                $($content)*
57            }
58        }
59    )
60}
61
62macro_rules! impl_panicfmt_panicarg {
63    (
64        fn $panic_arg_ctor:ident[$($impl:tt)*](
65            $this:ident:
66            $ty:ty,
67            $f:ident
68        ) -> PanicVal<$pa_lt:lifetime>
69        $panic_args:block
70
71
72    )=>{
73        impl PanicVal<'_> {
74            #[doc = concat!("Constructs a `PanicVal` from a `", stringify!($ty), "` .")]
75            pub const fn $panic_arg_ctor<$($impl)*>($this: $ty, $f: FmtArg) -> PanicVal<$pa_lt>
76            $panic_args
77        }
78
79        impl<$($impl)*> PanicFmt for $ty {
80            type This = Self;
81            type Kind = crate::fmt::IsStdType;
82
83            const PV_COUNT: usize = 1;
84        }
85
86        impl<'s, $($impl)*> StdWrapper<&'s $ty> {
87            #[doc = concat!(
88                "Formats this `", stringify!($ty),
89                "` into a single-`PanicVal` array",
90            )]
91            pub const fn to_panicvals(self: Self, f: FmtArg) -> [PanicVal<$pa_lt>;1] {
92                [PanicVal::$panic_arg_ctor(*self.0, f)]
93            }
94            #[doc = concat!("Formats this `", stringify!($ty), "` into a `PanicVal`")]
95            pub const fn to_panicval(self: Self, f: FmtArg) -> PanicVal<$pa_lt> {
96                PanicVal::$panic_arg_ctor(*self.0, f)
97            }
98        }
99    }
100}
101
102macro_rules! impl_panicfmt_int {
103    ($panic_arg_ctor:ident, $intarg_contructor:ident, $ty:ty) => {
104        impl PanicVal<'_> {
105            /// Constructs this `PanicVal` from an integer.
106            pub const fn $panic_arg_ctor(this: $ty, f: FmtArg) -> PanicVal<'static> {
107                const BITS: u8 = core::mem::size_of::<$ty>() as u8 * 8;
108                IntVal::$intarg_contructor(this as _, BITS, f)
109            }
110        }
111
112        primitive_static_panicfmt! {
113            fn[](&self: $ty, f) {
114                PanicVal::$panic_arg_ctor(*self.0, f)
115            }
116        }
117    };
118}
119
120impl_panicfmt_int! {from_u8, from_u128, u8}
121impl_panicfmt_int! {from_u16, from_u128, u16}
122impl_panicfmt_int! {from_u32, from_u128, u32}
123impl_panicfmt_int! {from_u64, from_u128, u64}
124impl_panicfmt_int! {from_u128, from_u128, u128}
125impl_panicfmt_int! {from_usize, from_u128, usize}
126
127impl_panicfmt_int! {from_i8, from_i128, i8}
128impl_panicfmt_int! {from_i16, from_i128, i16}
129impl_panicfmt_int! {from_i32, from_i128, i32}
130impl_panicfmt_int! {from_i64, from_i128, i64}
131impl_panicfmt_int! {from_i128, from_i128, i128}
132impl_panicfmt_int! {from_isize, from_i128, isize}
133
134impl_panicfmt_panicarg! {
135    fn from_bool[](this: bool, _f) -> PanicVal<'static> {
136        PanicVal::write_str(if this { "true" } else { "false" })
137    }
138}
139
140impl<'a> PanicVal<'a> {
141    /// Constructs a `PanicVal` from a `&str`
142    pub const fn from_str(this: &'a str, f: FmtArg) -> PanicVal<'a> {
143        PanicVal::__new(PanicVariant::Str(StrFmt::new(f), Packed(this)))
144    }
145}
146
147impl PanicFmt for str {
148    type This = Self;
149    type Kind = crate::fmt::IsStdType;
150    const PV_COUNT: usize = 1;
151}
152
153impl<'a> StdWrapper<&'a str> {
154    /// Formats this `&str` into a single-`PanicVal` array
155    pub const fn to_panicvals(self: Self, f: FmtArg) -> [PanicVal<'a>; 1] {
156        [PanicVal::from_str(self.0, f)]
157    }
158    /// Formats this `&str` into a `PanicVal`
159    pub const fn to_panicval(self: Self, f: FmtArg) -> PanicVal<'a> {
160        PanicVal::from_str(self.0, f)
161    }
162}
163
164impl_panicfmt_panicval_array! {
165    PV_COUNT = N;
166    fn['a, const N: usize](&self: [PanicVal<'a>; N]) -> &'s [PanicVal<'a>; N] {
167        self.0
168    }
169}
170
171impl_panicfmt_panicval_array! {
172    PV_COUNT = usize::MAX;
173    fn['a](&self: [PanicVal<'a>]) -> &'s [PanicVal<'a>] {
174        self.0
175    }
176}
177
178impl<'a, 'b> StdWrapper<&'a &'b [PanicVal<'b>]> {
179    /// Coerces a `&&[PanicVal<'_>]` into a `&[PanicVal<'_>]`
180    pub const fn deref_panic_vals(self) -> &'b [PanicVal<'b>] {
181        *self.0
182    }
183}
184impl<'a, 'b, const N: usize> StdWrapper<&'a &'b [PanicVal<'b>; N]> {
185    /// Coerces a `&&[PanicVal<'_>; N]` into a `&[PanicVal<'_>]`
186    pub const fn deref_panic_vals(self) -> &'b [PanicVal<'b>] {
187        *self.0
188    }
189}
190impl<'b, const N: usize> StdWrapper<&'b [PanicVal<'b>; N]> {
191    /// Coerces a `&[PanicVal<'_>; N]` into a `&[PanicVal<'_>]`
192    pub const fn deref_panic_vals(self) -> &'b [PanicVal<'b>] {
193        self.0
194    }
195}