Skip to content

Commit f790bee

Browse files
committed
Phrase flag in terms of whether core::fmt machinery is required
1 parent d43b759 commit f790bee

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

impl/src/attr.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ pub struct Attrs<'a> {
1818
#[derive(Clone)]
1919
pub struct Display<'a> {
2020
pub original: &'a Attribute,
21-
pub use_write_str: bool,
2221
pub fmt: LitStr,
2322
pub args: TokenStream,
23+
pub requires_fmt_machinery: bool,
2424
pub has_bonus_display: bool,
2525
pub implied_bounds: Set<(usize, Trait)>,
2626
}
@@ -106,12 +106,12 @@ fn parse_error_attribute<'a>(attrs: &mut Attrs<'a>, attr: &'a Attribute) -> Resu
106106

107107
let fmt: LitStr = input.parse()?;
108108
let args = parse_token_expr(input, false)?;
109+
let requires_fmt_machinery = !args.is_empty();
109110
let display = Display {
110111
original: attr,
111-
// This will be updated later if format_args are still required (i.e. has braces)
112-
use_write_str: args.is_empty(),
113112
fmt,
114113
args,
114+
requires_fmt_machinery,
115115
has_bonus_display: false,
116116
implied_bounds: Set::new(),
117117
};
@@ -205,13 +205,13 @@ impl ToTokens for Display<'_> {
205205
// Currently `write!(f, "text")` produces less efficient code than
206206
// `f.write_str("text")`. We recognize the case when the format string
207207
// has no braces and no interpolated values, and generate simpler code.
208-
tokens.extend(if self.use_write_str {
208+
tokens.extend(if self.requires_fmt_machinery {
209209
quote! {
210-
__formatter.write_str(#fmt)
210+
::core::write!(__formatter, #fmt #args)
211211
}
212212
} else {
213213
quote! {
214-
::core::write!(__formatter, #fmt #args)
214+
__formatter.write_str(#fmt)
215215
}
216216
});
217217
}

impl/src/fmt.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ impl Display<'_> {
3232
}
3333
}
3434

35-
if self.use_write_str && fmt.contains('}') {
36-
self.use_write_str = false;
37-
}
35+
self.requires_fmt_machinery = self.requires_fmt_machinery || fmt.contains('}');
3836

3937
while let Some(brace) = read.find('{') {
40-
self.use_write_str = false;
38+
self.requires_fmt_machinery = true;
4139
out += &read[..brace + 1];
4240
read = &read[brace + 1..];
4341
if read.starts_with('{') {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy