Skip to content

Commit 8a9dab3

Browse files
Remove *Space wrappers in favor of direct impls or functions
1 parent ec349be commit 8a9dab3

File tree

2 files changed

+84
-109
lines changed

2 files changed

+84
-109
lines changed

src/librustdoc/html/format.rs

Lines changed: 43 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,6 @@ impl Buffer {
108108
}
109109
}
110110

111-
/// Helper to render an optional visibility with a space after it (if the
112-
/// visibility is preset)
113-
#[derive(Copy, Clone)]
114-
pub struct VisSpace<'a>(pub &'a clean::Visibility);
115-
/// Similarly to VisSpace, this structure is used to render a function style with a
116-
/// space after it.
117-
#[derive(Copy, Clone)]
118-
pub struct UnsafetySpace(pub hir::Unsafety);
119-
/// Similarly to VisSpace, this structure is used to render a function constness
120-
/// with a space after it.
121-
#[derive(Copy, Clone)]
122-
pub struct ConstnessSpace(pub hir::Constness);
123-
/// Similarly to VisSpace, this structure is used to render a function asyncness
124-
/// with a space after it.
125-
#[derive(Copy, Clone)]
126-
pub struct AsyncSpace(pub hir::IsAsync);
127-
/// Similar to VisSpace, but used for mutability
128-
#[derive(Copy, Clone)]
129-
pub struct MutableSpace(pub clean::Mutability);
130-
pub struct AbiSpace(pub Abi);
131-
pub struct DefaultSpace(pub bool);
132-
133111
/// Wrapper struct for properly emitting a function or method declaration.
134112
pub struct Function<'a> {
135113
/// The declaration to emit.
@@ -638,12 +616,13 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
638616
clean::BareFunction(ref decl) => {
639617
if f.alternate() {
640618
write!(f, "{}{:#}fn{:#}{:#}",
641-
UnsafetySpace(decl.unsafety),
642-
AbiSpace(decl.abi),
619+
decl.unsafety.print_with_space(),
620+
print_abi_with_space(decl.abi),
643621
decl.print_generic_params(),
644622
decl.decl.print())
645623
} else {
646-
write!(f, "{}{}", UnsafetySpace(decl.unsafety), AbiSpace(decl.abi))?;
624+
write!(f, "{}{}",
625+
decl.unsafety.print_with_space(), print_abi_with_space(decl.abi))?;
647626
primitive_link(f, PrimitiveType::Fn, "fn")?;
648627
write!(f, "{}{}", decl.print_generic_params(), decl.decl.print())
649628
}
@@ -705,7 +684,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
705684
Some(l) => format!("{} ", l.print()),
706685
_ => String::new()
707686
};
708-
let m = MutableSpace(mutability);
687+
let m = mutability.print_with_space();
709688
let amp = if f.alternate() {
710689
"&".to_string()
711690
} else {
@@ -956,13 +935,13 @@ impl Function<'_> {
956935
}
957936
clean::SelfBorrowed(Some(ref lt), mtbl) => {
958937
args.push_str(
959-
&format!("{}{} {}self", amp, lt.print(), MutableSpace(mtbl)));
938+
&format!("{}{} {}self", amp, lt.print(), mtbl.print_with_space()));
960939
args_plain.push_str(
961-
&format!("&{} {}self", lt.print(), MutableSpace(mtbl)));
940+
&format!("&{} {}self", lt.print(), mtbl.print_with_space()));
962941
}
963942
clean::SelfBorrowed(None, mtbl) => {
964-
args.push_str(&format!("{}{}self", amp, MutableSpace(mtbl)));
965-
args_plain.push_str(&format!("&{}self", MutableSpace(mtbl)));
943+
args.push_str(&format!("{}{}self", amp, mtbl.print_with_space()));
944+
args_plain.push_str(&format!("&{}self", mtbl.print_with_space()));
966945
}
967946
clean::SelfExplicit(ref typ) => {
968947
if f.alternate() {
@@ -1032,14 +1011,8 @@ impl Function<'_> {
10321011
}
10331012
}
10341013

