Skip to content

Commit ac974b2

Browse files
committed
Auto merge of rust-lang#90891 - nbdd0121:format, r=Mark-Simulacrum
Create `core::fmt::ArgumentV1` with generics instead of fn pointer Split from (and prerequisite of) rust-lang#90488, as this seems to have perf implication. `@rustbot` label: +T-libs
2 parents 1f1e753 + a940b31 commit ac974b2

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

core/src/fmt/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,21 @@ static USIZE_MARKER: fn(&usize, &mut Formatter<'_>) -> Result = |ptr, _| {
308308
loop {}
309309
};
310310

311+
macro_rules! arg_new {
312+
($f: ident, $t: ident) => {
313+
#[doc(hidden)]
314+
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
315+
#[inline]
316+
pub fn $f<'b, T: $t>(x: &'b T) -> ArgumentV1<'_> {
317+
Self::new(x, $t::fmt)
318+
}
319+
};
320+
}
321+
311322
impl<'a> ArgumentV1<'a> {
312323
#[doc(hidden)]
313324
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
325+
#[inline]
314326
pub fn new<'b, T>(x: &'b T, f: fn(&T, &mut Formatter<'_>) -> Result) -> ArgumentV1<'b> {
315327
// SAFETY: `mem::transmute(x)` is safe because
316328
// 1. `&'b T` keeps the lifetime it originated with `'b`
@@ -323,6 +335,16 @@ impl<'a> ArgumentV1<'a> {
323335
unsafe { ArgumentV1 { formatter: mem::transmute(f), value: mem::transmute(x) } }
324336
}
325337

338+
arg_new!(new_display, Display);
339+
arg_new!(new_debug, Debug);
340+
arg_new!(new_octal, Octal);
341+
arg_new!(new_lower_hex, LowerHex);
342+
arg_new!(new_upper_hex, UpperHex);
343+
arg_new!(new_pointer, Pointer);
344+
arg_new!(new_binary, Binary);
345+
arg_new!(new_lower_exp, LowerExp);
346+
arg_new!(new_upper_exp, UpperExp);
347+
326348
#[doc(hidden)]
327349
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
328350
pub fn from_usize(x: &usize) -> ArgumentV1<'_> {

0 commit comments

Comments
 (0)