Skip to content

Commit

Permalink
Implement Print for FnOnce(&mut Buffer)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Mark-Simulacrum committed Sep 7, 2019
1 parent f8bccb1 commit f4a15ae
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
20 changes: 11 additions & 9 deletions src/librustdoc/html/format.rs
Expand Up @@ -19,23 +19,25 @@ use crate::html::item_type::ItemType;
use crate::html::render::{self, cache, CURRENT_DEPTH};

pub trait Print {
fn print(&self, buffer: &mut Buffer);
fn print(self, buffer: &mut Buffer);
}

impl<T: ?Sized + Print> Print for &'_ T {
fn print(&self, buffer: &mut Buffer) {
(&**self).print(buffer)
impl<F> Print for F
where F: FnOnce(&mut Buffer),
{
fn print(self, buffer: &mut Buffer) {
(self)(buffer)
}
}

impl Print for String {
fn print(&self, buffer: &mut Buffer) {
buffer.write_str(self);
fn print(self, buffer: &mut Buffer) {
buffer.write_str(&self);
}
}

impl Print for str {
fn print(&self, buffer: &mut Buffer) {
impl Print for &'_ str {
fn print(self, buffer: &mut Buffer) {
buffer.write_str(self);
}
}
Expand Down Expand Up @@ -92,7 +94,7 @@ impl Buffer {
self.buffer.write_fmt(v).unwrap();
}

crate fn to_display<T: ?Sized + Print>(mut self, t: &T) -> String {
crate fn to_display<T: Print>(mut self, t: T) -> String {
t.print(&mut self);
self.into_inner()
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/layout.rs
Expand Up @@ -34,7 +34,7 @@ pub struct Page<'a> {
pub fn render<T: fmt::Display, S: Print>(
layout: &Layout,
page: &Page<'_>,
sidebar: &S,
sidebar: S,
t: &T,
themes: &[PathBuf],
) -> String {
Expand Down
10 changes: 5 additions & 5 deletions src/librustdoc/html/render.rs
Expand Up @@ -1174,7 +1174,7 @@ themePicker.onblur = handleThemeButtonsBlur;
})
.collect::<String>());
let v = layout::render(&cx.shared.layout,
&page, &(""), &content,
&page, "", &content,
&cx.shared.themes);
cx.shared.fs.write(&dst, v.as_bytes())?;
}
Expand Down Expand Up @@ -1921,7 +1921,7 @@ impl Context {
String::new()
};
let v = layout::render(&self.shared.layout,
&page, &sidebar, &all,
&page, sidebar, &all,
&self.shared.themes);
self.shared.fs.write(&final_file, v.as_bytes())?;

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

Expand Down Expand Up @@ -1994,7 +1994,7 @@ impl Context {

if !self.render_redirect_pages {
layout::render(&self.shared.layout, &page,
&Sidebar{ cx: self, item: it },
Sidebar{ cx: self, item: it },
&Item{ cx: self, item: it },
&self.shared.themes)
} else {
Expand Down Expand Up @@ -4267,7 +4267,7 @@ fn item_foreign_type(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item)
}

impl Print for Sidebar<'_> {
fn print(&self, buffer: &mut Buffer) {
fn print(self, buffer: &mut Buffer) {
let cx = self.cx;
let it = self.item;
let parentlen = cx.current.len() - if it.is_mod() {1} else {0};
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/sources.rs
Expand Up @@ -120,7 +120,7 @@ impl<'a> SourceCollector<'a> {
static_extra_scripts: &[&format!("source-script{}", self.scx.resource_suffix)],
};
let v = layout::render(&self.scx.layout,
&page, &(""), &Source(contents),
&page, "", &Source(contents),
&self.scx.themes);
self.scx.fs.write(&cur, v.as_bytes())?;
self.scx.local_sources.insert(p.clone(), href);
Expand Down

0 comments on commit f4a15ae

Please sign in to comment.