1035-
impl<'a> fmt::Display for VisSpace<'a> {
1036-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1037-
fmt::Display::fmt(&self.0.print_with_space(), f)
1038-
}
1039-
}
1040-
10411014
impl clean::Visibility {
1042-
fn print_with_space(&self) -> impl fmt::Display + '_ {
1015+
crate fn print_with_space(&self) -> impl fmt::Display + '_ {
10431016
display_fn(move |f| {
10441017
match *self {
10451018
clean::Public => f.write_str("pub "),
@@ -1060,29 +1033,33 @@ impl clean::Visibility {
10601033
}
10611034
}
10621035

1063-
impl fmt::Display for UnsafetySpace {
1064-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1065-
match self.0 {
1066-
hir::Unsafety::Unsafe => write!(f, "unsafe "),
1067-
hir::Unsafety::Normal => Ok(())
1036+
crate trait PrintWithSpace {
1037+
fn print_with_space(&self) -> &str;
1038+
}
1039+
1040+
impl PrintWithSpace for hir::Unsafety {
1041+
fn print_with_space(&self) -> &str {
1042+
match self {
1043+
hir::Unsafety::Unsafe => "unsafe ",
1044+
hir::Unsafety::Normal => ""
10681045
}
10691046
}
10701047
}
10711048

1072-
impl fmt::Display for ConstnessSpace {
1073-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1074-
match self.0 {
1075-
hir::Constness::Const => write!(f, "const "),
1076-
hir::Constness::NotConst => Ok(())
1049+
impl PrintWithSpace for hir::Constness {
1050+
fn print_with_space(&self) -> &str {
1051+
match self {
1052+
hir::Constness::Const => "const ",
1053+
hir::Constness::NotConst => ""
10771054
}
10781055
}
10791056
}
10801057

1081-
impl fmt::Display for AsyncSpace {
1082-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1083-
match self.0 {
1084-
hir::IsAsync::Async => write!(f, "async "),
1085-
hir::IsAsync::NotAsync => Ok(()),
1058+
impl PrintWithSpace for hir::IsAsync {
1059+
fn print_with_space(&self) -> &str {
1060+
match self {
1061+
hir::IsAsync::Async => "async ",
1062+
hir::IsAsync::NotAsync => "",
10861063
}
10871064
}
10881065
}
@@ -1156,32 +1133,30 @@ impl clean::TypeBinding {
11561133
}
11571134
}
11581135

1159-
impl fmt::Display for MutableSpace {
1160-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1161-
match *self {
1162-
MutableSpace(clean::Immutable) => Ok(()),
1163-
MutableSpace(clean::Mutable) => write!(f, "mut "),
1136+
impl clean::Mutability {
1137+
crate fn print_with_space(&self) -> &str {
1138+
match self {
1139+
clean::Immutable => "",
1140+
clean::Mutable => "mut ",
11641141
}
11651142
}
11661143
}
11671144

1168-
impl fmt::Display for AbiSpace {
1169-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1145+
crate fn print_abi_with_space(abi: Abi) -> impl fmt::Display {
1146+
display_fn(move |f| {
11701147
let quot = if f.alternate() { "\"" } else { "&quot;" };
1171-
match self.0 {
1148+
match abi {
11721149
Abi::Rust => Ok(()),
11731150
abi => write!(f, "extern {0}{1}{0} ", quot, abi.name()),
11741151
}
1175-
}
1152+
})
11761153
}
11771154

1178-
impl fmt::Display for DefaultSpace {
1179-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1180-
if self.0 {
1181-
write!(f, "default ")
1182-
} else {
1183-
Ok(())
1184-
}
1155+
crate fn print_default_space<'a>(v: bool) -> &'a str {
1156+
if v {
1157+
"default "
1158+
} else {
1159+
""
11851160
}
11861161
}
11871162

src/librustdoc/html/render.rs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ use crate::docfs::{DocFS, ErrorStorage, PathError};
6565
use crate::doctree;
6666
use crate::fold::DocFolder;
6767
use crate::html::escape::Escape;
68-
use crate::html::format::{Buffer, AsyncSpace, ConstnessSpace};
69-
use crate::html::format::{print_generic_bounds, WhereClause, href, AbiSpace, DefaultSpace};
70-
use crate::html::format::{VisSpace, Function, UnsafetySpace, MutableSpace};
68+
use crate::html::format::{Buffer, PrintWithSpace, print_abi_with_space};
69+
use crate::html::format::{print_generic_bounds, WhereClause, href, print_default_space};
70+
use crate::html::format::{Function};
7171
use crate::html::format::fmt_impl_for_trait_page;
7272
use crate::html::item_type::ItemType;
7373
use crate::html::markdown::{self, Markdown, MarkdownHtml, MarkdownSummaryLine, ErrorCodes, IdMap};
@@ -2573,13 +2573,13 @@ fn item_module(w: &mut Buffer, cx: &Context, item: &clean::Item, items: &[clean:
25732573
match *src {
25742574
Some(ref src) => {
25752575
write!(w, "<tr><td><code>{}extern crate {} as {};",
2576-
VisSpace(&myitem.visibility),
2576+
myitem.visibility.print_with_space(),
25772577
anchor(myitem.def_id, src),
25782578
name)
25792579
}
25802580
None => {
25812581
write!(w, "<tr><td><code>{}extern crate {};",
2582-
VisSpace(&myitem.visibility),
2582+
myitem.visibility.print_with_space(),
25832583
anchor(myitem.def_id, name))
25842584
}
25852585
}
@@ -2588,7 +2588,7 @@ fn item_module(w: &mut Buffer, cx: &Context, item: &clean::Item, items: &[clean:
25882588

25892589
clean::ImportItem(ref import) => {
25902590
write!(w, "<tr><td><code>{}{}</code></td></tr>",
2591-
VisSpace(&myitem.visibility), import.print());
2591+
myitem.visibility.print_with_space(), import.print());
25922592
}
25932593

25942594
_ => {
@@ -2794,7 +2794,7 @@ fn item_constant(w: &mut Buffer, cx: &Context, it: &clean::Item, c: &clean::Cons
27942794
render_attributes(w, it, false);
27952795
write!(w, "{vis}const \
27962796
{name}: {typ}</pre>",
2797-
vis = VisSpace(&it.visibility),
2797+
vis = it.visibility.print_with_space(),
27982798
name = it.name.as_ref().unwrap(),
27992799
typ = c.type_.print());
28002800
document(w, cx, it)
@@ -2805,8 +2805,8 @@ fn item_static(w: &mut Buffer, cx: &Context, it: &clean::Item, s: &clean::Static
28052805
render_attributes(w, it, false);
28062806
write!(w, "{vis}static {mutability}\
28072807
{name}: {typ}</pre>",
2808-
vis = VisSpace(&it.visibility),
2809-
mutability = MutableSpace(s.mutability),
2808+
vis = it.visibility.print_with_space(),
2809+
mutability = s.mutability.print_with_space(),
28102810
name = it.name.as_ref().unwrap(),
28112811
typ = s.type_.print());
28122812
document(w, cx, it)
@@ -2815,11 +2815,11 @@ fn item_static(w: &mut Buffer, cx: &Context, it: &clean::Item, s: &clean::Static
28152815
fn item_function(w: &mut Buffer, cx: &Context, it: &clean::Item, f: &clean::Function) {
28162816
let header_len = format!(
28172817
"{}{}{}{}{:#}fn {}{:#}",
2818-
VisSpace(&it.visibility),
2819-
ConstnessSpace(f.header.constness),
2820-
UnsafetySpace(f.header.unsafety),
2821-
AsyncSpace(f.header.asyncness),
2822-
AbiSpace(f.header.abi),
2818+
it.visibility.print_with_space(),
2819+
f.header.constness.print_with_space(),
2820+
f.header.unsafety.print_with_space(),
2821+
f.header.asyncness.print_with_space(),
2822+
print_abi_with_space(f.header.abi),
28232823
it.name.as_ref().unwrap(),
28242824
f.generics.print()
28252825
).len();
@@ -2828,11 +2828,11 @@ fn item_function(w: &mut Buffer, cx: &Context, it: &clean::Item, f: &clean::Func
28282828
write!(w,
28292829
"{vis}{constness}{unsafety}{asyncness}{abi}fn \
28302830
{name}{generics}{decl}{where_clause}</pre>",
2831-
vis = VisSpace(&it.visibility),
2832-
constness = ConstnessSpace(f.header.constness),
2833-
unsafety = UnsafetySpace(f.header.unsafety),
2834-
asyncness = AsyncSpace(f.header.asyncness),
2835-
abi = AbiSpace(f.header.abi),
2831+
vis = it.visibility.print_with_space(),
2832+
constness = f.header.constness.print_with_space(),
2833+
unsafety = f.header.unsafety.print_with_space(),
2834+
asyncness = f.header.asyncness.print_with_space(),
2835+
abi = print_abi_with_space(f.header.abi),
28362836
name = it.name.as_ref().unwrap(),
28372837
generics = f.generics.print(),
28382838
where_clause = WhereClause { gens: &f.generics, indent: 0, end_newline: true },
@@ -2913,8 +2913,8 @@ fn item_trait(
29132913
write!(w, "<pre class='rust trait'>");
29142914
render_attributes(w, it, true);
29152915
write!(w, "{}{}{}trait {}{}{}",
2916-
VisSpace(&it.visibility),
2917-
UnsafetySpace(t.unsafety),
2916+
it.visibility.print_with_space(),
2917+
t.unsafety.print_with_space(),
29182918
if t.is_auto { "auto " } else { "" },
29192919
it.name.as_ref().unwrap(),
29202920
t.generics.print(),
@@ -3175,7 +3175,7 @@ fn assoc_const(w: &mut Buffer,
31753175
extra: &str) {
31763176
write!(w, "{}{}const <a href='{}' class=\"constant\"><b>{}</b></a>: {}",
31773177
extra,
3178-
VisSpace(&it.visibility),
3178+
it.visibility.print_with_space(),
31793179
naive_assoc_href(it, link),
31803180
it.name.as_ref().unwrap(),
31813181
ty.print());
@@ -3240,12 +3240,12 @@ fn render_assoc_item(w: &mut Buffer,
32403240
};
32413241
let mut header_len = format!(
32423242
"{}{}{}{}{}{:#}fn {}{:#}",
3243-
VisSpace(&meth.visibility),
3244-
ConstnessSpace(header.constness),
3245-
UnsafetySpace(header.unsafety),
3246-
AsyncSpace(header.asyncness),
3247-
DefaultSpace(meth.is_default()),
3248-
AbiSpace(header.abi),
3243+
meth.visibility.print_with_space(),
3244+
header.constness.print_with_space(),
3245+
header.unsafety.print_with_space(),
3246+
header.asyncness.print_with_space(),
3247+
print_default_space(meth.is_default()),
3248+
print_abi_with_space(header.abi),
32493249
name,
32503250
g.print()
32513251
).len();
@@ -3259,12 +3259,12 @@ fn render_assoc_item(w: &mut Buffer,
32593259
write!(w, "{}{}{}{}{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
32603260
{generics}{decl}{where_clause}",
32613261
if parent == ItemType::Trait { " " } else { "" },
3262-
VisSpace(&meth.visibility),
3263-
ConstnessSpace(header.constness),
3264-
UnsafetySpace(header.unsafety),
3265-
AsyncSpace(header.asyncness),
3266-
DefaultSpace(meth.is_default()),
3267-
AbiSpace(header.abi),
3262+
meth.visibility.print_with_space(),
3263+
header.constness.print_with_space(),
3264+
header.unsafety.print_with_space(),
3265+
header.asyncness.print_with_space(),
3266+
print_default_space(meth.is_default()),
3267+
print_abi_with_space(header.abi),
32683268
href = href,
32693269
name = name,
32703270
generics = g.print(),
@@ -3399,7 +3399,7 @@ fn item_enum(w: &mut Buffer, cx: &Context, it: &clean::Item, e: &clean::Enum) {
33993399
write!(w, "<pre class='rust enum'>");
34003400
render_attributes(w, it, true);
34013401
write!(w, "{}enum {}{}{}",
3402-
VisSpace(&it.visibility),
3402+
it.visibility.print_with_space(),
34033403
it.name.as_ref().unwrap(),
34043404
e.generics.print(),
34053405
WhereClause { gens: &e.generics, indent: 0, end_newline: true });
@@ -3588,7 +3588,7 @@ fn render_struct(w: &mut Buffer, it: &clean::Item,
35883588
tab: &str,
35893589
structhead: bool) {
35903590
write!(w, "{}{}{}",
3591-
VisSpace(&it.visibility),
3591+
it.visibility.print_with_space(),
35923592
if structhead {"struct "} else {""},
35933593
it.name.as_ref().unwrap());
35943594
if let Some(g) = g {
@@ -3605,7 +3605,7 @@ fn render_struct(w: &mut Buffer, it: &clean::Item,
36053605
if let clean::StructFieldItem(ref ty) = field.inner {
36063606
write!(w, "\n{} {}{}: {},",
36073607
tab,
3608-
VisSpace(&field.visibility),
3608+
field.visibility.print_with_space(),
36093609
field.name.as_ref().unwrap(),
36103610
ty.print());
36113611
has_visible_fields = true;
@@ -3635,7 +3635,7 @@ fn render_struct(w: &mut Buffer, it: &clean::Item,
36353635
write!(w, "_")
36363636
}
36373637
clean::StructFieldItem(ref ty) => {
3638-
write!(w, "{}{}", VisSpace(&field.visibility), ty.print())
3638+
write!(w, "{}{}", field.visibility.print_with_space(), ty.print())
36393639
}
36403640
_ => unreachable!()
36413641
}
@@ -3662,7 +3662,7 @@ fn render_union(w: &mut Buffer, it: &clean::Item,
36623662
tab: &str,
36633663
structhead: bool) {
36643664
write!(w, "{}{}{}",
3665-
VisSpace(&it.visibility),
3665+
it.visibility.print_with_space(),
36663666
if structhead {"union "} else {""},
36673667
it.name.as_ref().unwrap());
36683668
if let Some(g) = g {
@@ -3674,7 +3674,7 @@ fn render_union(w: &mut Buffer, it: &clean::Item,
36743674
for field in fields {
36753675
if let clean::StructFieldItem(ref ty) = field.inner {
36763676
write!(w, " {}{}: {},\n{}",
3677-
VisSpace(&field.visibility),
3677+
field.visibility.print_with_space(),
36783678
field.name.as_ref().unwrap(),
36793679
ty.print(),
36803680
tab);
@@ -4186,7 +4186,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context, it: &clean::Item) {
41864186
write!(
41874187
w,
41884188
" {}type {};\n}}</pre>",
4189-
VisSpace(&it.visibility),
4189+
it.visibility.print_with_space(),
41904190
it.name.as_ref().unwrap(),
41914191
);
41924192

0 commit comments

Comments
 (0)