ufmt/impls/
uxx.rs

1use core::{mem::MaybeUninit, slice, str};
2
3use crate::{uDebug, uDisplay, uWrite, Formatter};
4
5macro_rules! uxx {
6    ($n:expr, $buf:expr) => {{
7        let ptr = $buf.as_mut_ptr().cast::<u8>();
8        let len = $buf.len();
9        let mut n = $n;
10        let mut i = len - 1;
11        loop {
12            unsafe { ptr.add(i).write((n % 10) as u8 + b'0') }
13            n /= 10;
14
15            if n == 0 {
16                break;
17            } else {
18                i -= 1;
19            }
20        }
21
22        unsafe { str::from_utf8_unchecked(slice::from_raw_parts(ptr.add(i), len - i)) }
23    }};
24}
25
26fn usize(n: usize, buf: &mut [MaybeUninit<u8>]) -> &str {
27    uxx!(n, buf)
28}
29
30impl uDebug for u8 {
31    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
32    where
33        W: uWrite + ?Sized,
34    {
35        let mut buf = [MaybeUninit::uninit(); 3];
36
37        f.write_str(usize(usize::from(*self), &mut buf))
38    }
39}
40
41impl uDisplay for u8 {
42    #[inline(always)]
43    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
44    where
45        W: uWrite + ?Sized,
46    {
47        <u8 as uDebug>::fmt(self, f)
48    }
49}
50
51impl uDebug for u16 {
52    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
53    where
54        W: uWrite + ?Sized,
55    {
56        let mut buf = [MaybeUninit::uninit(); 5];
57
58        f.write_str(usize(usize::from(*self), &mut buf))
59    }
60}
61
62impl uDisplay for u16 {
63    #[inline(always)]
64    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
65    where
66        W: uWrite + ?Sized,
67    {
68        <u16 as uDebug>::fmt(self, f)
69    }
70}
71
72impl uDebug for u32 {
73    #[cfg(not(target_pointer_width = "16"))]
74    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
75    where
76        W: uWrite + ?Sized,
77    {
78        let mut buf = [MaybeUninit::uninit(); 10];
79
80        f.write_str(usize(*self as usize, &mut buf))
81    }
82
83    #[cfg(target_pointer_width = "16")]
84    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
85    where
86        W: uWrite + ?Sized,
87    {
88        let mut buf = [MaybeUninit::<u8>::uninit(); 10];
89
90        let s = uxx!(*self, buf);
91        f.write_str(s)
92    }
93}
94
95impl uDisplay for u32 {
96    #[inline(always)]
97    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
98    where
99        W: uWrite + ?Sized,
100    {
101        <u32 as uDebug>::fmt(self, f)
102    }
103}
104
105impl uDebug for u64 {
106    #[cfg(any(target_pointer_width = "32", target_pointer_width = "16"))]
107    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
108    where
109        W: uWrite + ?Sized,
110    {
111        let mut buf = [MaybeUninit::<u8>::uninit(); 20];
112
113        let s = uxx!(*self, buf);
114        f.write_str(s)
115    }
116
117    #[cfg(target_pointer_width = "64")]
118    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
119    where
120        W: uWrite + ?Sized,
121    {
122        let mut buf = [MaybeUninit::uninit(); 20];
123
124        f.write_str(usize(*self as usize, &mut buf))
125    }
126}
127
128impl uDisplay for u64 {
129    #[inline(always)]
130    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
131    where
132        W: uWrite + ?Sized,
133    {
134        <u64 as uDebug>::fmt(self, f)
135    }
136}
137
138impl uDebug for u128 {
139    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
140    where
141        W: uWrite + ?Sized,
142    {
143        let mut buf = [MaybeUninit::<u8>::uninit(); 39];
144
145        let s = uxx!(*self, buf);
146        f.write_str(s)
147    }
148}
149
150impl uDisplay for u128 {
151    #[inline(always)]
152    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
153    where
154        W: uWrite + ?Sized,
155    {
156        <u128 as uDebug>::fmt(self, f)
157    }
158}
159
160impl uDebug for usize {
161    #[cfg(target_pointer_width = "16")]
162    #[inline(always)]
163    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
164    where
165        W: uWrite + ?Sized,
166    {
167        <u16 as uDebug>::fmt(&(*self as u16), f)
168    }
169
170    #[cfg(target_pointer_width = "32")]
171    #[inline(always)]
172    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
173    where
174        W: uWrite + ?Sized,
175    {
176        <u32 as uDebug>::fmt(&(*self as u32), f)
177    }
178
179    #[cfg(target_pointer_width = "64")]
180    #[inline(always)]
181    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
182    where
183        W: uWrite + ?Sized,
184    {
185        <u64 as uDebug>::fmt(&(*self as u64), f)
186    }
187}
188
189impl uDisplay for usize {
190    #[cfg(target_pointer_width = "16")]
191    #[inline(always)]
192    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
193    where
194        W: uWrite + ?Sized,
195    {
196        <u16 as uDisplay>::fmt(&(*self as u16), f)
197    }
198
199    #[cfg(target_pointer_width = "32")]
200    #[inline(always)]
201    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
202    where
203        W: uWrite + ?Sized,
204    {
205        <u32 as uDisplay>::fmt(&(*self as u32), f)
206    }
207
208    #[cfg(target_pointer_width = "64")]
209    #[inline(always)]
210    fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
211    where
212        W: uWrite + ?Sized,
213    {
214        <u64 as uDisplay>::fmt(&(*self as u64), f)
215    }
216}