| 1 | use crate::export; |
| 2 | |
| 3 | use super::*; |
| 4 | |
| 5 | macro_rules! prim { |
| 6 | ($ty:ty, $fmt: literal, $self_:ident, $write:expr) => { |
| 7 | impl Format for $ty { |
| 8 | default_format!(); |
| 9 | |
| 10 | #[inline] |
| 11 | fn _format_tag() -> Str { |
| 12 | internp!($fmt) |
| 13 | } |
| 14 | |
| 15 | #[inline] |
| 16 | fn _format_data(&$self_) { |
| 17 | $write |
| 18 | } |
| 19 | } |
| 20 | }; |
| 21 | } |
| 22 | |
| 23 | prim!(i8, "{=i8}" , self, export::i8(self)); |
| 24 | prim!(i16, "{=i16}" , self, export::i16(self)); |
| 25 | prim!(i32, "{=i32}" , self, export::i32(self)); |
| 26 | prim!(i64, "{=i64}" , self, export::i64(self)); |
| 27 | prim!(i128, "{=i128}" , self, export::i128(self)); |
| 28 | prim!(isize, "{=isize}" , self, export::isize(self)); |
| 29 | prim!(u8, "{=u8}" , self, export::u8(self)); |
| 30 | prim!(u16, "{=u16}" , self, export::u16(self)); |
| 31 | prim!(u32, "{=u32}" , self, export::u32(self)); |
| 32 | prim!(u64, "{=u64}" , self, export::u64(self)); |
| 33 | prim!(u128, "{=u128}" , self, export::u128(self)); |
| 34 | prim!(usize, "{=usize}" , self, export::usize(self)); |
| 35 | prim!(f32, "{=f32}" , self, export::f32(self)); |
| 36 | prim!(f64, "{=f64}" , self, export::f64(self)); |
| 37 | prim!(str, "{=str}" , self, export::str(self)); |
| 38 | prim!(bool, "{=bool}" , self, export::bool(self)); |
| 39 | prim!(Str, "{=istr}" , self, export::istr(self)); |
| 40 | prim!(char, "{=char}" , self, export::char(self)); |
| 41 | |
| 42 | impl<T> Format for [T] |
| 43 | where |
| 44 | T: Format, |
| 45 | { |
| 46 | default_format!(); |
| 47 | |
| 48 | #[inline ] |
| 49 | fn _format_tag() -> Str { |
| 50 | internp!("{=[?]}" ) |
| 51 | } |
| 52 | |
| 53 | #[inline ] |
| 54 | fn _format_data(&self) { |
| 55 | export::fmt_slice(self); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl<T> Format for &'_ T |
| 60 | where |
| 61 | T: Format + ?Sized, |
| 62 | { |
| 63 | delegate_format!(T, self, self); |
| 64 | } |
| 65 | |
| 66 | impl<T> Format for &'_ mut T |
| 67 | where |
| 68 | T: Format + ?Sized, |
| 69 | { |
| 70 | delegate_format!(T, self, self); |
| 71 | } |
| 72 | |
| 73 | // Format raw pointer as hexadecimal |
| 74 | // |
| 75 | // First cast raw pointer to thin pointer, then to usize and finally format as hexadecimal. |
| 76 | impl<T> Format for *const T |
| 77 | where |
| 78 | T: ?Sized, |
| 79 | { |
| 80 | fn format(&self, fmt: Formatter) { |
| 81 | crate::write!(fmt, "0x{:x}" , *self as *const () as usize); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | impl<T> Format for *mut T |
| 86 | where |
| 87 | T: ?Sized, |
| 88 | { |
| 89 | fn format(&self, fmt: Formatter) { |
| 90 | Format::format(&(*self as *const T), fmt) |
| 91 | } |
| 92 | } |
| 93 | |