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.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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())?;
|
||||
}
|
||||
@@ -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())?;
|
||||
|
||||
@@ -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())?;
|
||||
|
||||
@@ -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 {
|
||||
@@ -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};
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user