diff options
| author | Boxy <rust@boxyuwu.dev> | 2025-02-25 21:27:44 +0000 |
|---|---|---|
| committer | Boxy <rust@boxyuwu.dev> | 2025-02-25 21:27:44 +0000 |
| commit | d9683df7c2f6d4141b1321e27635d2ce3167eaa4 (patch) | |
| tree | dce0d46d1b7d624ec9b9b09b2c1854f6245a5ff4 /src/librustdoc/html/layout.rs | |
| parent | 46392d1661540e256fd9573d8f06c2784a58c983 (diff) | |
| parent | 4ecd70ddd1039a3954056c1071e40278048476fa (diff) | |
| download | rust-d9683df7c2f6d4141b1321e27635d2ce3167eaa4.tar.gz rust-d9683df7c2f6d4141b1321e27635d2ce3167eaa4.zip | |
Merge from rustc
Diffstat (limited to 'src/librustdoc/html/layout.rs')
| -rw-r--r-- | src/librustdoc/html/layout.rs | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index d957cf1b569..df70df062fe 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -1,3 +1,4 @@ +use std::fmt::{self, Display}; use std::path::PathBuf; use rinja::Template; @@ -5,7 +6,6 @@ use rustc_data_structures::fx::FxIndexMap; use super::static_files::{STATIC_FILES, StaticFiles}; use crate::externalfiles::ExternalHtml; -use crate::html::format::{Buffer, Print}; use crate::html::render::{StylePath, ensure_trailing_slash}; #[derive(Clone)] @@ -71,7 +71,24 @@ struct PageLayout<'a> { pub(crate) use crate::html::render::sidebar::filters; -pub(crate) fn render<T: Print, S: Print>( +/// Implements [`Display`] for a function that accepts a mutable reference to a [`String`], and (optionally) writes to it. +/// +/// The wrapped function will receive an empty string, and can modify it, +/// and the `Display` implementation will write the contents of the string after the function has finished. +pub(crate) struct BufDisplay<F>(pub F); + +impl<F> Display for BufDisplay<F> +where + F: Fn(&mut String), +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut buf = String::new(); + self.0(&mut buf); + f.write_str(&buf) + } +} + +pub(crate) fn render<T: Display, S: Display>( layout: &Layout, page: &Page<'_>, sidebar: S, @@ -98,8 +115,8 @@ pub(crate) fn render<T: Print, S: Print>( let mut themes: Vec<String> = style_files.iter().map(|s| s.basename().unwrap()).collect(); themes.sort(); - let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar. - let sidebar = Buffer::html().to_display(sidebar); + let content = t.to_string(); // Note: This must happen before making the sidebar. + let sidebar = sidebar.to_string(); PageLayout { static_root_path, page, |
