From 9cb57c6546843430b504b91fccf627671157bdb1 Mon Sep 17 00:00:00 2001 From: Yotam Ofek Date: Tue, 11 Feb 2025 15:24:32 +0000 Subject: [PATCH] macro -> fn --- src/librustdoc/html/format.rs | 44 +-- src/librustdoc/html/highlight.rs | 73 ++-- src/librustdoc/html/render/mod.rs | 254 ++++++------ src/librustdoc/html/render/print_item.rs | 466 +++++++++++++---------- 4 files changed, 451 insertions(+), 386 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 02b6a86b3a7ff..f08e82dd6a3b0 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -37,37 +37,15 @@ use crate::html::render::Context; use crate::joined::Joined as _; use crate::passes::collect_intra_doc_links::UrlFragment; -/// This macro is the same as [`std::write!`] for [`String`]s, but swallows the returned `Result`, -/// since writing into a `String` can never fail. -macro_rules! write_str { - ($dst:expr, $($arg:tt)*) => {{ - // make sure $dst is a `String` (or `&mut String`) - trait AssertString { - fn assert_string(&mut self) {} - } - impl AssertString for ::std::string::String {} - $dst.assert_string(); - - let _ = $dst.write_fmt(::std::format_args!($($arg)*)); - }}; +pub(crate) fn write_str(s: &mut String, f: fmt::Arguments<'_>) { + s.write_fmt(f).unwrap(); } -/// This macro is the same as [`std::writeln!`] for [`String`]s, but swallows the returned `Result`, -/// since writing into a `String` can never fail. -macro_rules! writeln_str { - ($dst:expr, $($arg:tt)*) => {{ - // make sure $dst is a `String` (or `&mut String`) - trait AssertString { - fn assert_string(&mut self) {} - } - impl AssertString for ::std::string::String {} - $dst.assert_string(); - let _ = $dst.write_fmt(::std::format_args_nl!($($arg)*)); - }}; +pub(crate) fn writeln_str(s: &mut String, f: fmt::Arguments<'_>) { + s.write_fmt(f).unwrap(); + s.push('\n'); } -pub(crate) use {write_str, writeln_str}; - pub(crate) fn print_generic_bounds<'a, 'tcx: 'a>( bounds: &'a [clean::GenericBound], cx: &'a Context<'tcx>, @@ -689,8 +667,6 @@ pub(crate) fn href_relative_parts<'fqp>( } pub(crate) fn link_tooltip(did: DefId, fragment: &Option, cx: &Context<'_>) -> String { - use write_str as write; - let cache = cx.cache(); let Some((fqp, shortty)) = cache.paths.get(&did).or_else(|| cache.external_paths.get(&did)) else { @@ -704,16 +680,16 @@ pub(crate) fn link_tooltip(did: DefId, fragment: &Option, cx: &Cont fqp }; if let &Some(UrlFragment::Item(id)) = fragment { - write!(buf, "{} ", cx.tcx().def_descr(id)); + write_str(&mut buf, format_args!("{} ", cx.tcx().def_descr(id))); for component in fqp { - write!(buf, "{component}::"); + write_str(&mut buf, format_args!("{component}::")); } - write!(buf, "{}", cx.tcx().item_name(id)); + write_str(&mut buf, format_args!("{}", cx.tcx().item_name(id))); } else if !fqp.is_empty() { let mut fqp_it = fqp.iter(); - write!(buf, "{shortty} {}", fqp_it.next().unwrap()); + write_str(&mut buf, format_args!("{shortty} {}", fqp_it.next().unwrap())); for component in fqp_it { - write!(buf, "::{component}"); + write_str(&mut buf, format_args!("::{component}")); } } buf diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index c1a2894d7021c..943680a0e2a08 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -14,7 +14,7 @@ use rustc_span::edition::Edition; use rustc_span::symbol::Symbol; use rustc_span::{BytePos, DUMMY_SP, Span}; -use super::format; +use super::format::{self, write_str, writeln_str}; use crate::clean::PrimitiveType; use crate::html::escape::EscapeBodyText; use crate::html::render::{Context, LinkFromSrc}; @@ -65,35 +65,37 @@ fn write_header( tooltip: Tooltip, extra_classes: &[String], ) { - use super::format::write_str as write; - - write!( + write_str( out, - "
", - match tooltip { - Tooltip::Ignore => " ignore", - Tooltip::CompileFail => " compile_fail", - Tooltip::ShouldPanic => " should_panic", - Tooltip::Edition(_) => " edition", - Tooltip::None => "", - }, + format_args!( + "
", + match tooltip { + Tooltip::Ignore => " ignore", + Tooltip::CompileFail => " compile_fail", + Tooltip::ShouldPanic => " should_panic", + Tooltip::Edition(_) => " edition", + Tooltip::None => "", + } + ), ); if tooltip != Tooltip::None { let edition_code; - write!( + write_str( out, - "", - match tooltip { - Tooltip::Ignore => "This example is not tested", - Tooltip::CompileFail => "This example deliberately fails to compile", - Tooltip::ShouldPanic => "This example panics", - Tooltip::Edition(edition) => { - edition_code = format!("This example runs with edition {edition}"); - &edition_code + format_args!( + "", + match tooltip { + Tooltip::Ignore => "This example is not tested", + Tooltip::CompileFail => "This example deliberately fails to compile", + Tooltip::ShouldPanic => "This example panics", + Tooltip::Edition(edition) => { + edition_code = format!("This example runs with edition {edition}"); + &edition_code + } + Tooltip::None => unreachable!(), } - Tooltip::None => unreachable!(), - }, + ), ); } @@ -101,21 +103,25 @@ fn write_header( out.push_str(&extra); } if class.is_empty() { - write!( + write_str( out, - "
",
-            if extra_classes.is_empty() { "" } else { " " },
-            extra_classes.join(" "),
+            format_args!(
+                "
",
+                if extra_classes.is_empty() { "" } else { " " },
+                extra_classes.join(" ")
+            ),
         );
     } else {
-        write!(
+        write_str(
             out,
-            "
",
-            if extra_classes.is_empty() { "" } else { " " },
-            extra_classes.join(" "),
+            format_args!(
+                "
",
+                if extra_classes.is_empty() { "" } else { " " },
+                extra_classes.join(" ")
+            ),
         );
     }
-    write!(out, "");
+    write_str(out, format_args!(""));
 }
 
 /// Check if two `Class` can be merged together. In the following rules, "unclassified" means `None`
