diff options
| author | Jacob Hoffman-Andrews <github@hoffman-andrews.com> | 2022-10-29 01:57:39 -0700 |
|---|---|---|
| committer | Jacob Hoffman-Andrews <github@hoffman-andrews.com> | 2022-10-29 12:48:08 -0700 |
| commit | 0b0bf10533ce955e6bcc44c2392674f53c2f3952 (patch) | |
| tree | de94e9942d01fdac4a3b20d36258da247d035331 | |
| parent | bf2533406670078a9110122f8c90e53285f994b6 (diff) | |
| download | rust-0b0bf10533ce955e6bcc44c2392674f53c2f3952.tar.gz rust-0b0bf10533ce955e6bcc44c2392674f53c2f3952.zip | |
Generate static file hashes once
| -rw-r--r-- | src/librustdoc/html/render/write_shared.rs | 11 | ||||
| -rw-r--r-- | src/librustdoc/html/static_files.rs | 27 |
2 files changed, 23 insertions, 15 deletions
diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index 07d139e9e12..94d8a9feca6 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -85,12 +85,11 @@ pub(super) fn write_shared( } if options.emit.is_empty() || options.emit.contains(&EmitType::Toolchain) { - for f in static_files::STATIC_FILES_LIST { - let filename = cx.dst.join( - Path::new("static.files/").join(static_files::static_filename(f.filename, f.bytes)), - ); - cx.shared.fs.write(filename, f.minified())?; - } + let static_dir = cx.dst.join(Path::new("static.files")); + static_files::for_each(|f: &static_files::StaticFile| { + let filename = static_dir.join(f.output_filename()); + cx.shared.fs.write(filename, f.minified()) + })?; } /// Read a file and return all lines that match the `"{crate}":{data},` format, diff --git a/src/librustdoc/html/static_files.rs b/src/librustdoc/html/static_files.rs index c922890bc0b..afe920b7fa1 100644 --- a/src/librustdoc/html/static_files.rs +++ b/src/librustdoc/html/static_files.rs @@ -5,15 +5,19 @@ use rustc_data_structures::fx::FxHasher; use std::hash::Hasher; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::{fmt, str}; pub(crate) struct StaticFile { - pub(crate) filename: &'static str, + pub(crate) filename: PathBuf, pub(crate) bytes: &'static [u8], } impl StaticFile { + fn new(filename: &str, bytes: &'static [u8]) -> StaticFile { + Self { filename: static_filename(filename, bytes), bytes } + } + pub(crate) fn minified(&self) -> Vec<u8> { if self.filename.ends_with(".css") { minifier::css::minify(str::from_utf8(self.bytes).unwrap()).unwrap().to_string().into() @@ -24,8 +28,8 @@ impl StaticFile { } } - pub(crate) fn output_filename(&self) -> PathBuf { - static_filename(self.filename, self.bytes) + pub(crate) fn output_filename(&self) -> &Path { + &self.filename } } @@ -66,13 +70,18 @@ macro_rules! static_files { $(pub $field: StaticFile,)+ } - pub(crate) const STATIC_FILES: StaticFiles = StaticFiles { - $($field: StaticFile { filename: $file_path, bytes: include_bytes!($file_path) },)+ - }; + pub(crate) static STATIC_FILES: std::sync::LazyLock<StaticFiles> = std::sync::LazyLock::new(|| StaticFiles { + $($field: StaticFile::new($file_path, include_bytes!($file_path)),)+ + }); - pub(crate) static STATIC_FILES_LIST: &[&'static StaticFile] = &[ + pub(crate) fn for_each<E>(f: impl Fn(&StaticFile) -> Result<(), E>) -> Result<(), E> { + for sf in [ $(&STATIC_FILES.$field,)+ - ]; + ] { + f(sf)? + } + Ok(()) + } } } |
