From e0e0c3787ca64d8c2540d703ca1e2f26607c5717 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 13 Sep 2019 09:26:11 -0400 Subject: [PATCH] Replace SlashChecker with ensure_trailing_slash --- src/librustdoc/html/layout.rs | 4 ++-- src/librustdoc/html/render.rs | 22 ++++++++++------------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index 56074f4ab119..6414241727a7 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use crate::externalfiles::ExternalHtml; -use crate::html::render::SlashChecker; +use crate::html::render::ensure_trailing_slash; use crate::html::format::{Buffer, Print}; #[derive(Clone)] @@ -180,7 +180,7 @@ pub fn render( css_class = page.css_class, logo = { let p = format!("{}{}", page.root_path, layout.krate); - let p = SlashChecker(&p); + let p = ensure_trailing_slash(&p); if layout.logo.is_empty() { format!("\
\ diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 8d5bd0d2e16e..9064dc8b44db 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -33,7 +33,7 @@ use std::cmp::Ordering; use std::collections::{BTreeMap, VecDeque}; use std::default::Default; use std::error; -use std::fmt::{self, Display, Formatter, Write as FmtWrite}; +use std::fmt::{self, Formatter, Write as FmtWrite}; use std::ffi::OsStr; use std::fs::{self, File}; use std::io::prelude::*; @@ -82,16 +82,14 @@ mod tests; /// A pair of name and its optional document. pub type NameDoc = (String, Option); -pub struct SlashChecker<'a>(pub &'a str); - -impl<'a> Display for SlashChecker<'a> { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - if !self.0.ends_with("/") && !self.0.is_empty() { - write!(f, "{}/", self.0) +crate fn ensure_trailing_slash(v: &str) -> impl fmt::Display + '_ { + crate::html::format::display_fn(move |f| { + if !v.ends_with("/") && !v.is_empty() { + write!(f, "{}/", v) } else { - write!(f, "{}", self.0) + write!(f, "{}", v) } - } + }) } #[derive(Debug)] @@ -106,7 +104,7 @@ impl error::Error for Error { } } -impl Display for Error { +impl std::fmt::Display for Error { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { let file = self.file.display().to_string(); if file.is_empty() { @@ -1162,7 +1160,7 @@ themePicker.onblur = handleThemeButtonsBlur; .iter() .map(|s| { format!("
  • {}
  • ", - SlashChecker(s), s) + ensure_trailing_slash(s), s) }) .collect::()); let v = layout::render(&cx.shared.layout, @@ -2286,7 +2284,7 @@ fn print_item(cx: &Context, item: &clean::Item, buf: &mut Buffer) { fn item_path(ty: ItemType, name: &str) -> String { match ty { - ItemType::Module => format!("{}index.html", SlashChecker(name)), + ItemType::Module => format!("{}index.html", ensure_trailing_slash(name)), _ => format!("{}.{}.html", ty, name), } }