const_format/fmt/
std_type_impls.rs

1#![allow(missing_docs)]
2
3use crate::{
4    fmt::{Error, Formatter},
5    marker_traits::IsStdKind,
6    wrapper_types::PWrapper,
7};
8
9mod ranges;
10
11////////////////////////////////////////////////////////////////////////////////
12
13impl PWrapper<&str> {
14    pub const fn const_display_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
15        f.write_str(self.0)
16    }
17
18    pub const fn const_debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
19        f.write_str_debug(self.0)
20    }
21}
22
23impl PWrapper<bool> {
24    pub const fn const_display_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
25        f.write_str(if self.0 { "true" } else { "false" })
26    }
27
28    #[inline(always)]
29    pub const fn const_debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
30        self.const_display_fmt(f)
31    }
32}
33
34impl PWrapper<char> {
35    pub const fn const_display_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
36        f.write_char(self.0)
37    }
38
39    #[inline(always)]
40    pub const fn const_debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
41        f.write_char_debug(self.0)
42    }
43}
44
45macro_rules! slice_of_std_impl {($($elem:ty),* $(,)?) => (
46    $(
47
48        impl PWrapper<&[$elem]> {
49            pub const fn const_debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
50                let mut f = f.debug_list();
51                __for_range!{i in 0..self.0.len() =>
52                    try_!(PWrapper(self.0[i]).const_debug_fmt(f.entry()));
53                }
54                f.finish()
55            }
56        }
57    )*
58)}
59
60slice_of_std_impl! {
61    &str,
62    bool,
63    char,
64    u8, i8,
65    u16, i16,
66    u32, i32,
67    u64, i64,
68    u128, i128,
69    usize, isize,
70}
71
72////////////////////////////////////////////////////////////////////////////////
73
74use core::{
75    marker::{PhantomData, PhantomPinned},
76    num::{
77        NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
78        NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
79    },
80    ptr::NonNull,
81};
82
83impl_fmt! {
84    is_std_type;
85
86    impl[T,] Option<NonNull<T>>;
87    impl[] Option<NonZeroU8>;
88    impl[] Option<NonZeroI8>;
89    impl[] Option<NonZeroU16>;
90    impl[] Option<NonZeroI16>;
91    impl[] Option<NonZeroU32>;
92    impl[] Option<NonZeroI32>;
93    impl[] Option<NonZeroU64>;
94    impl[] Option<NonZeroI64>;
95    impl[] Option<NonZeroU128>;
96    impl[] Option<NonZeroI128>;
97    impl[] Option<NonZeroUsize>;
98    impl[] Option<NonZeroIsize>;
99    impl[] Option<u8>;
100    impl[] Option<i8>;
101    impl[] Option<u16>;
102    impl[] Option<i16>;
103    impl[] Option<u32>;
104    impl[] Option<i32>;
105    impl[] Option<u64>;
106    impl[] Option<i64>;
107    impl[] Option<u128>;
108    impl[] Option<i128>;
109    impl[] Option<usize>;
110    impl[] Option<isize>;
111    impl[] Option<bool>;
112    impl[] Option<char>;
113    impl['a,] Option<&'a str>;
114
115    pub const fn const_debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
116        match self.0 {
117            Some(x) => {
118                let mut f = f.debug_tuple("Some");
119                try_!(PWrapper(x).const_debug_fmt(f.field()));
120                f.finish()
121            },
122            None => f.write_str("None"),
123        }
124    }
125}
126
127macro_rules! non_zero_impls {
128    ($($ty:ident,)*) => (
129        $(
130            std_kind_impl!{ impl[] $ty }
131
132            impl PWrapper<$ty> {
133                #[inline(always)]
134                pub const fn const_debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
135                    PWrapper(self.0.get()).const_debug_fmt(f)
136                }
137
138                #[inline(always)]
139                pub const fn const_display_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
140                    PWrapper(self.0.get()).const_display_fmt(f)
141                }
142            }
143        )*
144    )
145}
146
147non_zero_impls! {
148    NonZeroU8, NonZeroI8, NonZeroU16, NonZeroI16,
149    NonZeroU32, NonZeroI32, NonZeroU64, NonZeroI64,
150    NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize,
151}
152
153std_kind_impl! { impl[T,] *mut T }
154// Unfortunately, can't print pointer addresses at compile-time.
155impl<T> PWrapper<*mut T> {
156    const PTR: &'static str = "<pointer>";
157
158    #[inline(always)]
159    pub const fn const_debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
160        f.write_str(Self::PTR)
161    }
162}
163
164std_kind_impl! { impl[T,] *const T }
165impl<T> PWrapper<*const T> {
166    #[inline(always)]
167    pub const fn const_debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
168        PWrapper(self.0 as *mut T).const_debug_fmt(f)
169    }
170}
171
172std_kind_impl! { impl[T,] NonNull<T> }
173impl<T> PWrapper<NonNull<T>> {
174    #[inline(always)]
175    pub const fn const_debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
176        PWrapper(self.0.as_ptr()).const_debug_fmt(f)
177    }
178}
179
180macro_rules! impl_std_marker_type {
181    (
182        $( impl[$($impl_:tt)*] $type:ty = $tyname:expr ;)*
183    ) => (
184        $(
185            std_kind_impl!{ impl[$($impl_)*] $type }
186
187            impl<$($impl_)*> PWrapper<$type> {
188                const NAME: &'static str = $tyname;
189
190                #[inline(always)]
191                pub const fn const_debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
192                    PWrapper(Self::NAME).const_display_fmt(f)
193                }
194            }
195        )*
196    )
197}
198
199impl_std_marker_type! {
200    impl[T: ?Sized,] PhantomData<T> = "PhantomData";
201    impl[] PhantomPinned = "PhantomPinned";
202    impl[] () = "()";
203}
204
205////////////////////////////////////////////////////////////////////////////////
206
207use core::{cmp::Ordering, sync::atomic::Ordering as AtomicOrdering};
208
209impl_fmt! {
210    is_std_type;
211
212    impl AtomicOrdering;
213
214    pub const fn const_debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
215        match self.0 {
216            AtomicOrdering::Relaxed => f.write_str("Relaxed"),
217            AtomicOrdering::Release => f.write_str("Release"),
218            AtomicOrdering::Acquire => f.write_str("Acquire"),
219            AtomicOrdering::AcqRel => f.write_str("AcqRel"),
220            AtomicOrdering::SeqCst => f.write_str("SeqCst"),
221            _ => f.write_str("<core::atomic::Ordering>"),
222        }
223    }
224}
225
226impl_fmt! {
227    is_std_type;
228
229    impl Ordering;
230
231    pub const fn const_debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
232        match self.0 {
233            Ordering::Less => f.write_str("Less"),
234            Ordering::Equal => f.write_str("Equal"),
235            Ordering::Greater => f.write_str("Greater"),
236        }
237    }
238}