Skip to content

Commit 0b0bf10

Browse files
committed
Generate static file hashes once
1 parent bf25334 commit 0b0bf10

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/librustdoc/html/render/write_shared.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,11 @@ pub(super) fn write_shared(
8585
}
8686

8787
if options.emit.is_empty() || options.emit.contains(&EmitType::Toolchain) {
88-
for f in static_files::STATIC_FILES_LIST {
89-
let filename = cx.dst.join(
90-
Path::new("static.files/").join(static_files::static_filename(f.filename, f.bytes)),
91-
);
92-
cx.shared.fs.write(filename, f.minified())?;
93-
}
88+
let static_dir = cx.dst.join(Path::new("static.files"));
89+
static_files::for_each(|f: &static_files::StaticFile| {
90+
let filename = static_dir.join(f.output_filename());
91+
cx.shared.fs.write(filename, f.minified())
92+
})?;
9493
}
9594

9695
/// Read a file and return all lines that match the `"{crate}":{data},` format,

src/librustdoc/html/static_files.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
66
use rustc_data_structures::fx::FxHasher;
77
use std::hash::Hasher;
8-
use std::path::PathBuf;
8+
use std::path::{Path, PathBuf};
99
use std::{fmt, str};
1010

1111
pub(crate) struct StaticFile {
12-
pub(crate) filename: &'static str,
12+
pub(crate) filename: PathBuf,
1313
pub(crate) bytes: &'static [u8],
1414
}
1515

1616
impl StaticFile {
17+
fn new(filename: &str, bytes: &'static [u8]) -> StaticFile {
18+
Self { filename: static_filename(filename, bytes), bytes }
19+
}
20+
1721
pub(crate) fn minified(&self) -> Vec<u8> {
1822
if self.filename.ends_with(".css") {
1923
minifier::css::minify(str::from_utf8(self.bytes).unwrap()).unwrap().to_string().into()
@@ -24,8 +28,8 @@ impl StaticFile {
2428
}
2529
}
2630

27-
pub(crate) fn output_filename(&self) -> PathBuf {
28-
static_filename(self.filename, self.bytes)
31+
pub(crate) fn output_filename(&self) -> &Path {
32+
&self.filename
2933
}
3034
}
3135

@@ -66,13 +70,18 @@ macro_rules! static_files {
6670
$(pub $field: StaticFile,)+
6771
}
6872

69-
pub(crate) const STATIC_FILES: StaticFiles = StaticFiles {
70-
$($field: StaticFile { filename: $file_path, bytes: include_bytes!($file_path) },)+
71-
};
73+
pub(crate) static STATIC_FILES: std::sync::LazyLock<StaticFiles> = std::sync::LazyLock::new(|| StaticFiles {
74+
$($field: StaticFile::new($file_path, include_bytes!($file_path)),)+
75+
});
7276

73-
pub(crate) static STATIC_FILES_LIST: &[&'static StaticFile] = &[
77+
pub(crate) fn for_each<E>(f: impl Fn(&StaticFile) -> Result<(), E>) -> Result<(), E> {
78+
for sf in [
7479
$(&STATIC_FILES.$field,)+
75-
];
80+
] {
81+
f(sf)?
82+
}
83+
Ok(())
84+
}
7685
}
7786
}
7887

0 commit comments

Comments
 (0)