Skip to content

Commit f4a15ae

Browse files
Implement Print for FnOnce(&mut Buffer)
This means that callers can pass in a closure like `|buf| some_function(..., &mut buf)` and pass in arbitrary arguments to that function without complicating the trait definition. We also keep the impl for str and String, since it's useful to be able to just pass in "" or format!("{}"...) results in some cases. This changes Print's definition to take self, instead of &self, because otherwise FnOnce cannot be called directly. We could instead take FnMut or even Fn, but that seems like it'd merely complicate matters -- most of the time, the FnOnce does not constrain us at all anyway. If it does, a custom Print impl for &'_ SomeStruct is not all that painful.
1 parent f8bccb1 commit f4a15ae

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

src/librustdoc/html/format.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,25 @@ use crate::html::item_type::ItemType;
1919
use crate::html::render::{self, cache, CURRENT_DEPTH};
2020

2121
pub trait Print {
22-
fn print(&self, buffer: &mut Buffer);
22+
fn print(self, buffer: &mut Buffer);
2323
}
2424

25-
impl<T: ?Sized + Print> Print for &'_ T {
26-
fn print(&self, buffer: &mut Buffer) {
27-
(&**self).print(buffer)
25+
impl<F> Print for F
26+
where F: FnOnce(&mut Buffer),
27+
{
28+
fn print(self, buffer: &mut Buffer) {
29+
(self)(buffer)
2830
}
2931
}
3032

3133
impl Print for String {
32-
fn print(&self, buffer: &mut Buffer) {
33-
buffer.write_str(self);
34+
fn print(self, buffer: &mut Buffer) {
35+
buffer.write_str(&self);
3436
}
3537
}
3638

37-
impl Print for str {
38-
fn print(&self, buffer: &mut Buffer) {
39+
impl Print for &'_ str {
40+
fn print(self, buffer: &mut Buffer) {
3941
buffer.write_str(self);
4042
}
4143
}
@@ -92,7 +94,7 @@ impl Buffer {
9294
self.buffer.write_fmt(v).unwrap();
9395
}
9496

95-
crate fn to_display<T: ?Sized + Print>(mut self, t: &T) -> String {
97+
crate fn to_display<T: Print>(mut self, t: T) -> String {
9698
t.print(&mut self);
9799
self.into_inner()
98100
}

src/librustdoc/html/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct Page<'a> {
3434
pub fn render<T: fmt::Display, S: Print>(
3535
layout: &Layout,
3636
page: &Page<'_>,
37-
sidebar: &S,
37+
sidebar: S,
3838
t: &T,
3939
themes: &[PathBuf],
4040
) -> String {

src/librustdoc/html/render.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,7 @@ themePicker.onblur = handleThemeButtonsBlur;
11741174
})
11751175
.collect::<String>());
11761176
let v = layout::render(&cx.shared.layout,
1177-
&page, &(""), &content,
1177+
&page, "", &content,
11781178
&cx.shared.themes);
11791179
cx.shared.fs.write(&dst, v.as_bytes())?;
11801180
}
@@ -1921,7 +1921,7 @@ impl Context {
19211921
String::new()
19221922
};
19231923
let v = layout::render(&self.shared.layout,
1924-
&page, &sidebar, &all,
1924+
&page, sidebar, &all,
19251925
&self.shared.themes);
19261926
self.shared.fs.write(&final_file, v.as_bytes())?;
19271927

@@ -1937,7 +1937,7 @@ impl Context {
19371937
themes.push(PathBuf::from("settings.css"));
19381938
let v = layout::render(
19391939
&self.shared.layout,
1940-
&page, &sidebar, &settings,
1940+
&page, sidebar, &settings,
19411941
&themes);
19421942
self.shared.fs.write(&settings_file, v.as_bytes())?;
19431943

@@ -1994,7 +1994,7 @@ impl Context {
19941994

19951995
if !self.render_redirect_pages {
19961996
layout::render(&self.shared.layout, &page,
1997-
&Sidebar{ cx: self, item: it },
1997+
Sidebar{ cx: self, item: it },
19981998
&Item{ cx: self, item: it },
19991999
&self.shared.themes)
20002000
} else {
@@ -4267,7 +4267,7 @@ fn item_foreign_type(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item)
42674267
}
42684268

42694269
impl Print for Sidebar<'_> {
4270-
fn print(&self, buffer: &mut Buffer) {
4270+
fn print(self, buffer: &mut Buffer) {
42714271
let cx = self.cx;
42724272
let it = self.item;
42734273
let parentlen = cx.current.len() - if it.is_mod() {1} else {0};

src/librustdoc/html/sources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<'a> SourceCollector<'a> {
120120
static_extra_scripts: &[&format!("source-script{}", self.scx.resource_suffix)],
121121
};
122122
let v = layout::render(&self.scx.layout,
123-
&page, &(""), &Source(contents),
123+
&page, "", &Source(contents),
124124
&self.scx.themes);
125125
self.scx.fs.write(&cur, v.as_bytes())?;
126126
self.scx.local_sources.insert(p.clone(), href);

0 commit comments

Comments
 (0)