@@ -325,8 +331,7 @@ pub(super) fn write_code(
 }
 
 fn write_footer(out: &mut String, playground_button: Option<&str>) {
-    use super::format::writeln_str as writeln;
-    writeln!(out, "
{}
", playground_button.unwrap_or_default()); + writeln_str(out, format_args!("{}
", playground_button.unwrap_or_default())); } /// How a span of text is classified. Mostly corresponds to token kinds. diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index d828c9fb50062..2f52a38c58173 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -71,7 +71,7 @@ use crate::html::escape::Escape; use crate::html::format::{ Ending, HrefError, PrintWithSpace, href, join_with_double_colon, print_abi_with_space, print_constness_with_space, print_default_space, print_generic_bounds, print_where_clause, - visibility_print_with_space, + visibility_print_with_space, write_str, }; use crate::html::markdown::{ HeadingOffset, IdMap, Markdown, MarkdownItemInfo, MarkdownSummaryLine, @@ -438,20 +438,20 @@ impl AllTypes { fn print(&self, f: &mut String) { fn print_entries(f: &mut String, e: &FxIndexSet, kind: ItemSection) { - use super::format::write_str as write; - if !e.is_empty() { let mut e: Vec<&ItemEntry> = e.iter().collect(); e.sort(); - write!( + write_str( f, - "

{title}

    ", - id = kind.id(), - title = kind.name(), + format_args!( + "

    {title}

      ", + id = kind.id(), + title = kind.name(), + ), ); for s in e.iter() { - write!(f, "
    • {}
    • ", s.print()); + write_str(f, format_args!("
    • {}
    • ", s.print())); } f.push_str("
    "); @@ -858,18 +858,18 @@ fn assoc_const( indent: usize, cx: &Context<'_>, ) { - use super::format::write_str as write; - let tcx = cx.tcx(); - write!( + write_str( w, - "{indent}{vis}const {name}{generics}: {ty}", - indent = " ".repeat(indent), - vis = visibility_print_with_space(it, cx), - href = assoc_href_attr(it, link, cx), - name = it.name.as_ref().unwrap(), - generics = generics.print(cx), - ty = ty.print(cx), + format_args!( + "{indent}{vis}const {name}{generics}: {ty}", + indent = " ".repeat(indent), + vis = visibility_print_with_space(it, cx), + href = assoc_href_attr(it, link, cx), + name = it.name.as_ref().unwrap(), + generics = generics.print(cx), + ty = ty.print(cx), + ), ); if let AssocConstValue::TraitDefault(konst) | AssocConstValue::Impl(konst) = value { // FIXME: `.value()` uses `clean::utils::format_integer_with_underscore_sep` under the @@ -883,10 +883,10 @@ fn assoc_const( AssocConstValue::Impl(_) => repr != "_", // show if there is a meaningful value to show AssocConstValue::None => unreachable!(), } { - write!(w, " = {}", Escape(&repr)); + write_str(w, format_args!(" = {}", Escape(&repr))); } } - write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline)); + write_str(w, format_args!("{}", print_where_clause(generics, cx, indent, Ending::NoNewline))); } fn assoc_type( @@ -899,25 +899,25 @@ fn assoc_type( indent: usize, cx: &Context<'_>, ) { - use super::format::write_str as write; - - write!( + write_str( w, - "{indent}{vis}type {name}{generics}", - indent = " ".repeat(indent), - vis = visibility_print_with_space(it, cx), - href = assoc_href_attr(it, link, cx), - name = it.name.as_ref().unwrap(), - generics = generics.print(cx), + format_args!( + "{indent}{vis}type {name}{generics}", + indent = " ".repeat(indent), + vis = visibility_print_with_space(it, cx), + href = assoc_href_attr(it, link, cx), + name = it.name.as_ref().unwrap(), + generics = generics.print(cx), + ), ); if !bounds.is_empty() { - write!(w, ": {}", print_generic_bounds(bounds, cx)); + write_str(w, format_args!(": {}", print_generic_bounds(bounds, cx))); } // Render the default before the where-clause which aligns with the new recommended style. See #89122. if let Some(default) = default { - write!(w, " = {}", default.print(cx)); + write_str(w, format_args!(" = {}", default.print(cx))); } - write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline)); + write_str(w, format_args!("{}", print_where_clause(generics, cx, indent, Ending::NoNewline))); } fn assoc_method( @@ -930,8 +930,6 @@ fn assoc_method( cx: &Context<'_>, render_mode: RenderMode, ) { - use super::format::write_str as write; - let tcx = cx.tcx(); let header = meth.fn_header(tcx).expect("Trying to get header from a non-function item"); let name = meth.name.as_ref().unwrap(); @@ -969,30 +967,32 @@ fn assoc_method( let (indent, indent_str, end_newline) = if parent == ItemType::Trait { header_len += 4; let indent_str = " "; - write!(w, "{}", render_attributes_in_pre(meth, indent_str, cx)); + write_str(w, format_args!("{}", render_attributes_in_pre(meth, indent_str, cx))); (4, indent_str, Ending::NoNewline) } else { render_attributes_in_code(w, meth, cx); (0, "", Ending::Newline) }; w.reserve(header_len + "{".len() + "".len()); - write!( + write_str( w, - "{indent}{vis}{defaultness}{constness}{asyncness}{safety}{abi}fn \ + format_args!( + "{indent}{vis}{defaultness}{constness}{asyncness}{safety}{abi}fn \ {name}{generics}{decl}{notable_traits}{where_clause}", - indent = indent_str, - vis = vis, - defaultness = defaultness, - constness = constness, - asyncness = asyncness, - safety = safety, - abi = abi, - href = href, - name = name, - generics = g.print(cx), - decl = d.full_print(header_len, indent, cx), - notable_traits = notable_traits.unwrap_or_default(), - where_clause = print_where_clause(g, cx, indent, end_newline), + indent = indent_str, + vis = vis, + defaultness = defaultness, + constness = constness, + asyncness = asyncness, + safety = safety, + abi = abi, + href = href, + name = name, + generics = g.print(cx), + decl = d.full_print(header_len, indent, cx), + notable_traits = notable_traits.unwrap_or_default(), + where_clause = print_where_clause(g, cx, indent, end_newline), + ), ); } @@ -1016,8 +1016,6 @@ fn render_stability_since_raw_with_extra( const_stability: Option, extra_class: &str, ) -> bool { - use super::format::write_str as write; - let mut title = String::new(); let mut stability = String::new(); @@ -1068,7 +1066,10 @@ fn render_stability_since_raw_with_extra( } if !stability.is_empty() { - write!(w, r#"{stability}"#); + write_str( + w, + format_args!(r#"{stability}"#), + ); } !stability.is_empty() @@ -1491,8 +1492,6 @@ pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &Context<'_>) -> Optio } fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> (String, String) { - use super::format::write_str as write; - let mut out = String::new(); let did = ty.def_id(cx.cache()).expect("notable_traits_button already checked this"); @@ -1515,15 +1514,20 @@ fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> (String, String) { if cx.cache().traits.get(&trait_did).is_some_and(|t| t.is_notable_trait(cx.tcx())) { if out.is_empty() { - write!( + write_str( &mut out, - "

    Notable traits for {}

    \ -
    ",
    -                        impl_.for_.print(cx)
    +                        format_args!(
    +                            "

    Notable traits for {}

    \ +
    ",
    +                            impl_.for_.print(cx)
    +                        ),
                         );
                     }
     
    -                write!(&mut out, "
    {}
    ", impl_.print(false, cx)); + write_str( + &mut out, + format_args!("
    {}
    ", impl_.print(false, cx)), + ); for it in &impl_.items { if let clean::AssocTypeItem(ref tydef, ref _bounds) = it.kind { out.push_str("
    "); @@ -1595,8 +1599,6 @@ fn render_impl( aliases: &[String], rendering_params: ImplRenderingParameters, ) { - use super::format::write_str as write; - let cache = &cx.shared.cache; let traits = &cache.traits; let trait_ = i.trait_did().map(|did| &traits[&did]); @@ -1648,25 +1650,26 @@ fn render_impl( document_item_info(cx, it, Some(parent)) .render_into(&mut info_buffer) .unwrap(); - write!( + write_str( &mut doc_buffer, - "{}", - document_full(item, cx, HeadingOffset::H5) + format_args!("{}", document_full(item, cx, HeadingOffset::H5)), ); short_documented = false; } else { // In case the item isn't documented, // provide short documentation from the trait. - write!( + write_str( &mut doc_buffer, - "{}", - document_short( - it, - cx, - link, - parent, - rendering_params.show_def_docs, - ) + format_args!( + "{}", + document_short( + it, + cx, + link, + parent, + rendering_params.show_def_docs, + ) + ), ); } } @@ -1675,15 +1678,20 @@ fn render_impl( .render_into(&mut info_buffer) .unwrap(); if rendering_params.show_def_docs { - write!(&mut doc_buffer, "{}", document_full(item, cx, HeadingOffset::H5)); + write_str( + &mut doc_buffer, + format_args!("{}", document_full(item, cx, HeadingOffset::H5)), + ); short_documented = false; } } } else { - write!( + write_str( &mut doc_buffer, - "{}", - document_short(item, cx, link, parent, rendering_params.show_def_docs) + format_args!( + "{}", + document_short(item, cx, link, parent, rendering_params.show_def_docs) + ), ); } } @@ -1692,7 +1700,10 @@ fn render_impl( let toggled = !doc_buffer.is_empty(); if toggled { let method_toggle_class = if item_type.is_method() { " method-toggle" } else { "" }; - write!(w, "
    "); + write_str( + w, + format_args!("
    "), + ); } match &item.kind { clean::MethodItem(..) | clean::RequiredMethodItem(_) => { @@ -1707,11 +1718,14 @@ fn render_impl( .find(|item| item.name.map(|n| n == *name).unwrap_or(false)) }) .map(|item| format!("{}.{name}", item.type_())); - write!(w, "
    "); + write_str( + w, + format_args!("
    "), + ); render_rightside(w, cx, item, render_mode); if trait_.is_some() { // Anchors are only used on trait impls. - write!(w, "§"); + write_str(w, format_args!("§")); } w.push_str("

    "); render_assoc_item( @@ -1728,11 +1742,14 @@ fn render_impl( clean::RequiredAssocConstItem(ref generics, ref ty) => { let source_id = format!("{item_type}.{name}"); let id = cx.derive_id(&source_id); - write!(w, "
    "); + write_str( + w, + format_args!("
    "), + ); render_rightside(w, cx, item, render_mode); if trait_.is_some() { // Anchors are only used on trait impls. - write!(w, "§"); + write_str(w, format_args!("§")); } w.push_str("

    "); assoc_const( @@ -1750,11 +1767,14 @@ fn render_impl( clean::ProvidedAssocConstItem(ci) | clean::ImplAssocConstItem(ci) => { let source_id = format!("{item_type}.{name}"); let id = cx.derive_id(&source_id); - write!(w, "
    "); + write_str( + w, + format_args!("
    "), + ); render_rightside(w, cx, item, render_mode); if trait_.is_some() { // Anchors are only used on trait impls. - write!(w, "§"); + write_str(w, format_args!("§")); } w.push_str("

    "); assoc_const( @@ -1776,11 +1796,14 @@ fn render_impl( clean::RequiredAssocTypeItem(ref generics, ref bounds) => { let source_id = format!("{item_type}.{name}"); let id = cx.derive_id(&source_id); - write!(w, "
    "); + write_str( + w, + format_args!("
    "), + ); render_rightside(w, cx, item, render_mode); if trait_.is_some() { // Anchors are only used on trait impls. - write!(w, "§"); + write_str(w, format_args!("§")); } w.push_str("

    "); assoc_type( @@ -1798,11 +1821,14 @@ fn render_impl( clean::AssocTypeItem(tydef, _bounds) => { let source_id = format!("{item_type}.{name}"); let id = cx.derive_id(&source_id); - write!(w, "
    "); + write_str( + w, + format_args!("
    "), + ); render_rightside(w, cx, item, render_mode); if trait_.is_some() { // Anchors are only used on trait impls. - write!(w, "§"); + write_str(w, format_args!("§")); } w.push_str("

    "); assoc_type( @@ -1970,11 +1996,13 @@ fn render_impl( let toggled = !(impl_items.is_empty() && default_impl_items.is_empty()); if toggled { close_tags.push("

    "); - write!( + write_str( w, - "
    \ - ", - if rendering_params.toggle_open_by_default { " open" } else { "" } + format_args!( + "
    \ + ", + if rendering_params.toggle_open_by_default { " open" } else { "" } + ), ); } @@ -2017,7 +2045,7 @@ fn render_impl( ); } if let Some(after_dox) = after_dox { - write!(w, "
    {after_dox}
    "); + write_str(w, format_args!("
    {after_dox}
    ")); } } if !default_impl_items.is_empty() || !impl_items.is_empty() { @@ -2037,8 +2065,6 @@ fn render_impl( // Render the items that appear on the right side of methods, impls, and // associated types. For example "1.0.0 (const: 1.39.0) · source". fn render_rightside(w: &mut String, cx: &Context<'_>, item: &clean::Item, render_mode: RenderMode) { - use super::format::write_str as write; - let tcx = cx.tcx(); // FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove @@ -2059,13 +2085,19 @@ fn render_rightside(w: &mut String, cx: &Context<'_>, item: &clean::Item, render ); if let Some(link) = src_href { if has_stability { - write!(rightside, " · Source"); + write_str( + &mut rightside, + format_args!(" · Source"), + ); } else { - write!(rightside, "Source"); + write_str( + &mut rightside, + format_args!("Source"), + ); } } if has_stability && has_src_ref { - write!(w, "{rightside}"); + write_str(w, format_args!("{rightside}")); } else { w.push_str(&rightside); } @@ -2083,8 +2115,6 @@ pub(crate) fn render_impl_summary( aliases: &[String], doc: &Option, ) { - use super::format::write_str as write; - let inner_impl = i.inner_impl(); let id = cx.derive_id(get_id_for_impl(cx.tcx(), i.impl_item.item_id)); let aliases = if aliases.is_empty() { @@ -2092,16 +2122,18 @@ pub(crate) fn render_impl_summary( } else { format!(" data-aliases=\"{}\"", aliases.join(",")) }; - write!(w, "
    "); + write_str(w, format_args!("
    ")); render_rightside(w, cx, &i.impl_item, RenderMode::Normal); - write!( + write_str( w, - "§\ -

    " + format_args!( + "§\ +

    " + ), ); if let Some(use_absolute) = use_absolute { - write!(w, "{}", inner_impl.print(use_absolute, cx)); + write_str(w, format_args!("{}", inner_impl.print(use_absolute, cx))); if show_def_docs { for it in &inner_impl.items { if let clean::AssocTypeItem(ref tydef, ref _bounds) = it.kind { @@ -2121,22 +2153,24 @@ pub(crate) fn render_impl_summary( } } } else { - write!(w, "{}", inner_impl.print(false, cx)); + write_str(w, format_args!("{}", inner_impl.print(false, cx))); } w.push_str("

    "); let is_trait = inner_impl.trait_.is_some(); if is_trait && let Some(portability) = portability(&i.impl_item, Some(parent)) { - write!( + write_str( w, - "\ + format_args!( + "\
    {portability}
    \
    ", + ), ); } if let Some(doc) = doc { - write!(w, "
    {doc}
    "); + write_str(w, format_args!("
    {doc}
    ")); } w.push_str("
    "); diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index b5d2e37739d81..2fea5ee4643b1 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -32,7 +32,8 @@ use crate::formats::item_type::ItemType; use crate::html::escape::{Escape, EscapeBodyTextWithWbr}; use crate::html::format::{ Ending, PrintWithSpace, join_with_double_colon, print_abi_with_space, - print_constness_with_space, print_where_clause, visibility_print_with_space, + print_constness_with_space, print_where_clause, visibility_print_with_space, write_str, + writeln_str, }; use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine}; use crate::html::render::{document_full, document_item_info}; @@ -169,16 +170,12 @@ fn print_where_clause_and_check<'a, 'tcx: 'a>( gens: &'a clean::Generics, cx: &'a Context<'tcx>, ) -> bool { - use crate::html::format::write_str as write; - let len_before = buffer.len(); - write!(buffer, "{}", print_where_clause(gens, cx, 0, Ending::Newline)); + write_str(buffer, format_args!("{}", print_where_clause(gens, cx, 0, Ending::Newline))); len_before != buffer.len() } pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut String) { - use crate::html::format::write_str as write; - debug_assert!(!item.is_stripped()); let typ = match item.kind { clean::ModuleItem(_) => { @@ -284,10 +281,12 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut String) // Render notable-traits.js used for all methods in this module. let mut types_with_notable_traits = cx.types_with_notable_traits.borrow_mut(); if !types_with_notable_traits.is_empty() { - write!( + write_str( buf, - r#""#, - notable_traits_json(types_with_notable_traits.iter(), cx) + format_args!( + r#""#, + notable_traits_json(types_with_notable_traits.iter(), cx) + ), ); types_with_notable_traits.clear(); } @@ -318,9 +317,7 @@ trait ItemTemplate<'a, 'cx: 'a>: rinja::Template + Display { } fn item_module(w: &mut String, cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) { - use crate::html::format::write_str as write; - - write!(w, "{}", document(cx, item, None, HeadingOffset::H2)); + write_str(w, format_args!("{}", document(cx, item, None, HeadingOffset::H2))); let mut not_stripped_items = items.iter().filter(|i| !i.is_stripped()).enumerate().collect::>(); @@ -421,20 +418,24 @@ fn item_module(w: &mut String, cx: &Context<'_>, item: &clean::Item, items: &[cl match *src { Some(src) => { - write!( + write_str( w, - "
    {}extern crate {} as {};", - visibility_print_with_space(myitem, cx), - anchor(myitem.item_id.expect_def_id(), src, cx), - EscapeBodyTextWithWbr(myitem.name.unwrap().as_str()), + format_args!( + "
    {}extern crate {} as {};", + visibility_print_with_space(myitem, cx), + anchor(myitem.item_id.expect_def_id(), src, cx), + EscapeBodyTextWithWbr(myitem.name.unwrap().as_str()) + ), ); } None => { - write!( + write_str( w, - "
    {}extern crate {};", - visibility_print_with_space(myitem, cx), - anchor(myitem.item_id.expect_def_id(), myitem.name.unwrap(), cx), + format_args!( + "
    {}extern crate {};", + visibility_print_with_space(myitem, cx), + anchor(myitem.item_id.expect_def_id(), myitem.name.unwrap(), cx) + ), ); } } @@ -452,13 +453,15 @@ fn item_module(w: &mut String, cx: &Context<'_>, item: &clean::Item, items: &[cl } clean::ImportKind::Glob => String::new(), }; - write!( + write_str( w, - "\ - {vis}{imp}{stab_tags}\ -
    ", - vis = visibility_print_with_space(myitem, cx), - imp = import.print(cx), + format_args!( + "\ + {vis}{imp}{stab_tags}\ + ", + vis = visibility_print_with_space(myitem, cx), + imp = import.print(cx) + ), ); } @@ -496,26 +499,28 @@ fn item_module(w: &mut String, cx: &Context<'_>, item: &clean::Item, items: &[cl MarkdownSummaryLine(&myitem.doc_value(), &myitem.links(cx)).into_string(); let (docs_before, docs_after) = if docs.is_empty() { ("", "") } else { ("
    ", "
    ") }; - write!( + write_str( w, - "
    \ - {name}\ - {visibility_and_hidden}\ - {unsafety_flag}\ - {stab_tags}\ -
    \ - {docs_before}{docs}{docs_after}", - name = EscapeBodyTextWithWbr(myitem.name.unwrap().as_str()), - visibility_and_hidden = visibility_and_hidden, - stab_tags = extra_info_tags(tcx, myitem, item, None), - class = myitem.type_(), - unsafety_flag = unsafety_flag, - href = item_path(myitem.type_(), myitem.name.unwrap().as_str()), - title = [myitem.type_().to_string(), full_path(cx, myitem)] - .iter() - .filter_map(|s| if !s.is_empty() { Some(s.as_str()) } else { None }) - .collect::>() - .join(" "), + format_args!( + "
    \ + {name}\ + {visibility_and_hidden}\ + {unsafety_flag}\ + {stab_tags}\ +
    \ + {docs_before}{docs}{docs_after}", + name = EscapeBodyTextWithWbr(myitem.name.unwrap().as_str()), + visibility_and_hidden = visibility_and_hidden, + stab_tags = extra_info_tags(tcx, myitem, item, None), + class = myitem.type_(), + unsafety_flag = unsafety_flag, + href = item_path(myitem.type_(), myitem.name.unwrap().as_str()), + title = [myitem.type_().to_string(), full_path(cx, myitem)] + .iter() + .filter_map(|s| if !s.is_empty() { Some(s.as_str()) } else { None }) + .collect::>() + .join(" "), + ), ); } } @@ -585,8 +590,6 @@ fn extra_info_tags<'a, 'tcx: 'a>( } fn item_function(w: &mut String, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) { - use crate::html::format::write_str as write; - let tcx = cx.tcx(); let header = it.fn_header(tcx).expect("printing a function which isn't a function"); debug!( @@ -621,29 +624,29 @@ fn item_function(w: &mut String, cx: &Context<'_>, it: &clean::Item, f: &clean:: wrap_item(w, |w| { w.reserve(header_len); - write!( + write_str( w, - "{attrs}{vis}{constness}{asyncness}{safety}{abi}fn \ + format_args!( + "{attrs}{vis}{constness}{asyncness}{safety}{abi}fn \ {name}{generics}{decl}{notable_traits}{where_clause}", - attrs = render_attributes_in_pre(it, "", cx), - vis = visibility, - constness = constness, - asyncness = asyncness, - safety = safety, - abi = abi, - name = name, - generics = f.generics.print(cx), - where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline), - decl = f.decl.full_print(header_len, 0, cx), - notable_traits = notable_traits.unwrap_or_default(), + attrs = render_attributes_in_pre(it, "", cx), + vis = visibility, + constness = constness, + asyncness = asyncness, + safety = safety, + abi = abi, + name = name, + generics = f.generics.print(cx), + where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline), + decl = f.decl.full_print(header_len, 0, cx), + notable_traits = notable_traits.unwrap_or_default(), + ), ); }); - write!(w, "{}", document(cx, it, None, HeadingOffset::H2)); + write_str(w, format_args!("{}", document(cx, it, None, HeadingOffset::H2))); } fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Trait) { - use crate::html::format::{write_str as write, writeln_str as writeln}; - let tcx = cx.tcx(); let bounds = bounds(&t.bounds, false, cx); let required_types = @@ -661,19 +664,24 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra // Output the trait definition wrap_item(w, |mut w| { - write!( + write_str( w, - "{attrs}{vis}{safety}{is_auto}trait {name}{generics}{bounds}", - attrs = render_attributes_in_pre(it, "", cx), - vis = visibility_print_with_space(it, cx), - safety = t.safety(tcx).print_with_space(), - is_auto = if t.is_auto(tcx) { "auto " } else { "" }, - name = it.name.unwrap(), - generics = t.generics.print(cx), + format_args!( + "{attrs}{vis}{safety}{is_auto}trait {name}{generics}{bounds}", + attrs = render_attributes_in_pre(it, "", cx), + vis = visibility_print_with_space(it, cx), + safety = t.safety(tcx).print_with_space(), + is_auto = if t.is_auto(tcx) { "auto " } else { "" }, + name = it.name.unwrap(), + generics = t.generics.print(cx), + ), ); if !t.generics.where_predicates.is_empty() { - write!(w, "{}", print_where_clause(&t.generics, cx, 0, Ending::Newline)); + write_str( + w, + format_args!("{}", print_where_clause(&t.generics, cx, 0, Ending::Newline)), + ); } else { w.push_str(" "); } @@ -747,7 +755,10 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra } if !required_methods.is_empty() { - writeln!(w, " // Required method{}", pluralize(required_methods.len())); + writeln_str( + w, + format_args!(" // Required method{}", pluralize(required_methods.len())), + ); } for (pos, m) in required_methods.iter().enumerate() { render_assoc_item( @@ -769,7 +780,10 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra } if !provided_methods.is_empty() { - writeln!(w, " // Provided method{}", pluralize(provided_methods.len())); + writeln_str( + w, + format_args!(" // Provided method{}", pluralize(provided_methods.len())), + ); } for (pos, m) in provided_methods.iter().enumerate() { render_assoc_item( @@ -795,7 +809,7 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra }); // Trait documentation - write!(w, "{}", document(cx, it, None, HeadingOffset::H2)); + write_str(w, format_args!("{}", document(cx, it, None, HeadingOffset::H2))); fn trait_item(w: &mut String, cx: &Context<'_>, m: &clean::Item, t: &clean::Item) { let name = m.name.unwrap(); @@ -804,16 +818,19 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra let id = cx.derive_id(format!("{item_type}.{name}")); let mut content = String::new(); - write!(content, "{}", document_full(m, cx, HeadingOffset::H5)); + write_str(&mut content, format_args!("{}", document_full(m, cx, HeadingOffset::H5))); let toggled = !content.is_empty(); if toggled { let method_toggle_class = if item_type.is_method() { " method-toggle" } else { "" }; - write!(w, "
    "); + write_str( + w, + format_args!("
    "), + ); } - write!(w, "
    "); + write_str(w, format_args!("
    ")); render_rightside(w, cx, m, RenderMode::Normal); - write!(w, "

    "); + write_str(w, format_args!("

    ")); render_assoc_item( w, m, @@ -825,9 +842,9 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra w.push_str("

    "); document_item_info(cx, m, Some(t)).render_into(w).unwrap(); if toggled { - write!(w, "
    "); + write_str(w, format_args!("
    ")); w.push_str(&content); - write!(w, "
    "); + write_str(w, format_args!("
    ")); } } @@ -896,10 +913,12 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra ); if let Some(list) = must_implement_one_of_functions.as_deref() { - write!( + write_str( w, - "
    At least one of the `{}` methods is required.
    ", - list.iter().join("`, `") + format_args!( + "
    At least one of the `{}` methods is required.
    ", + list.iter().join("`, `") + ), ); } @@ -923,7 +942,13 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra } // If there are methods directly on this trait object, render them here. - write!(w, "{}", render_assoc_items(cx, it, it.item_id.expect_def_id(), AssocItemRender::All)); + write_str( + w, + format_args!( + "{}", + render_assoc_items(cx, it, it.item_id.expect_def_id(), AssocItemRender::All) + ), + ); let mut extern_crates = FxIndexSet::default(); @@ -1152,10 +1177,12 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra .join(","); let (extern_before, extern_after) = if extern_crates.is_empty() { ("", "") } else { (" data-ignore-extern-crates=\"", "\"") }; - write!( + write_str( w, - "", - src = js_src_path.finish(), + format_args!( + "", + src = js_src_path.finish() + ), ); } @@ -1188,22 +1215,22 @@ fn item_trait_alias( } fn item_type_alias(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::TypeAlias) { - use crate::html::format::write_str as write; - wrap_item(w, |w| { - write!( + write_str( w, - "{attrs}{vis}type {name}{generics}{where_clause} = {type_};", - attrs = render_attributes_in_pre(it, "", cx), - vis = visibility_print_with_space(it, cx), - name = it.name.unwrap(), - generics = t.generics.print(cx), - where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline), - type_ = t.type_.print(cx), + format_args!( + "{attrs}{vis}type {name}{generics}{where_clause} = {type_};", + attrs = render_attributes_in_pre(it, "", cx), + vis = visibility_print_with_space(it, cx), + name = it.name.unwrap(), + generics = t.generics.print(cx), + where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline), + type_ = t.type_.print(cx), + ), ); }); - write!(w, "{}", document(cx, it, None, HeadingOffset::H2)); + write_str(w, format_args!("{}", document(cx, it, None, HeadingOffset::H2))); if let Some(inner_type) = &t.inner_type { write_section_heading(w, "Aliased Type", "aliased-type", None, ""); @@ -1219,7 +1246,7 @@ fn item_type_alias(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean let variants_count = variants_iter().count(); let has_stripped_entries = variants_len != variants_count; - write!(w, "enum {}{}", it.name.unwrap(), t.generics.print(cx)); + write_str(w, format_args!("enum {}{}", it.name.unwrap(), t.generics.print(cx))); render_enum_fields( w, cx, @@ -1238,7 +1265,10 @@ fn item_type_alias(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean let fields_count = fields.iter().filter(|i| !i.is_stripped()).count(); let has_stripped_fields = fields.len() != fields_count; - write!(w, "union {}{}", it.name.unwrap(), t.generics.print(cx)); + write_str( + w, + format_args!("union {}{}", it.name.unwrap(), t.generics.print(cx)), + ); render_struct_fields( w, Some(&t.generics), @@ -1257,7 +1287,10 @@ fn item_type_alias(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean let fields_count = fields.iter().filter(|i| !i.is_stripped()).count(); let has_stripped_fields = fields.len() != fields_count; - write!(w, "struct {}{}", it.name.unwrap(), t.generics.print(cx)); + write_str( + w, + format_args!("struct {}{}", it.name.unwrap(), t.generics.print(cx)), + ); render_struct_fields( w, Some(&t.generics), @@ -1279,8 +1312,8 @@ fn item_type_alias(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean // won't be visible anywhere in the docs. It would be nice to also show // associated items from the aliased type (see discussion in #32077), but // we need #14072 to make sense of the generics. - write!(w, "{}", render_assoc_items(cx, it, def_id, AssocItemRender::All)); - write!(w, "{}", document_type_layout(cx, def_id)); + write_str(w, format_args!("{}", render_assoc_items(cx, it, def_id, AssocItemRender::All))); + write_str(w, format_args!("{}", document_type_layout(cx, def_id))); // [RUSTDOCIMPL] type.impl // @@ -1370,10 +1403,12 @@ fn item_type_alias(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean js_src_path.extend(target_fqp[..target_fqp.len() - 1].iter().copied()); js_src_path.push_fmt(format_args!("{target_type}.{}.js", target_fqp.last().unwrap())); let self_path = self_fqp.iter().map(Symbol::as_str).collect::>().join("::"); - write!( + write_str( w, - "", - src = js_src_path.finish(), + format_args!( + "", + src = js_src_path.finish() + ), ); } } @@ -1464,17 +1499,17 @@ fn print_tuple_struct_fields<'a, 'cx: 'a>( } fn item_enum(w: &mut String, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum) { - use crate::html::format::write_str as write; - let count_variants = e.variants().count(); wrap_item(w, |w| { render_attributes_in_code(w, it, cx); - write!( + write_str( w, - "{}enum {}{}", - visibility_print_with_space(it, cx), - it.name.unwrap(), - e.generics.print(cx), + format_args!( + "{}enum {}{}", + visibility_print_with_space(it, cx), + it.name.unwrap(), + e.generics.print(cx), + ), ); render_enum_fields( @@ -1489,14 +1524,14 @@ fn item_enum(w: &mut String, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum ); }); - write!(w, "{}", document(cx, it, None, HeadingOffset::H2)); + write_str(w, format_args!("{}", document(cx, it, None, HeadingOffset::H2))); if count_variants != 0 { item_variants(w, cx, it, &e.variants, it.def_id().unwrap()); } let def_id = it.item_id.expect_def_id(); - write!(w, "{}", render_assoc_items(cx, it, def_id, AssocItemRender::All)); - write!(w, "{}", document_type_layout(cx, def_id)); + write_str(w, format_args!("{}", render_assoc_items(cx, it, def_id, AssocItemRender::All))); + write_str(w, format_args!("{}", document_type_layout(cx, def_id))); } /// It'll return false if any variant is not a C-like variant. Otherwise it'll return true if at @@ -1533,18 +1568,16 @@ fn display_c_like_variant( should_show_enum_discriminant: bool, enum_def_id: DefId, ) { - use crate::html::format::write_str as write; - let name = item.name.unwrap(); if let Some(ref value) = variant.discriminant { - write!(w, "{} = {}", name.as_str(), value.value(cx.tcx(), true)); + write_str(w, format_args!("{} = {}", name.as_str(), value.value(cx.tcx(), true))); } else if should_show_enum_discriminant { let adt_def = cx.tcx().adt_def(enum_def_id); let discr = adt_def.discriminant_for_variant(cx.tcx(), index); if discr.ty.is_signed() { - write!(w, "{} = {}", name.as_str(), discr.val as i128); + write_str(w, format_args!("{} = {}", name.as_str(), discr.val as i128)); } else { - write!(w, "{} = {}", name.as_str(), discr.val); + write_str(w, format_args!("{} = {}", name.as_str(), discr.val)); } } else { w.push_str(name.as_str()); @@ -1561,8 +1594,6 @@ fn render_enum_fields( is_non_exhaustive: bool, enum_def_id: DefId, ) { - use crate::html::format::write_str as write; - let should_show_enum_discriminant = should_show_enum_discriminant(cx, enum_def_id, variants); if !g.is_some_and(|g| print_where_clause_and_check(w, g, cx)) { // If there wasn't a `where` clause, we add a whitespace. @@ -1596,7 +1627,14 @@ fn render_enum_fields( enum_def_id, ), clean::VariantKind::Tuple(ref s) => { - write!(w, "{}({})", v.name.unwrap(), print_tuple_struct_fields(cx, s)); + write_str( + w, + format_args!( + "{}({})", + v.name.unwrap(), + print_tuple_struct_fields(cx, s) + ), + ); } clean::VariantKind::Struct(ref s) => { render_struct(w, v, None, None, &s.fields, TAB, false, cx); @@ -1624,8 +1662,6 @@ fn item_variants( variants: &IndexVec, enum_def_id: DefId, ) { - use crate::html::format::write_str as write; - let tcx = cx.tcx(); write_section_heading( w, @@ -1641,10 +1677,12 @@ fn item_variants( continue; } let id = cx.derive_id(format!("{}.{}", ItemType::Variant, variant.name.unwrap())); - write!( + write_str( w, - "
    \ - §", + format_args!( + "
    \ + §" + ), ); render_stability_since_raw_with_extra( w, @@ -1672,11 +1710,11 @@ fn item_variants( let clean::VariantItem(variant_data) = &variant.kind else { unreachable!() }; if let clean::VariantKind::Tuple(ref s) = variant_data.kind { - write!(w, "({})", print_tuple_struct_fields(cx, s)); + write_str(w, format_args!("({})", print_tuple_struct_fields(cx, s))); } w.push_str("
    "); - write!(w, "{}", document(cx, variant, Some(it), HeadingOffset::H4)); + write_str(w, format_args!("{}", document(cx, variant, Some(it), HeadingOffset::H4))); let heading_and_fields = match &variant_data.kind { clean::VariantKind::Struct(s) => { @@ -1702,12 +1740,14 @@ fn item_variants( if let Some((heading, fields)) = heading_and_fields { let variant_id = cx.derive_id(format!("{}.{}.fields", ItemType::Variant, variant.name.unwrap())); - write!( + write_str( w, - "
    \ -

    {heading}

    \ - {}", - document_non_exhaustive(variant) + format_args!( + "
    \ +

    {heading}

    \ + {}", + document_non_exhaustive(variant) + ), ); for field in fields { match field.kind { @@ -1718,20 +1758,24 @@ fn item_variants( variant.name.unwrap(), field.name.unwrap() )); - write!( + write_str( w, - "
    \ - \ - §\ - {f}: {t}\ - ", - f = field.name.unwrap(), - t = ty.print(cx), + format_args!( + "
    \ + \ + §\ + {f}: {t}\ + ", + f = field.name.unwrap(), + t = ty.print(cx), + ), ); - write!( + write_str( w, - "{}
    ", - document(cx, field, Some(variant), HeadingOffset::H5) + format_args!( + "{}
    ", + document(cx, field, Some(variant), HeadingOffset::H5), + ), ); } _ => unreachable!(), @@ -1740,20 +1784,18 @@ fn item_variants( w.push_str("
    "); } } - write!(w, "
    "); + write_str(w, format_args!("
    ")); } fn item_macro(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Macro) { - use crate::html::format::write_str as write; - wrap_item(w, |w| { // FIXME: Also print `#[doc(hidden)]` for `macro_rules!` if it `is_doc_hidden`. if !t.macro_rules { - write!(w, "{}", visibility_print_with_space(it, cx)); + write_str(w, format_args!("{}", visibility_print_with_space(it, cx))); } - write!(w, "{}", Escape(&t.source)); + write_str(w, format_args!("{}", Escape(&t.source))); }); - write!(w, "{}", document(cx, it, None, HeadingOffset::H2)); + write_str(w, format_args!("{}", document(cx, it, None, HeadingOffset::H2))); } fn item_proc_macro( @@ -1814,20 +1856,20 @@ fn item_constant( ty: &clean::Type, c: &clean::ConstantKind, ) { - use crate::html::format::write_str as write; - wrap_item(w, |w| { let tcx = cx.tcx(); render_attributes_in_code(w, it, cx); - write!( + write_str( w, - "{vis}const {name}{generics}: {typ}{where_clause}", - vis = visibility_print_with_space(it, cx), - name = it.name.unwrap(), - generics = generics.print(cx), - typ = ty.print(cx), - where_clause = print_where_clause(generics, cx, 0, Ending::NoNewline), + format_args!( + "{vis}const {name}{generics}: {typ}{where_clause}", + vis = visibility_print_with_space(it, cx), + name = it.name.unwrap(), + generics = generics.print(cx), + typ = ty.print(cx), + where_clause = print_where_clause(generics, cx, 0, Ending::NoNewline) + ), ); // FIXME: The code below now prints @@ -1843,7 +1885,7 @@ fn item_constant( let is_literal = c.is_literal(tcx); let expr = c.expr(tcx); if value.is_some() || is_literal { - write!(w, " = {expr};", expr = Escape(&expr)); + write_str(w, format_args!(" = {expr};", expr = Escape(&expr))); } else { w.push_str(";"); } @@ -1856,30 +1898,28 @@ fn item_constant( if value_lowercase != expr_lowercase && value_lowercase.trim_end_matches("i32") != expr_lowercase { - write!(w, " // {value}", value = Escape(value)); + write_str(w, format_args!(" // {value}", value = Escape(value))); } } } }); - write!(w, "{}", document(cx, it, None, HeadingOffset::H2)); + write_str(w, format_args!("{}", document(cx, it, None, HeadingOffset::H2))); } fn item_struct(w: &mut String, cx: &Context<'_>, it: &clean::Item, s: &clean::Struct) { - use crate::html::format::write_str as write; - wrap_item(w, |w| { render_attributes_in_code(w, it, cx); render_struct(w, it, Some(&s.generics), s.ctor_kind, &s.fields, "", true, cx); }); - write!(w, "{}", document(cx, it, None, HeadingOffset::H2)); + write_str(w, format_args!("{}", document(cx, it, None, HeadingOffset::H2))); item_fields(w, cx, it, &s.fields, s.ctor_kind); let def_id = it.item_id.expect_def_id(); - write!(w, "{}", render_assoc_items(cx, it, def_id, AssocItemRender::All)); - write!(w, "{}", document_type_layout(cx, def_id)); + write_str(w, format_args!("{}", render_assoc_items(cx, it, def_id, AssocItemRender::All))); + write_str(w, format_args!("{}", document_type_layout(cx, def_id))); } fn item_fields( @@ -1889,8 +1929,6 @@ fn item_fields( fields: &[clean::Item], ctor_kind: Option, ) { - use crate::html::format::write_str as write; - let mut fields = fields .iter() .filter_map(|f| match f.kind { @@ -1910,16 +1948,18 @@ fn item_fields( let field_name = field.name.map_or_else(|| index.to_string(), |sym| sym.as_str().to_string()); let id = cx.derive_id(format!("{typ}.{field_name}", typ = ItemType::StructField)); - write!( + write_str( w, - "\ - §\ - {field_name}: {ty}\ - ", - item_type = ItemType::StructField, - ty = ty.print(cx) + format_args!( + "\ + §\ + {field_name}: {ty}\ + ", + item_type = ItemType::StructField, + ty = ty.print(cx) + ), ); - write!(w, "{}", document(cx, field, Some(it), HeadingOffset::H3)); + write_str(w, format_args!("{}", document(cx, field, Some(it), HeadingOffset::H3))); } } } @@ -1968,9 +2008,7 @@ fn item_foreign_type(w: &mut impl fmt::Write, cx: &Context<'_>, it: &clean::Item } fn item_keyword(w: &mut String, cx: &Context<'_>, it: &clean::Item) { - use crate::html::format::write_str as write; - - write!(w, "{}", document(cx, it, None, HeadingOffset::H2)); + write_str(w, format_args!("{}", document(cx, it, None, HeadingOffset::H2))); } /// Compare two strings treating multi-digit numbers as single units (i.e. natural sort order). @@ -2241,17 +2279,17 @@ fn render_struct( structhead: bool, cx: &Context<'_>, ) { - use crate::html::format::write_str as write; - - write!( + write_str( w, - "{}{}{}", - visibility_print_with_space(it, cx), - if structhead { "struct " } else { "" }, - it.name.unwrap() + format_args!( + "{}{}{}", + visibility_print_with_space(it, cx), + if structhead { "struct " } else { "" }, + it.name.unwrap() + ), ); if let Some(g) = g { - write!(w, "{}", g.print(cx)); + write_str(w, format_args!("{}", g.print(cx))); } render_struct_fields( w, @@ -2275,8 +2313,6 @@ fn render_struct_fields( has_stripped_entries: bool, cx: &Context<'_>, ) { - use crate::html::format::write_str as write; - match ty { None => { let where_displayed = @@ -2297,23 +2333,30 @@ fn render_struct_fields( } for field in fields { if let clean::StructFieldItem(ref ty) = field.kind { - write!( + write_str( w, - "\n{tab} {vis}{name}: {ty},", - vis = visibility_print_with_space(field, cx), - name = field.name.unwrap(), - ty = ty.print(cx), + format_args!( + "\n{tab} {vis}{name}: {ty},", + vis = visibility_print_with_space(field, cx), + name = field.name.unwrap(), + ty = ty.print(cx) + ), ); } } if has_visible_fields { if has_stripped_entries { - write!(w, "\n{tab} /* private fields */"); + write_str( + w, + format_args!( + "\n{tab} /* private fields */" + ), + ); } - write!(w, "\n{tab}"); + write_str(w, format_args!("\n{tab}")); } else if has_stripped_entries { - write!(w, " /* private fields */ "); + write_str(w, format_args!(" /* private fields */ ")); } if toggle { toggle_close(&mut w); @@ -2327,7 +2370,7 @@ fn render_struct_fields( matches!(field.kind, clean::StrippedItem(box clean::StructFieldItem(..))) }) { - write!(w, "/* private fields */"); + write_str(w, format_args!("/* private fields */")); } else { for (i, field) in fields.iter().enumerate() { if i > 0 { @@ -2335,10 +2378,17 @@ fn render_struct_fields( } match field.kind { clean::StrippedItem(box clean::StructFieldItem(..)) => { - write!(w, "_"); + write_str(w, format_args!("_")); } clean::StructFieldItem(ref ty) => { - write!(w, "{}{}", visibility_print_with_space(field, cx), ty.print(cx)); + write_str( + w, + format_args!( + "{}{}", + visibility_print_with_space(field, cx), + ty.print(cx) + ), + ); } _ => unreachable!(), } @@ -2346,7 +2396,7 @@ fn render_struct_fields( } w.push_str(")"); if let Some(g) = g { - write!(w, "{}", print_where_clause(g, cx, 0, Ending::NoNewline)); + write_str(w, format_args!("{}", print_where_clause(g, cx, 0, Ending::NoNewline))); } // We only want a ";" when we are displaying a tuple struct, not a variant tuple struct. if structhead { @@ -2356,7 +2406,7 @@ fn render_struct_fields( Some(CtorKind::Const) => { // Needed for PhantomData. if let Some(g) = g { - write!(w, "{}", print_where_clause(g, cx, 0, Ending::NoNewline)); + write_str(w, format_args!("{}", print_where_clause(g, cx, 0, Ending::NoNewline))); } w.push_str(";"); }