1#![allow(missing_debug_implementations)]
2#![unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
3
4use super::*;
10use crate::hint::unreachable_unchecked;
11use crate::ptr::NonNull;
12
13#[derive(Copy, Clone)]
14#[ferrocene::prevalidated]
15enum ArgumentType<'a> {
16 Placeholder {
17 value: NonNull<()>,
20 formatter: unsafe fn(NonNull<()>, &mut Formatter<'_>) -> Result,
21 _lifetime: PhantomData<&'a ()>,
22 },
23 Count(u16),
24}
25
26#[lang = "format_argument"]
37#[derive(Copy, Clone)]
38#[repr(align(2))] #[ferrocene::prevalidated]
40pub struct Argument<'a> {
41 ty: ArgumentType<'a>,
42}
43
44macro_rules! argument_new {
45 ($t:ty, $x:expr, $f:expr) => {
46 Argument {
47 ty: ArgumentType::Placeholder {
50 value: NonNull::<$t>::from_ref($x).cast(),
51 #[cfg(not(any(sanitize = "cfi", sanitize = "kcfi")))]
71 formatter: {
72 let f: fn(&$t, &mut Formatter<'_>) -> Result = $f;
73 unsafe { core::mem::transmute(f) }
75 },
76 #[cfg(any(sanitize = "cfi", sanitize = "kcfi"))]
77 formatter: |ptr: NonNull<()>, fmt: &mut Formatter<'_>| {
78 let func = $f;
79 let r = unsafe { ptr.cast::<$t>().as_ref() };
81 (func)(r, fmt)
82 },
83 _lifetime: PhantomData,
84 },
85 }
86 };
87}
88
89impl Argument<'_> {
90 #[inline]
91 #[ferrocene::prevalidated]
92 pub const fn new_display<T: Display>(x: &T) -> Argument<'_> {
93 argument_new!(T, x, <T as Display>::fmt)
94 }
95 #[inline]
96 #[ferrocene::prevalidated]
97 pub const fn new_debug<T: Debug>(x: &T) -> Argument<'_> {
98 argument_new!(T, x, <T as Debug>::fmt)
99 }
100 #[ferrocene::annotation(
101 "This function is not publicly available in stable Rust. It is only available in nightly with the \"fmt_internals\" feature enabled. \
102 Also it is not emitted by the compiler unless the unstable compiler option `-Z fmt-debug=none` is passed to `rustc`. Unstable compiler options are not qualified, therefore this function will never be used in qualified code. \
103 See https://github.com/ferrocene/ferrocene/blob/205b3a16164939e0da17deb74285418b3d4991ae/compiler/rustc_ast_lowering/src/format.rs#L247."
104 )]
105 #[inline]
106 #[ferrocene::prevalidated]
107 pub const fn new_debug_noop<T: Debug>(x: &T) -> Argument<'_> {
108 argument_new!(T, x, |_: &T, _| Ok(()))
109 }
110 #[inline]
111 #[ferrocene::prevalidated]
112 pub const fn new_octal<T: Octal>(x: &T) -> Argument<'_> {
113 argument_new!(T, x, <T as Octal>::fmt)
114 }
115 #[inline]
116 #[ferrocene::prevalidated]
117 pub const fn new_lower_hex<T: LowerHex>(x: &T) -> Argument<'_> {
118 argument_new!(T, x, <T as LowerHex>::fmt)
119 }
120 #[inline]
121 #[ferrocene::prevalidated]
122 pub const fn new_upper_hex<T: UpperHex>(x: &T) -> Argument<'_> {
123 argument_new!(T, x, <T as UpperHex>::fmt)
124 }
125 #[inline]
126 #[ferrocene::prevalidated]
127 pub const fn new_pointer<T: Pointer>(x: &T) -> Argument<'_> {
128 argument_new!(T, x, <T as Pointer>::fmt)
129 }
130 #[inline]
131 #[ferrocene::prevalidated]
132 pub const fn new_binary<T: Binary>(x: &T) -> Argument<'_> {
133 argument_new!(T, x, <T as Binary>::fmt)
134 }
135 #[inline]
136 #[ferrocene::prevalidated]
137 pub const fn new_lower_exp<T: LowerExp>(x: &T) -> Argument<'_> {
138 argument_new!(T, x, <T as LowerExp>::fmt)
139 }
140 #[inline]
141 #[ferrocene::prevalidated]
142 pub const fn new_upper_exp<T: UpperExp>(x: &T) -> Argument<'_> {
143 argument_new!(T, x, <T as UpperExp>::fmt)
144 }
145 #[inline]
146 #[track_caller]
147 #[ferrocene::prevalidated]
148 pub const fn from_usize(x: &usize) -> Argument<'_> {
149 if *x > u16::MAX as usize {
150 panic!("Formatting argument out of range");
151 }
152 Argument { ty: ArgumentType::Count(*x as u16) }
153 }
154
155 #[inline]
161 #[ferrocene::prevalidated]
162 pub(super) unsafe fn fmt(&self, f: &mut Formatter<'_>) -> Result {
163 match self.ty {
164 ArgumentType::Placeholder { formatter, value, .. } => unsafe { formatter(value, f) },
172 #[ferrocene::annotation(
173 "Cannot be covered as this code is unreachable. See the SAFETY comment."
174 )]
175 ArgumentType::Count(_) => unsafe { unreachable_unchecked() },
177 }
178 }
179
180 #[inline]
181 #[ferrocene::prevalidated]
182 pub(super) const fn as_u16(&self) -> Option<u16> {
183 match self.ty {
184 ArgumentType::Count(count) => Some(count),
185 ArgumentType::Placeholder { .. } => None,
186 }
187 }
188}